1

I've been away from Wordpress for a few months, and now I'm struggling to remember how to create a Page of Posts. It's most infuriating!

The Current Site

I have home.php set up to display the latest Posts of a Custom Post Type as the main page. It works fine. A simple pre_get_posts function in functions.php sets the CPT to be displayed:

$query->set('post_type', 'campaign');
$query->set('posts_per_page', 11);

...when is_home() && $query->is_main_query(). This is correct, and does not need to be changed. It works great, but now the client wants a normal news blog elsewhere on the site.

The New Version

So... I need to add a news blog, that's not on the homepage, leaving the homepage as it is. It makes sense to me to use the default Post post type for this, and it makes sense for me to create a Page for this. But I can't seem to get a Page of Posts to work.

At first I created a page-blog.php template for a "News" Page, but it didn't pull in Posts with while ( have_posts() ) : the_post();. Instead it only listed itself...?

I then added a new line to my pre_get_posts function:

if(is_page_template('page-news.php')) {
    $query->set('post_type', 'post');
}

But now the page returns 404.

What am I doing wrong?

3 Answers 3

1

Try this code in your page-blog.php to display list of posts

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
        'post_type' => 'post',    //Change this with your post type
        'posts_per_page' => 10,  //No. of Pages to show         
        'offset' => 0,   //excluding the latest post if any
        'paged' => $paged  //For Pagination
    );

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();?>
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title();?></a></h3>
<?php    the_content(__('Continue Reading'));             
endwhile;

wp_reset_postdata();
1
1

Changing the post type on the home page

By default, WordPress shows the post post type on your home page To add pages, open your theme’s functions.php file and paste this PHP code into it:

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'page', 'album', 'movie' ) );

    return $query;
}

Showing the post types in your feed Realizing that many of you might want to also add these post types to your feed to match your blog, a small change in the code is required. All you need to do is change this line:

if ( is_home() && $query->is_main_query() )

We’ll use the is_feed() conditional tag:

if ( ( is_home() && $query->is_main_query() ) || is_feed() )

Now, you can have custom post types in your regular blog post rotation and your feed.

0
0

Try This:post_type=custom_post type slug

<?php
    global $paged;  
        if( get_query_var( 'paged' ) ) {
            $paged = get_query_var( 'paged' );
        } elseif( get_query_var( 'page' ) ) {
            $paged = get_query_var( 'page' );
        } else {
            $paged = 1;
        }

        $args = array(
            'post_type'     => 'your_post_type',
            'paged'         => $paged,
        );

        $blog_query = new WP_Query($args);
    ?>

    <section>
        <?php if ( $blog_query->have_posts() ) : ?>
        <?php while ( $blog_query->have_posts() ) : $blog_query->the_post(); ?>
        <?php get_template_part( 'content-parts/content', get_post_format() ); ?>
        <?php endwhile; ?>
        <?php endif; ?>
    </section><!-- End #content -->
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.