Forums

[resolved] Excluding a category (5 posts)

  1. aldodager
    Member
    Posted 2 years ago #

    hi to everyone

    im running a digital magazine, and above the post title im adding a line for the reader to know on which category is that post.. right now it looks this way:

    Featured, Fitness
    POST TITLE

    I would like to avoid showing FEATURED or MAIN ARTICLE in that line. So far I'm using:

    <?php $category = get_the_category();
    echo $category[1]->cat_name;
    ?>

    But i dont think that this code is what I need. Also, I don't know how to link the Category name to that specific category. So finally I would only like to show:

    Fitness (linked to fitness category)
    POST TITLE

    instead of

    Featured (linked to this cat), Fitness (linked to this cat)
    POST TITLE

    Probably this has been resolved before but im not good at tweaking wordpress codes

    Thank you very much!

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    An example of excluding one (or more) categories displaying:

    <?php
    foreach((get_the_category()) as $category) {
        if (!in_array($category->cat_name,array('cat1_to_exclude','cat2_to_exclude','cat3_to_exclude')) ) {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
    }
    }
    ?>
  3. aldodager
    Member
    Posted 2 years ago #

    the code worked great!!!
    thank you very much!

  4. ronvil
    Member
    Posted 1 year ago #

    Sorry, but i'm just beginning to customize wordpress themes. how can i edit

    <?php
    foreach((get_the_category()) as $category) {
        if (!in_array($category->cat_name,array('cat1_to_exclude','cat2_to_exclude','cat3_to_exclude')) ) {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
    }
    }
    ?>

    to exlude the categories that i do not want to appear? Thank you in advance!

  5. ronvil
    Member
    Posted 1 year ago #

    Sorry for posting again. i would like to clarify my question above.

    for example, i have the following categories

    1. Category 1
    1. Category 1.1
    2. Category 1.2
    3. Category 2

    Then, under the post title, i want to put a link to the category archive for "Category 1.1" ONLY and not have the other categories of the same post appear. How do i do this? thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic