• Resolved Raider000

    (@raider000)


    Hi

    I have created my Custom Post Type and I see it in the backend and I can create posts in it. I use this in my index.php:

    <?php if (is_single()) : ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    but it won’t work. It just shows me the normal posts but none of my Custom Post Types. Help?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You need to first query your custom post.. then return it through the loop.

    Something like this should work:

    $args = array( 'post_type' => 'SLUG', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    	the_title();
    	echo '<div class="entry-content">';
    	the_content();
    	echo '</div>';
    endwhile;

    Just change “SLUG” to the actual slug you used for the custom post type.

    Thread Starter Raider000

    (@raider000)

    You sir are my hero!!

    I have already tried it like this:

    <?php
    if(is_single()) :
    // Post args
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $arr_post_args = array(
    		'posts_per_page' => 10,
    		'post_type' => 'news',
    		'orderby' => 'date',
    		'order' => 'DESC',
    		'paged' => $paged
    );
    
    // Post Query
    query_posts($arr_post_args);
    endif;
    ?>

    But it didn’t work. Your code works perfectly for me though. Thank you!!

    ?? My pleasure!

    The code you are using above is an advanced form of querying the argument… but it doesn’t do anything to actually output the data.

    You could combine both into something like this:

    $args = array(
    		'posts_per_page' => 10,
    		'post_type' => 'news',
    		'orderby' => 'date',
    		'order' => 'DESC',
    		'paged' => $paged
    );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    	the_title();
    	echo '<div class="entry-content">';
    	the_content();
    	echo '</div>';
    endwhile;

    That’ll give you a little advanced flexibility on how to query the custom posts.

    Thread Starter Raider000

    (@raider000)

    Oh I see, this is even much better … just what I wanted.

    Thank you so much, you were a great help!!

    You bet!!

    (Please mark your thread as “resolved” to help others’ in the future)

    Happy blogging!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Post Type won't appear’ is closed to new replies.