• Resolved nimaha

    (@nimaha)


    I’m working from the twenty ten theme and have this code to return the date if the post belongs to a category and return nothing if it doesn’t (in this case, if it’s a custom post type, it won’t return the date).

    <?php if ( in_category('') ) : ?>
       <div class="entry-meta">
    	<?php twentyten_posted_on(); ?>
       </div><!-- .entry-meta -->
    <?php else: ?>       
    
    <?php endif ?>

    It works fine if I enter the name of a given category, but I don’t want to have to manage this code every time I add a new category. Is there something I can add that globally accepts all categories? Haven’t been able to find that answer anywhere.

Viewing 4 replies - 1 through 4 (of 4 total)
  • So are you saying you want to display that date if the post has ANY category assigned?

    I didn’t test but try:

    <?php
    // in post loop, if post has terms belonging to the taxonomy 'category', display a message
    $terms = wp_get_post_terms( $post->ID , 'category', '');
    if ( !is_wp_error($terms) && $terms ) {
    echo 'found featured terms on this post';
    }
    ?>

    EDIT: Too slow! Should have refreshed the screen!

    Here is a query that should work:

    <?php $sql = "SELECT tr.object_id FROM $wpdb->term_relationships tr
    JOIN $wpdb->term_taxonomy tt
       ON (tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = 'category')
    WHERE tr.object_id = $post->ID";
    $my_query = $wpdb->get_results($sql);
    if ( $my_query ) : ?>
       <div class="entry-meta">
    	<?php twentyten_posted_on(); ?>
       </div><!-- .entry-meta -->
    <?php else: ?>       
    
    <?php endif ?>
    Thread Starter nimaha

    (@nimaha)

    Perfection. I gave the second suggestion a go and it worked right away.

    Thanks to both of you!

    You are welcome!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using in_category to to show or hide data’ is closed to new replies.