categories - Displaying just one newcurrent post per category in homepage

admin2025-06-04  3

I want to displaying only one new/current post per category in homepage. For example, there is 3 post with category A, and i want the old post from category A is replaced with the new post from category A in homepage, and the old post is just gone but not deleted. This also same for other categories.

I'm using pre_get_posts to approach this, but mine is just display one category only and the other is not.

Here's my code

function my_home_category( $query ) {
    if ( $query->is_home() ) {
        $query->set( 'cat', -1 );

        $query->set( 'posts_per_page', -1);
    }
}
add_action( 'pre_get_posts', 'my_home_category' );

I know my code is not good, and i gladly appreciated any help that come to mind. thanks

I want to displaying only one new/current post per category in homepage. For example, there is 3 post with category A, and i want the old post from category A is replaced with the new post from category A in homepage, and the old post is just gone but not deleted. This also same for other categories.

I'm using pre_get_posts to approach this, but mine is just display one category only and the other is not.

Here's my code

function my_home_category( $query ) {
    if ( $query->is_home() ) {
        $query->set( 'cat', -1 );

        $query->set( 'posts_per_page', -1);
    }
}
add_action( 'pre_get_posts', 'my_home_category' );

I know my code is not good, and i gladly appreciated any help that come to mind. thanks

Share Improve this question edited Jan 4, 2019 at 9:10 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Jan 4, 2019 at 9:03 Minyak IkanMinyak Ikan 112 bronze badges 1
  • You can't do this with a single query. You will need to get a list of categories, loop over them, and query 1 post from each, separately. – Jacob Peattie Commented Jan 4, 2019 at 9:20
Add a comment  | 

1 Answer 1

Reset to default 0

As you are not comfortable with coding, you can try using Recent Posts Widget Extended plugin. It allows you to specify the number of posts per category that you want to be displayed.

Follow these steps:

  1. Install and activate the plugin.
  2. Head to Appearance > Widgets. Add Recent Posts Extended widget to your sidebar.
  3. The widget menu will expand to show its settings. Here, you can choose your desired category and specify the number of posts to show.
  4. Finally, click on the Save button to store your widget settings.
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749044658a315883.html

最新回复(0)