Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Although I've read several times through the Wordpress Codex, I have difficulties to do what I want. Actually this is for certain parts of my blog, so I guess once I figure out how to do it I can make it work for all of them.

For example: If there are no tags, the CSS coding is still shown (background, image etc.) which looks weird. Same for Recent Posts and Pingbacks.

My setup for the them looks like this:

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


    <ul class="pingbacks"><h2>Pingbacks</h2>
<?php wp_list_comments(array(
  'callback'=>'mytheme_comment',
  'type'=>'pings',
)); ?>
    </ul>
 <?php else : // this is displayed if there are no comments so far ?>

    <?php if ('open' == $post->comment_status) : ?>
        <!-- If comments are open, but there are no comments. -->

     <?php else : // comments are closed ?>
        <!-- If comments are closed. -->
        <p class="nocomments">Comments are closed.</p>

    <?php endif; ?>
<?php endif; ?>

And the function.php code for the comments:

function mytheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class('clearfix'); ?> id="li-comment-<?php comment_ID() ?>">
     <?php echo get_avatar($comment,$size='63'); ?>
     <div id="comment-<?php comment_ID(); ?>">
      <div class="comment-meta commentmetadata clearfix">
        <?php printf(__('<strong>%s</strong>'), get_comment_author_link()) ?><?php edit_comment_link(__('<img src="http://www.zoomingjapan.com/wp-content/themes/alltuts/images/edit.gif">'),'  ','') ?> <span><?php printf(__('%1$s @ %2$s'), get_comment_date('Y/n/j'),  get_comment_time('G:i')) ?>
      </span>
  <div class="text">
          <?php comment_text() ?>
      </div>
      </div>



      <?php if ($comment->comment_approved == '0') : ?>
         <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
         <br />
      <?php endif; ?>

      <div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>
     </div>
<?php }

The page link is only shown when there's actually more than 1 page, so why doesn't that work for the tags as well?

Related Posts:

<h2>Related Posts</h2>                      


<!-- "previous page" action -->
<a class="prev browse left"></a>

<!-- root element for scrollable -->
<div class="scrollable" id=chained>   

   <!-- root element for the items -->
   <div class="items">


      <!-- 1-5 -->
      <div>

            <!-- Related Posts-->

                            <?php 
                            $backup = $post;
                            $tags = wp_get_post_tags($post->ID);
                            if ($tags) {
                                $tag_ids = array();
                                foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

                                $args=array(
                                    'tag__in' => $tag_ids,
                                    'post__not_in' => array($post->ID),
                                    'showposts'=>4, // Number of related posts that will be shown.
                                    'caller_get_posts'=>1
                                );
                                $my_query = new wp_query($args);
                                if( $my_query->have_posts() ) {
                                    while ($my_query->have_posts()) {
                                        $my_query->the_post();
                                    ?>
<div class="relatedPosts"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail(array(120,80)); ?></a>
<div class="relatedPosts_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div></div>
                                    <?php
                                    }
                                    echo '';
                                }
                            }
                            $post = $backup;
                            wp_reset_query();
                             ?>

                            <!-- end Related Posts-->
      </div>

      <!-- 10-15 -->
      <div>

How do I get them to display everything (including CSS) only if there actually is content to show?

Thanks a lot in advance.

share|improve this question
    
I really appreaciate any help here, thanks so much! :) –  japanworm Oct 14 '11 at 5:28
add comment

1 Answer

up vote 2 down vote accepted

For the pingbacks ul & h2 you'll have to move it inside your mytheme_comments function, which should be in your themes functions.php file.

The tags are a bit easier, you just need to them inside the php:

<?php the_tags('<div class="postTags">',' ','<br /></div>'); ?>

So it won't show an empty div when there aren't any tags.

share|improve this answer
    
Thank you very much! :) For the comments I don't want the "h2"-headline "Pingbacks" to show up :) –  japanworm Sep 30 '11 at 16:43
1  
Ah ok, you'll need to wrap them in the mytheme_comments function. I've updated my answer explaining where. –  Jono Warren Sep 30 '11 at 19:27
    
Thank you so much it worked great for the tags. I'm unsure where exactly I should put it into my function.php's mytheme_comments, so I copied that part into my updated question. Could you have a look at it? Also, I posted my "Related Posts" code. It's more complicated than I thought as it's not only a headline, but I'm also using scrollable as container and I don't want any of it to show up if there are no related posts (which is the same as no tags as they're based on post tags). I'd be great if you could help me out a bit, then I'm done anyways. Thanks a bunch! :) –  japanworm Oct 1 '11 at 10:25
    
It's been almost 2 weeks now and I still haven't figured out how to do it. It would great if you could help me out a little bit more, please! :) –  japanworm Oct 13 '11 at 12:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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