• My site is currently using 2 custom post types.
    On the home page, I want to pull up a list of all posts, whether of a custom type or not. However, I want to display them differently.

    For a default post, I would like to display the title & an excerpt, and for the custom post types I want to display their custom fields.

    <?php while ( have_posts() ) : the_post(); ?>
    
    				<a href = '<?php the_permalink(); ?>'><h2><?php the_title(); ?></h2></a>
    				<p><em><?php the_excerpt(); ?></em></p>
    
    				<p><em><?php echo(types_render_field("publish", array("arg1"=>"val1","arg2"=>"val2"))); ?> <?php echo(types_render_field("show", array("arg1"=>"val1","arg2"=>"val2"))); ?>,
    				<?php echo(types_render_field("date", array("arg1"=>"val1","arg2"=>"val2")));?></em></p>
    
    				<?php endwhile; ?>

    Currently I just have this, which is displaying an excerpt and custom types for all post types, including the default post (So it displays as just a ‘,’ after the excerpt).

    Basically I want to structure an if block to display the excerpt only if it’s NOT a custom post type, and display custom fields only if it IS a custom post type.
    Seems simple enough, but I’ve been having difficulty.

    Any help is greatly appreciated ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Try something like this in your while loop:

    //place code to display the title here
    if ( get_post_type( $post->ID ) == 'posttypename' ){
      //place code to display fields here
    } else {
      //place code to display excerpt here
    }

    Thread Starter sdaponte

    (@sdaponte)

    Hey bcworkz, thanks a lot for the help! It did exactly what I wanted.
    My next question: how do I display only posts that do not belong to a custom post type in another section of the page?

    You will need to make a 2nd loop like the following…

    <?php
    query_posts('post_type=post');
    if (have_posts()) : while (have_posts()) : the_post();
    ?>
    // Loop Stuff Here
    <?php endwhile; ?>
    <?php endif; ?>
    Thread Starter sdaponte

    (@sdaponte)

    Oh derp, I had done if (get_post_type($post->ID) != “television” && get_post_type($post->ID) != “writing”), but if I wanted to add more custom post types in the future that was probably the better solution.
    Thanks very much!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Post Type display on homepage’ is closed to new replies.