Support » Fixing WordPress » How loop trough Custom Post Types content including the_field

  • Hi guys recently I had a task to loop trough Custom Post Types like menus and get the content of each post like menu.
    So I made this code

    <?php
    
    			$args = array(
    			   'post_type' => 'menus'
    			, 'posts_per_page' => 4 );
    			$the_query = new WP_Query( $args );
    			?>
    			<?php if ( $the_query->have_posts() ) : ?>
    
    			<div id="owl-demo" class="owl-carousel owl-theme">
    				<?php while ( $the_query->have_posts() ) { ?>
    				<?php $the_query->the_post(); ?>
    			<div class="item">
    				<?php the_title(); the_content()?>
    
    			</div>
    				<?php };?>
    
    				<?php wp_reset_postdata(); ?>
    
    			<?php else:  ?>
    				<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    				<?php endif; ?>
    				 </div>

    The thing is that this brings only the title of post. I use ACF plugin’s function as the_field(). And put all my post’s code in one single_menus code. Is it right way? What kind of solution yuo can give. Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • i use ul li for looping

    
    <?php
    
    $args = array(
    	'post_type'      => 'menus',
    	'posts_per_page' => 4,
    );
    
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if ( $the_query->have_posts() ) : ?>
    
    	<div id="owl-demo" class="owl-carousel owl-theme">
    
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    			<div class="item">
    				<?php the_title(); ?>
    				<?php the_content(); ?>
    			</div>
    		<?php endwhile; ?>
    
    		<?php wp_reset_postdata(); ?>
    
    	</div>
    <?php else : ?>
    	<p><?php _e( 'Sorry, no posts matched your criteria.', 'language_name_here' ); ?></p>
    <?php endif; ?>
    

    You have some small changes you should tidy up. See my cleaned up example of your code. You were ending your </div> late after the if statement.

    • This reply was modified 6 years, 4 months ago by xBLADEx.

    As far as ACF goes, yes use the_field( 'acf_name' ) inside your .item div.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How loop trough Custom Post Types content including the_field’ is closed to new replies.