$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - Displaying just one newcurrent post per category in homepage|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Displaying just one newcurrent post per category in homepage

matteradmin11PV0评论

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.
Post a comment

comment list (0)

  1. No comments so far