哦,对你问题的评论很清楚。如果你想总是返回一篇最新的帖子,无论带有菜单链接的页面是否刷新,都必须用另一种方式解决。
首先:创建新页面,比如说:“最新帖子”-注意它的ID。
然后将其添加到函数中。php:
function binda_redirect_to_latest( $query ) {
$page_id = 234; //ID of your page with Latest post
if ( $query->is_page( $page_id ) && $query->is_main_query() ) {
$args = array( \'numberposts\' => 1, \'orderby\' => \'post_date\', \'order\' => \'DESC\', \'post_type\' => \'magazine_issue\', \'post_status\' => \'publish\' );
$latest_post = get_posts( $args );
foreach( $latest_post as $p ){
$query->set( \'p\', $p->ID );
$query->set( \'page_id\', \'\' );
}
}
return $query;
}
add_action( \'pre_get_posts\', \'binda_redirect_to_rand\' );
瞧
如果不想创建额外的页面,可以注册一个新的查询变量和chceck,因为它存在于pre\\u get\\u posts挂钩中。这样做:
add_filter( \'query_vars\', \'my_query_vars\' );
function my_query_vars( $vars ) {
$vars[] = \'latest_post\';
return $vars;
}
修改的pre\\u get\\u posts挂钩回调:
function binda_redirect_to_latest( $query ) {
if ( $query->is_home() && isset($query->query_vars[\'latest_post\']) && $query->query_vars[\'latest_post\'] == 1 && $query->is_main_query() ) {
$args = array( \'numberposts\' => 1, \'orderby\' => \'post_date\', \'order\' => \'DESC\', \'post_type\' => \'magazine_issue\', \'post_status\' => \'publish\' );
$latest_post = get_posts( $args );
foreach( $latest_post as $p ){
$query->set( \'p\', $p->ID );
}
}
return $query;
}
add_action( \'pre_get_posts\', \'binda_redirect_to_rand\' );
要创建ling,请添加\\u query\\u arg:
echo add_query_arg( \'latest_post\', 1, get_bloginfo(\'url\') );