• Hi, I have a static home page with a slider and below a section for “recent news” which is 4 recent posts on a custom post type. I created a home.php page by using the index.php code.

    It has the main loop:

    <?php
    	// Start the Loop.
    	while ( have_posts() ) : the_post();
    
    		// Include the page content template.
    		get_template_part( 'content', 'page' );
    
    		// If comments are open or we have at least one comment, load up the comment template.
    		if ( comments_open() || get_comments_number() ) {
    			comments_template();
    		}
    	endwhile;
    ?>

    I want to add my second query to fetch the posts from my custom post type. How do I insert the code:

    <ul class="filterable-grid clearfix">
    	<?php $wpbp = new WP_Query(array(  'order'=> 'ASC', 'orderby' => 'title', 'post_type' => 'news', 'posts_per_page' =>'-1' ) ); ?>
    	<?php if ($wpbp->have_posts()) :  while ($wpbp->have_posts()) : $wpbp->the_post(); ?>
    	<?php $terms = get_the_terms(  get_the_ID(), 'news' ); ?>
    	<li data-id="id-<?php echo  $count; ?>" data-type="<?php foreach ($terms as $term) { echo  strtolower(preg_replace('/\s+/', '-', $term->name)). ' '; } ?>">
    		<a href="<?php the_permalink(); ?>" >
    			<figure class="bw">
    				<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) :  ?>
    				      <?php  the_post_thumbnail('houses'); ?>
    				<?php endif; ?>
    				<figcaption>
    					<?php echo get_the_title() , '</br> ', get_the_date();?>
    				</figcaption>
    			</figure>
    		</a>
    	</li>
    	<?php $count++; ?>
    	<?php endwhile; endif; ?>
    	<?php wp_reset_query(); ?>
    </ul>

    I inserted it after the main loop which I know is wrong because it shows up after the edit button outside of the main content. I could create a content-home.php with the custom php in it but it doesn’t include the slideshow.

    Thank you for your help

The topic ‘insert custom php in a loop’ is closed to new replies.