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(
)); ?>
 <?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')) ?>
  <div class="text">
          <?php comment_text() ?>

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

            <!-- Related Posts-->

                            $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;

                                    'tag__in' => $tag_ids,
                                    'post__not_in' => array($post->ID),
                                    'showposts'=>4, // Number of related posts that will be shown.
                                $my_query = new wp_query($args);
                                if( $my_query->have_posts() ) {
                                    while ($my_query->have_posts()) {
<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>
                                    echo '';
                            $post = $backup;

                            <!-- end Related Posts-->

      <!-- 10-15 -->

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

Thanks a lot in advance.

  • I really appreaciate any help here, thanks so much! :)
    – japanworm
    Commented Oct 14, 2011 at 5:28

1 Answer 1


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.

  • Thank you very much! :) For the comments I don't want the "h2"-headline "Pingbacks" to show up :)
    – japanworm
    Commented Sep 30, 2011 at 16:43
  • 1
    Ah ok, you'll need to wrap them in the mytheme_comments function. I've updated my answer explaining where. Commented Sep 30, 2011 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
    Commented Oct 1, 2011 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
    Commented Oct 13, 2011 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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