Site logo

Archived topic

Control posts shown on first page of blog

35 replies · Started by Conor on September 16, 2020

Viewing posts 31–36 of 36

Hmm I actually tested the code and it worked for me.

How are you adding it?

Can you make sure it's the correct syntax and quotation marks?

function generate_home_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'category_name', 'secret-category' );
    }
}
add_action( 'pre_get_posts', 'generate_home_category' );

Leo, it worked this time!! Only one thing left to do is that I need to make it work with this other piece of code.

add_action( 'pre_get_posts', function( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}

if ( is_category('your-category-slug') ) {
$query->set( 'posts_per_page', 15 );
}

if ( is_home() ) {
$query->set( 'posts_per_page', 15 );
}

} );

I think I may have messed up the syntax with this one too. My hope is that the home page will still only show secret category, and since I only have 6 posts in that category it should only show 6 posts.. then, I would like all the other category pages to show 15 per page.

Thank you very much leo, you and the team are amazing.

C

I still think WP Show Posts is your best bet rather than trying to modify the loop that much.

We were pretty close with the styling no?

Leo, I changed the settings in the reading tab, and now everything works perfectly.

Thank you so much for all the help with this one.. You can check it out conorwriter.com - i have 6 articles on the home page, then I have the 15 on the latest articles category page

This is exactly what I want and it works

Glad to hear :)

This archived topic is closed to new replies.