我想知道分类页面上的帖子有多粘,我在我的function.php
文件以进行管理。
贴子显示在分类页面上,一切正常。
但由于某种原因,使用此代码,当我访问一个预期显示404错误消息的页面时,它会显示博客页面。
Example:
如果我访问
example.com/fake-page
它显示的内容与我在
example.com/blog
我注意到一件奇怪的事是如果我example.com/real-category/fake-post
或example.com/fake-category/post
正如预期的那样,它正确地显示了404页。
我的permalink配置为/%category%/%postname%
.
Here is my code:
function get_term_sticky_posts()
{
// First check if we are on a category page, if not, return false
if ( !is_category() )
return false;
// Secondly, check if we have stickies, return false on failure
$stickies = get_option( \'sticky_posts\' );
if ( !$stickies )
return false;
// OK, we have stickies and we are on a category page, continue to execute. Get current object (category) ID
$current_object = get_queried_object_id();
// Create the query to get category specific stickies, just get post ID\'s though
$args = [
\'nopaging\' => true,
\'post__in\' => $stickies,
\'cat\' => $current_object,
\'ignore_sticky_posts\' => 1,
\'fields\' => \'ids\'
];
$q = get_posts( $args );
return $q;
}
add_action( \'pre_get_posts\', function ( $q )
{
if ( !is_admin() // IMPORTANT, make sure to target front end only
&& $q->is_main_query() // IMPORTANT, make sure we only target the main query
&& $q->is_category() // Only target category archives
) {
// Check if our function to get term related stickies exists to avoid fatal errors
if ( function_exists( \'get_term_sticky_posts\' ) ) {
// check if we have stickies
$stickies = get_term_sticky_posts();
if ( $stickies ) {
// Remove stickies from the main query to avoid duplicates
$q->set( \'post__not_in\', $stickies );
// Check that we add stickies on the first page only, remove this check if you need stickies on all paged pages
if ( !$q->is_paged() ) {
// Add stickies via the the_posts filter
add_filter( \'the_posts\', function ( $posts ) use ( $stickies )
{
$term_stickies = get_posts( [\'post__in\' => $stickies, \'nopaging\' => true] );
$posts = array_merge( $term_stickies, $posts );
return $posts;
}, 10, 1 );
}
}
}
}
});
我需要找到一些修复,以显示404时,该页面不存在。