WordPress.org

Ready to get started?Download WordPress

Forums

wanting Custom Post type to display on home page with image (1 post)

  1. Jeff
    Member
    Posted 11 months ago #

    Hi Everyone I hope that you can help me clean up this code. I got it working but there just has to be an easier way.

    I am trying to display each custom post type with the featured image in a grid on the home page template. Below is the example of my "portfolio" and "testimonial" CPT.

    I start a query, loop it, and then end it; and start another query for the different post type.

    <?php
    // The Query
    $query = new WP_Query( array('post_type' => 'portfolio','posts_per_page' => 1));
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		}
    
    ?>
      <div id="transitions_panel_wrapper">
        <div class="condense" id="transition_1">
    
              <h2><?php the_title(); ?></h2>
              <p> <?php if ( has_post_thumbnail()) : ?>
              	<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
               	<?php the_post_thumbnail(); ?>
              	</a>
             	<?php endif; ?>
             	</p>
    			<p><?php the_excerpt()?></p>
        </div> <!--end #transition_1-->
    			<?php
                } else { // no posts found
                }
                /* Restore original Post Data */
                wp_reset_postdata();
                ?>
    
                <?php
    // The Query
    $query = new WP_Query( array('post_type' => 'testimonial','posts_per_page' => 1));
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		}
    
    ?>
        <div class="condense" id="transition_2">
          <h2><?php the_title(); ?></h2>
              <p> <?php if ( has_post_thumbnail()) : ?>
              	<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
               	<?php the_post_thumbnail(); ?>
              	</a>
             	<?php endif; ?>
             	</p>
    			<p><?php the_excerpt()?></p>
        </div> <!--end #transition_2-->
    			<?php
                } else { // no posts found
                }
                /* Restore original Post Data */
                wp_reset_postdata();
                ?>
    
    <?php

    Thanks for your help, I try to keep things simple

Reply

You must log in to post.

About this Topic