我想修改搜索。php,这样它只显示帖子而不显示页面。你能帮忙吗?
这是搜索中的循环。php:
<?php if (have_posts()) : ?>
<?php while(have_posts()): the_post(); ?>
我想修改搜索。php,这样它只显示帖子而不显示页面。你能帮忙吗?
这是搜索中的循环。php:
<?php if (have_posts()) : ?>
<?php while(have_posts()): the_post(); ?>
搜索模板太晚了,无法更改任何内容:页面已在此处搜索,您的页码可能会错误。不要更改输出,而是更改查询。
滤器posts_search
相反,如果有搜索词,则过滤posts_where_paged
并删除不希望搜索的帖子类型。
示例代码:
is_admin() || add_filter( \'posts_search\', function( $search )
{
empty ( $search ) || add_filter( \'posts_where_paged\', function( $where ) {
return preg_replace(
\'~post_type IN \\((.*)\\\'page\\\',?~\',
\'post_type IN (\\1\',
$where
);
});
return $search;
});
不久前我写了一篇关于这个的文章:Excluding Certain Posts Or Pages From Search Results In WordPress
但其长短不一之处在于:
function mySearchFilter($query) {
if ($query->is_search) {
$query->set(\'post_type\', \'post\');
};
return $query;
};
add_filter(\'pre_get_posts\',\'mySearchFilter\');
您可以将其添加到主题中functions.php
文件它所做的是在WP实际运行之前过滤WP运行的主数据库查询,并告诉WP只查找帖子。
用这个替换你的循环,并让我知道这是否有效。
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();?>
<?php if (is_page()) continue; ?>
<?php endwhile; ?>
<?php endif; ?>
我试图使用一组页面模板(如日期、存档、标记等)创建8个侧边栏区域,然后在数组中循环,为每个值创建一个侧边栏。我认为应该这样做,但它只会为数组中的最后一个值创建一个侧栏:$widget_areas = array(\'single\',\'index\',\'category\',\'archive\',\'tag\',\'taxonomy\',\'author\',\'date\',\'search\',\'attachment\'); foreach($widget_areas as $area