Support » Fixing WordPress » conditional statement for sidebar element

  • Resolved jrcollins


    I added some code to my sidebar to show links to posts in the same category as the current post. I placed the code inside a div element to give the code’s output the proper formatting.
    My question is, how can I stop that div element from displaying on the home page?
    The code’s output is set to display only on single post pages but the empty div element (with borders) still displays on the home page.
    I have included the code below to give you a better idea of what I’m talking about.

    <div class="widget"><?php
    if ( is_single() ) {
      $cats = wp_get_post_categories($post->ID);
      if ($cats) {
        $first_cat = $cats[0];
          'cat' => $first_cat,
          'post__not_in' => array($post->ID),
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'Related Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a>" rel="bookmark" title="Permanent Link to <?php 
    the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        } //if ($my_query)
      } //if ($cats)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘conditional statement for sidebar element’ is closed to new replies.