按类别自定义搜索结果

时间:2014-09-01 10:34:47

标签: php wordpress

我有不同的类别,其中显示的帖子由不同的元素表征,因此当我在wordpress中使用搜索时,结果不能很好地显示。 出于这个原因,我想根据其类别更改搜索结果中显示的帖子的外观。 例: What I have now and what I would like to have. Image example.

我的search.php模板如下所示:

   <?php get_header(); ?>

    <div id="content">
        <div class="padder">

        <?php do_action( 'bp_before_blog_search' ); ?>

        <div class="page" id="blog-search" role="main">

            <?php if (have_posts()) : ?>


                <?php bp_dtheme_content_nav( 'nav-above' ); ?>

                <?php while (have_posts()) : the_post(); ?>

                    <?php do_action( 'bp_before_blog_post' ); ?>
<div class="blog-post">
// Here is displayed the blog post style and features
</div><!--blog-post-->
<div class="clear"> </div>

                    <?php do_action( 'bp_after_blog_post' ); ?>

                <?php endwhile; ?>

                <?php bp_dtheme_content_nav( 'nav-below' ); ?>

            <?php else : ?>

                <h2 class="center"><?php _e( 'No posts found. Try a different search?', 'OneCommunity' ); ?></h2>

            <?php endif; ?>
<div style="display:inline">
<center><?php wp_pagenavi(); ?></center>
</div>

        </div>

        <?php do_action( 'bp_after_blog_search' ); ?>

        </div><!-- .padder -->
    </div><!-- #content -->


<div id="sidebar">
    <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-blog')) : ?><?php endif; ?>
    <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-ad-blog')) : ?><?php endif; ?>
</div><!--sidebar ends-->

<?php get_footer(); ?>

我试图根据帖子类别显示一个不同的&#34;模板&#34;这个职位但没有成功。 我在wordpress.stackexchange.com上问了同样的问题,但没有成功,我指出了一个不起作用的解决方案,希望它可以帮助你开发我需要的代码。 https://wordpress.stackexchange.com/questions/141856/different-layout-on-search-page-depending-on-category-post

非常感谢你给予的任何帮助,请原谅我的英语。

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您只需使用post_class()功能:

<div <?php post_class( 'blog-post' );?> >
    // Here is displayed the blog post style and features
</div><!--blog-post-->

这将呈现如下:

<div class="post-3654 post type-post status-publish format-standard hentry category-buildings blog-post">
    // Here is displayed the blog post style and features
</div><!--blog-post-->

适用于buildings类别中的帖子。

然后你的CSS会处理其余的事情:

.post.category-buildings {
    border: 3px solid red;
}

但我认为您的设置/构思仅适用于单个类别帖子。

参考:来自Codex

  

post_class可能包含以下一个或多个值   class属性,取决于网页浏览。

     
      
  • .post-id
  •   
  • .post
  •   
  • .attachment
  •   
  • .sticky
  •   
  • .hentry(hAtom微格式页面)
  •   
  • .category-ID
  •   
  • .category-name
  •   
  • .tag-name
  •   
  • .format名
  •