Support

Account

Home Forums ACF PRO Flexible Content Field Called From Variable

Solving

Flexible Content Field Called From Variable

  • I have a php file that I use include() from multiple page templates to return my flexible content. The file looks like this:

    <?php if( have_rows('qd_flex_page_content') ):
        $flexCounter = 0; ?>
        <div id="pageContentBlocks">
            <?php while ( have_rows('qd_flex_page_content') ) :
                the_row();
                $flexCounter ++;
                $contentBlockType = get_row_layout();
                switch ($contentBlockType) {
                    case 'pathway_block':
                        include('flexblocks/pathways.php');
                        break;
                    case 'checkerboard_content':
                        include('flexblocks/checkerboard_content.php');
                        break;
                }
            endwhile; ?>
        </div>    
    <?php endif; ?>

    On the 404 and Search Results page, I want to include flexible content as well. I’ve added the fields on an Options page, but now I’m unsure how to add to the above file to allow for this, without needing to duplicate content. What I’d like to do is something like this:

    <?php 
    	if(is_404()){
    		$myFlexBlocks = get_field('qd_flex_content_404','option');
    	}elseif(is_search()){
    		$myFlexBlocks = get_field('qd_flex_content_search','option');
    	}else{
    		$myFlexBlocks = get_field('qd_flex_page_content');
    	}
    	if( have_rows($myFlexBlocks) ):
        $flexCounter = 0; ?>
        <div id="pageContentBlocks">
            <?php while ( have_rows($myFlexBlocks) ) :
                the_row();
                $flexCounter ++;
                $contentBlockType = get_row_layout();
                switch ($contentBlockType) {
                    case 'pathway_block':
                        include('flexblocks/pathways.php');
                        break;
                    case 'checkerboard_content':
                        include('flexblocks/checkerboard_content.php');
                        break;
                }
            endwhile; ?>
        </div>    
    <?php endif; ?>

    I know this code doesn’t work, but I was hoping someone might know how to make something like this work?

  • So I just realized I’ve been overlooking a rather simple solution:

    <?php if(is_404()||is_search()){
    		$myFlexID = 'option';
    	}else{
    		$myFlexID = get_the_ID();
    	}
    if( have_rows('qd_flex_page_content', $myFlexID) ):
        $slideshow = false;
        $flexCounter = 0; ?>
        <div id="pageContentBlocks">
            <?php while ( have_rows('qd_flex_page_content', $myFlexID) ) :
  • Did this solve the question?

  • my solution sort of solved what i was ultimately trying to do, but my question still remains, is there a way to store the value of a flexible content or repeater field to a variable, and then run the have_rows or something else on it to access the sub_fields and run through the field content as normal.

  • Since you’re using include to include different files, any variable you set in the code where it’s included will be available in the included file.

    If you were using get_template_part() it would be a bit more difficult.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.