• Resolved Roy

    (@gangleri)


    Is it possible to not display “category” or “tag” when there is none? So not “filed under category —-white space—-“, but just no “filed under…” at all.

Viewing 15 replies - 1 through 15 (of 16 total)
  • Moderator James Huff

    (@macmanx)

    Volunteer Moderator

    Is it even possible to display a post without a category? I though the default category was always added automatically if no categories were selected.

    For the tags, try this:

    <?php if (function_exists('the_tags')) { ?> <?php the_tags('Tags:', ', ', '<br />'); ?> <?php } ?>

    Thread Starter Roy

    (@gangleri)

    He James. Good question, easy answer πŸ™‚
    I use custom post types which (indeed) get no category, but however I display them with a ‘special index’ (a page) they do use the default single.php and archive.php, so they get empty categories there. Could I use this “if” statement in this?
    <?php _e('Category:') ?> <?php the_category(', ') ?>

    <?php if (function_exists(_e('Category:'))) ?> <?php the_category(', ') ?>
    ?

    Moderator James Huff

    (@macmanx)

    Volunteer Moderator

    I suppose so, or perhaps:

    <?php if (function_exists('the_category')) { ?> <?php the_category(', ') ?> <?php } ?>

    Thread Starter Roy

    (@gangleri)

    <?php if (function_exists()) _e('Category:') ?> <?php the_category(', ') ?>

    Seems to do the job too! Trial-and-error πŸ™‚ That other code I came up with didn’t work.

    Moderator James Huff

    (@macmanx)

    Volunteer Moderator

    Yay, nice work!

    Thread Starter Roy

    (@gangleri)

    Hm, not entirely, the 'Category:' part doesn’t work, now only the categories are shown, it doesn’t say “category:” anymore. When I remove the () the “category:” is back, but the categories are not excluded when empty.

    Moderator James Huff

    (@macmanx)

    Volunteer Moderator

    I’ve adapted this from Twenty Ten, no clue if it will work with your setup though:

    <?php if ( count( get_the_category() ) ) : ?> <?php printf( __( 'Category:' ), get_the_category_list( ', ' ) ); ?> <?php endif; ?>

    Use..

    <?php echo get_the_term_list( $post->ID, 'taxonomy-slug-goes-here', 'Text to show: ', ', ', '' ); ?>

    It’ll be empty if there are no terms set for the post..

    Blank when no terms, text + terms when there are…

    Thread Starter Roy

    (@gangleri)

    James, your code hides the category itself in all occasions, and the term “category” when the category is empty πŸ™‚

    Mark, I’m actually trying to hide the default WP category when it is empty, not a custom taxonomy. The reason for this is that I have custom post types which I display by means of a page that I can code any way I like, but as soon as for example a custom taxonomy is clicked or a custom post type viewed in single view, WP uses my theme’s default achives.php and single.php and these (of course) display the category, but my custom post types have none, so it says “category: –empty–. I would like to have that entire category line hidden when there is no category. It could be relatively simple. Here’s the normal code of my theme:
    <?php _e('Category:') ?> <?php the_category(', ') ?>
    When I edit this to:
    <?php if (function_exists()) _e('Category:') ?> <?php the_category(', ') ?>
    It works almost perfectly. There are category links when a post is shown that has categories and the line is gone when a custom post type is displayed which has no categories. The only problem is that the categories are no longer displayed as “category: –category name– but just as “–category name–“. For some reason the 'Category:' part is never used.
    It’s no matter of live and death btw, but I just hoped it would be as easy to do as hide tags when there are none.

    But my custom post types are slowly getting pretty cool. Check this out and scroll all the way down to the “on duty” quote of Julius Evola. It says:
    “more quotes of Evola
    reviews of books of Julius Evola”
    where “Evola” is a custom post type and “Julius Evola” a WP tag linking to all books of the man! The other way around (a custom post type accompanying the normal posts, the book reviews) did not entirely work, since the tag and custom taxonomy archives are then somehow thrown together.

    But it is getting better and better.

    The code supplied can use any taxonomy and does exactly what you’re asking for, did you try the code provided?

    Thread Starter Roy

    (@gangleri)

    I didn’t immediately think of the obvious (taxonomy=category). But that codes displays the same as the original code, the word “category” is displayed whether there is one or not.

    Thread Starter Roy

    (@gangleri)

    Sorry, I’ve been behind the computer too long. I have been editing the wrong file. It works perfectly! Thanks!!

    Mark, maybe you can help me find out?

    I have this code in buffet framework theme:

    $postfooter = '<div class="break"></div>';
    
    	if ( !is_attachment() ) {
    		$postfooter .= '<ul class="entry-links clearfix">';
    		$postfooter .= __('
    
    <li><span class="entry-tags">Category: ', 'buffet') . get_the_term_list( $post->ID, 'category', '', ', ', '' ) . '</span></li>
    ';
    		$postfooter .= __('
    
    <li><span class="entry-tags">Tags:', 'buffet') . get_the_tag_list(' ', ', ', ' ') . '</span></li>
    ';
    		$postfooter .= '
    
    <li><a href="' . get_comments_link() . '">' . __('Comments', 'buffet') . ' ' . get_comments_number() . '</a></li>
    ';
    		$postfooter .= __('
    
    <li><span class="entry-tags">Custom Taxonomy: ', 'buffet') . get_the_term_list( $post->ID, 'custom-taxonomy', '', ', ', '' ) . '</span></li>
    ';
    		if ( function_exists('wp_print') ) $postheader .= '
    
    <li>' . print_link('', '', false) . '</li>
    ';
    
    		$postfooter .= '';
    	}
    
    	echo apply_filters('bf_postfooter', $postfooter);
    }

    Where I must put “echo” to hide Custom Taxonomy if it empty?

    Hi,

    See my first response to this thread…. the third parameter of get_the_term_list sets the text to show next to the results.. (if there are any)..

    If you’re not too comfy with the PHP let me know and i’ll make the adjustments for you..

    EDIT: Had some time to spare, so just did it for you anyway.. πŸ˜‰

    // Ignore this comment line, works around forum bug that removes indent on first line
    
    	$postfooter = '<div class="break"></div>';
    
    	if ( !is_attachment() ) {
    		$postfooter .= '<ul class="entry-links clearfix">';
    		$postfooter .= '<li><span class="entry-tags">' . get_the_term_list( $post->ID, 'category', __('Category: ', 'buffet' ), ', ', '' ) . '</span></li>';
    		$postfooter .= '<li><span class="entry-tags">' . get_the_tag_list( __('Tags:', 'buffet'), ', ', ' ') . '</span></li>';
    		$postfooter .= '<li><a href="' . get_comments_link() . '">'. sprintf( __('Comments ', 'buffet'), get_comments_number() ) .'</a></li>';
    		$postfooter .= '<li><span class="entry-tags">' . get_the_term_list( $post->ID, 'custom-taxonomy', __('Custom Taxonomy: ', 'buffet'), ', ', '' ) . '</span></li>';
    		if ( function_exists('wp_print') )
    			$postheader .= '<li>' . print_link('', '', false) . '</li>';
    		$postfooter .= '';
    	}
    
    	echo apply_filters('bf_postfooter', $postfooter);
    }

    Mark, it works perfect! Thank you very much!

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘hide meta when empty’ is closed to new replies.