0

I would like to display different layouts for post with different categories on the search page:

I tried with this code:

<?php
if(isset($_GET['the_category'])) {
    $type = $_GET['the_category'];
    if($type == '1') {
?>

But it doesn't work, could someone can point out a solution?

Full code:

<?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' ); ?>

      <?php
      if(isset($_GET['the_category'])) {
        $type = $_GET['the_category'];
        if( $type == '1' ) {
      ?>

      <div class="blog-post">
        <div class="post-title">
          <a href="<?php the_permalink() ?>" rel="bookmark"
               title="Fixed link <?php the_title_attribute(); ?>">
            <?php the_title(); ?>
          </a>
        </div><!--post-title-->
        <?php if ( has_post_thumbnail() ) { ?>
        <div class="thumbnail">
          <?php the_post_thumbnail('full'); ?>
        </div>
        <?php
          } else {
            // no thumbnail
          }
        ?>
        <div class="text">
        <?php
        global $more;
        $more = 0;
        the_content( __('Read more','OneCommunity') );
        ?>
        </div><!--text-->
      </div><!--blog-post-->
      <div class="clear"></div>

      <?php    
        } elseif( $type == '4' ) {
      ?>

      //Display layout for post marked with category 4
      <?php
        }
      }
      ?>

      <?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>
  <?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(); ?>
  • Do you have the_category in URL? It isn't native WordPress query variable, how exactly are you faceting search by categories? – Rarst Apr 20 '14 at 11:58
  • I don't understand your question, i want to display a different post layout in the search results based on category. I am using the standard wordpress search. – Benjamin Apr 20 '14 at 15:30
0

You can use:

if ( in_category(4) ) 

in order to add different HTML for a post in category 4. This has to be in within the loop for it to work.

  • <?php if ( in_category(4) ) ) { ?> // Do something <?php } else { // do something else} ?> This should be the code you are talking about? I am unable to figure out a solution. Could you please implement with more info your answer? – Benjamin Apr 20 '14 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.