• I’m currently grabbing the first category for a post by the following method:

    <?php
    $category = get_the_category();
    if($category[0]){
    echo '<a class="entry-category" href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
    }
    ?>

    This is working perfectly, BUT we want to exclude a category and its children from display.

    First thought was to exclude by an array of IDs, but get_the_category doesn’t allow that.

    Second thought was to change the category IDs, so that the “first” category the code came up with would be the one we wanted. Would take a bit of fudging, but would do the job. However, get_the_category returns an alphabetised list rather than by cat ID.

    Would love to know how one can return:
    – only one category.
    – excluding some categories (by either exclusion method above).
    – linked up and formatted as above.

  • The topic ‘get_the_category, limited and by ID’ is closed to new replies.