【发布时间】:2015-12-15 15:28:38
【问题描述】:
在我的 Wordpress 网站中,我想防止任何未分类的帖子出现在网站搜索和存档页面中 - 包括最近帖子的首页。
我希望未分类帖子可见的唯一地方是实际帖子本身,以及作者存档页面上,例如 example.com/author/authorName
我一直在徒劳地寻找插件。我想肯定有一些自定义的php,但我的技术不是很深。
非常感谢任何帮助或线索!
【问题讨论】:
标签: wordpress
在我的 Wordpress 网站中,我想防止任何未分类的帖子出现在网站搜索和存档页面中 - 包括最近帖子的首页。
我希望未分类帖子可见的唯一地方是实际帖子本身,以及作者存档页面上,例如 example.com/author/authorName
我一直在徒劳地寻找插件。我想肯定有一些自定义的php,但我的技术不是很深。
非常感谢任何帮助或线索!
【问题讨论】:
标签: wordpress
您必须从 archive.php 和 index.php 的循环中排除该类别。此示例使用类别 ID 编号,您可以通过转到 帖子 >> 类别 找到该编号。您会看到每个类别的 ID。
在你上面提到的文件中,找到循环
<?php $query = new WP_Query( 'cat=-1' ); ?> // This is where you exclude. You can comma separate multiple categories : 'cat=-1,-2,-3' etc
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="post">
<!-- Here is the post content - use whatever your theme is using -->
</div>
<?php endwhile;
wp_reset_postdata();
else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
至于搜索结果,试试这个。这将进入functions.php(我还没有测试过,如果有问题请告诉我)。
add_filter( 'pre_get_posts' , 'search_exclude' );
function search_exclude( $query ) {
if( $query->is_admin )
return $query;
if( $query->is_search ) {
$query->set( 'category__not_in' , array( 1 ) ); // Category ID
}
return $query;
}
请注意:您也可以在此处使用逗号分隔类别:
$query->set( 'category__not_in' , array( 1,2,3 ) );
【讨论】:
<?php if (have_posts()) : ?> <!-- loops-wrapper --> <div id="loops-wrapper" class="loops-wrapper <?php echo $themify->layout . ' ' . $themify->post_layout; ?>"> <?php while (have_posts()) : the_post(); ?> <?php if(is_search()): ?> <?php get_template_part( 'includes/loop' , 'search'); ?> <?php else: ?> <?php get_template_part( 'includes/loop' , 'index'); ?> <?php endif; ?> <?php endwhile; ?> </div> <!-- /loops-wrapper --> <?php get_template_part( 'includes/pagination'); ?>