dongshi8425 2015-09-06 08:34
浏览 27
已采纳

将搜索结果设置为仅显示帖子

I'm using a custom Wordpress theme. When I try a search on my website, it redirects me to the search.php. The problem is that it shows not only the posts, but also the pages and the events from the plugin "The Events Calendar". Which is bad because they don't align correctly and appear one onto another. I think edit it to show everything correctly would be harder.

So I would like my search results to show only the posts. I'm not good in PHP but I think this is the part of code that need to be changed.

<div class="content<?php if($bpxl_goblog_options['bpxl_layout'] == 'c_layout' || $bpxl_goblog_options['bpxl_layout'] == 'gs_layout' || $bpxl_goblog_options['bpxl_layout'] == 'sg_layout' || $bpxl_goblog_options['bpxl_layout'] == 'g_layout') { echo ' masonry masonry-home'; } ?>">
        <?php
            if (have_posts()) : while (have_posts()) : the_post();

            get_template_part( 'content', get_post_format() );
        ?>
        <?php endwhile; else: ?>
            <div class="post">
                <div class="single-page-content error-page-content">
                    <p><strong><?php _e('Rien à afficher'); ?></strong></p>
                    <?php get_search_form(); ?>
                </div><!--noResults-->
            </div>  
        <?php endif; ?>
    </div><!--content-->

Full search.php code here https://jsfiddle.net/jzx4evy9/

Any ideas on what need to be changed to only show the posts ?

  • 写回答

1条回答 默认 最新

  • dtny30176 2015-09-06 08:41
    关注

    You can use the following code in functions.php to exclude the page result

    function SearchFilter($query) {
    if ($query->is_search) {
    $query->set('post_type', 'post');
    }
    return $query;
    }

    add_filter('pre_get_posts','SearchFilter');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答