WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] conditional statement for sidebar element (5 posts)

  1. jrcollins
    Member
    Posted 1 year ago #

    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];
        $args=array(
          'cat' => $first_cat,
          'post__not_in' => array($post->ID),
          'showposts'=>5,
          'caller_get_posts'=>1
        );
        $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>
           <?php
          endwhile;
        } //if ($my_query)
      } //if ($cats)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?></div>
  2. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    the conditional statement for that is is_home
    http://codex.wordpress.org/Function_Reference/is_home

  3. jrcollins
    Member
    Posted 1 year ago #

    The problem is not with the code itself. As you can see, I've placed it inside a div element to give the output the proper formatting. However, I don't want that div to display on the home page. I was thinking of something along the lines of:

    <?php if (!is_home()) {
    echo '<div class="widget"><?php
    if ( is_single() ) {
    //the rest of the code here//
    </div>'; ?>

    ...which I tried but it just wiped out my sidebar.

  4. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    try:
    http://codex.wordpress.org/Function_Reference/is_front_page

    Actually combine the conditionals into one statement

  5. jrcollins
    Member
    Posted 1 year ago #

    Thanks for the link. I just tried the following code which seems to work:

    <?php if (!is_home()): ?>
    <div class="widget"><?php
    if ( is_single() ) {
    //the rest of the code here//
    </div>
    <?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.