Site logo

Archived topic

Show specific post category on Home

12 replies · Started by Mauro on November 7, 2016

Viewing posts 1–13 of 13

Hi,
I'd like to show only a specific post category on the homepage of my site, but I can't figure how to do it.
Can someone please help me??

Thank you so much

Hi Mauro,

You should be able to do that with Tom's WP Show post plugin: https://wpshowposts.com/

Hope this helps!

It doesn't work.... But surely i'm missing something.
My category name is "Prossimi Eventi" and the slug is prossimi-eventi.
the code i pasted in my child theme's function.php is:

function generate_home_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'prossimi_eventi', 'prossimi-eventi' );
    }
}
add_action( 'pre_get_posts', 'generate_home_category' );

Instead of:

$query->set( 'prossimi_eventi', 'prossimi-eventi' );

Try:

$query->set( 'category_name', 'prossimi-eventi' );

Nope....
It didn't work too

Hmm, well that's the way to do it. You'll need to play with it and make sure your category slug is correct etc..

I have a plugin named Simple Custom Post Order installed, which let me set manually the post order. May this cause conflict with the php code you post?

It's possible, worth testing.

GOT TO WORK!

Don't know what i missed but with this code, and the plugin too, it works!

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

Thank you so much! :D

Awesome! Glad you got it working :)

Hi!

Is it better to disable "Blog" plugin in case i want to use "WP Show Post"?

No need - you can use both if you want, or just one. Having them both doesn't hurt anything :)

This archived topic is closed to new replies.