我构建了一个插件pre_get_posts()
, 这是我最早修改过的帖子。
但我也想看看这篇帖子之后的最新修改。有人有主意吗?
这是我的代码:
function modified_one_desc( $query) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( \'posts_per_page\', 1 );
$query->set(\'category_name\',\'Blog\');
$query->set(\'orderby\',\'modified\');
$query->set(\'order\',\'DESC\');
}
}
add_action( \'pre_get_posts\', \'modified_one_desc\' );
function modified_one_asc( $query) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( \'posts_per_page\', 1 );
$query->set(\'category_name\',\'Blog\');
$query->set(\'orderby\',\'modified\');
$query->set(\'order\',\'ASC\');
}
}
add_action( \'pre_get_posts\', \'modified_one_asc\' );
只有一个有效。
最合适的回答,由SO网友:Pieter Goosen 整理而成
我认为最简单的方法是使用the_posts
滤器
add_filter( \'the_posts\', function ( $posts, \\WP_Query $q )
{
// Only target the main query on the home page
if ( $q->is_main_query()
&& $q->is_home()
&& !$q->is_paged() // Only target page one
) {
// Get the newest modified post
$args = [
\'posts_per_page\' => 1,
\'category_name\' => \'blog\', // Note, this must be slug
\'order\' => \'ASC\',
\'orderby\' => \'modified\'
];
$newest = get_posts( $args );
// Make sure we have a post to inject
if ( !$newest )
return $posts;
// We have a post, append it to our array of posts
$posts = array_merge( $newest, $posts );
}
return $posts;
}, 10, 2 );
您可以根据需要进行调整