Support

Account

Home Forums Front-end Issues Display multiple sets of post using flexible content field

Solved

Display multiple sets of post using flexible content field

  • I’m trying to build a simple page builder where I can list out posts using flexible content. I want to be able to add a new flexible content field to list a number of posts, then adding a new entry of the same flexible content field to display another set of posts.

    Currently, I’ve been successful listing posts and adding custom fields to e.g. “select number of posts to be shown”, and “post offset”, but it only does this once, even if I add several flexible content fields.

    How do I separate these queries to make each field display their own set of posts separately (when using the same flexible content field)?

    
    <?php
    
    /**
     * Template Name: Page builder
     */
    
    add_action('page_builder_content', 'do_page_builder_content');
    
    function do_page_builder_content() {
    
    ?>
    
    	<main>
    		
    		<?php	
    		/* -------------------------
    		 * DISPLAY POSTS
    		 * ---------------------- */
    
    		// check if the flexible content field has rows of data
    		if( have_rows('pa_page_builder') ):
    
    		    // loop through the rows of data
    		    while ( have_rows('pa_page_builder') ) : the_row();
    
    		        if( get_row_layout() == 'pa_article_list' ):
    
    		        	/* Number of articles to display */
    		        	$articlesToShow = get_sub_field('pa_articles_to_show');
    		        	/* Post offset */
    		        	$articlesOffset = get_sub_field('pa_articles_offset');
    		        	
    
    		        endif;
    
    		    endwhile;
    
    			// args
    			$args = array(
    				'posts_per_page'	=> $articlesToShow,
    				'post_type'			=> 'post',
    				'orderby' 			=> 'post_date',
    				'offset'            => $articlesOffset,
    				/* 'category_name' => 'category1, category2' */
    
    			);
    
    			// query
    			$the_query = new WP_Query( $args );
    
    			?>
    
    			<?php if( $the_query->have_posts() ): ?>
    				<ul>
    				<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    					<li>
    						<a href="<?php the_permalink(); ?>">
    							<fig><?php the_post_thumbnail(thumbnail); ?></fig>
    							<?php the_title(); ?>
    						</a>
    						<?php the_excerpt(); ?>
    					</li>
    				<?php endwhile; ?>
    				</ul>
    			<?php endif; ?>
    
    			<?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
    
    		<?php
    
    		else :
    
    		    // no layouts found
    
    		endif;
    
    		?>
    
    	</main>
    
    <?php
    
    }
    
    get_header(); 
    
    do_action('page_builder_content');
    
    get_footer(); 
    
  • Okay, I feel so stupid for not seeing that the end tags for the loops were out of place…

    
    endif;
    
    endwhile;
    

    Should have been placed before

    
    else :
    
    // no layouts found
    

    That fixed it.

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

You must be logged in to reply to this topic.