admin管理员组文章数量:1392095
Is there a way to show scheduled posts in the regular loop but have them not link? I'd like to show that they're coming soon.
This code makes them visible, but you can click into the post, which I don't want.
add_action( 'pre_get_posts', function ( $wp_query ) {
global $wp_post_statuses;
if (
! empty( $wp_post_statuses['future'] ) &&
! is_admin() &&
$wp_query->is_main_query()
) {
$wp_post_statuses['future']->public = true;
}
});
Is there a way to show scheduled posts in the regular loop but have them not link? I'd like to show that they're coming soon.
This code makes them visible, but you can click into the post, which I don't want.
add_action( 'pre_get_posts', function ( $wp_query ) {
global $wp_post_statuses;
if (
! empty( $wp_post_statuses['future'] ) &&
! is_admin() &&
$wp_query->is_main_query()
) {
$wp_post_statuses['future']->public = true;
}
});
Share
Improve this question
asked Feb 3, 2020 at 14:18
ElizabethElizabeth
3772 silver badges13 bronze badges
1 Answer
Reset to default 1Is there a way to show scheduled posts in the regular loop but have them not link? I'd like to show that they're coming soon.
Yes, if you don't want to link to them, then don't create links for them.
This code makes them visible, but you can click into the post, which I don't want.
pre_get_posts
changes what posts a query fetches, it has nothing to do with how those posts are displayed, how they are displayed is entirely the job of the theme templates.
Oddly though, your pre_get_posts
filter does not modify the query object. The consequence of this is that all following queries will have future posts, even if they aren't the main query, as long as they happen after it.
You would be much better off just modifying the query instead of changing WP globals e.g.
add_action( 'pre_get_posts', function ( $wp_query ) {
if ( is_admin() ) {
return;
}
if ( ! $wp_query->is_main_query()) {
return;
}
$wp_query->set( 'post_status', [ 'publish', 'future' ] );
});
So, how do you only link to the published ones? Easy, just check the status and only link if it's publish
if ( get_post_status() == 'publish' ) {
// show the post in the loop normally
} else {
// it's a scheduled post, show it differently
}
本文标签: Show scheduled posts in loop (but don39t link to them)
版权声明:本文标题:Show scheduled posts in loop (but don't link to them)? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744780829a2624701.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论