Support » Fixing WordPress » get_category, not showing just the NAME of a category AND using comma division

  • Hi,
    I run a webmagazine and I have two columns in the home page, one for featured articles and one for the other ones.
    Often one article is in more than one category, and I could simply use
    <?php the_category(', ') ?> to display them and divide them with a comma.
    BUT I use a category to divide featured and not featured articles easily in the loop, but i want the name (“Feature”) hidden in the when using <?php the_category(', ') ?>. This is because I want to use this category to divide the articles, but I do not want it to be of public domain.
    For this reason I use the following script:

    <?php foreach((get_the_category()) as $cat) {
    if (!($cat->cat_name=='Feature')) echo $cat->cat_name . ' ';} ?>

    Only problem, no comma between the categories that have to be shown, if I have more than one (eg. “Politics” and “Economical Crisis”).
    I changed it to this:

    <?php foreach((get_the_category()) as $cat) {
    if (!($cat->cat_name=='Fiori')) echo $cat->cat_name . ', ';} ?>

    addid the comma into the brackets.. BUT then it displays the comma even when the category to be displayed is only one (eg. an article only under “Politics”)

    What kind of code should I use then, to be able to filter out just the name of the “feature” category, keeping a comma as a divider between the other categories?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You might consider assigning the category names to one variable, then after retrieving all categories, strip the comma off the end of the string.
    $cats .= $cat->cat_name . ', ';

    then when you gathered all the categories into one string;
    echo rtrim($cats,',');

    uhm i’m not sure i understand what you mean. I’m not expert in php, i was thinking about something that uses the get_category(). Something like:
    – get the categories of the post
    – loop of categories to display, and if one of the categories is “Feature” do not display it (something like the code “if post has the id=35, then ‘continue'” to skip the visualization
    – all this, ensuring that if a post, apart the “Feature” category, has as well 2 or more other categories, it will show a comma in between… but not after the last one! (problem i have now)
    Is anybody able to help this php-newbie with the effective code? 🙂

    foreach($post_cats as $cat) {
    if ( $cat->cat_name !='Featured') {  //could use $cat->cat_ID != '1'
    $list_of_cats .= $cat->cat_name . ', '; // put comma and space between categories
    } //foreach
    if ($list_of_cats) {
    echo rtrim($list_of_cats,', '); //strip off last comma/space

    thank you so much Michael!
    Just to be picky and profit of your kindness 🙂 is there a way to make the name of the category become a link to the category page again, like in the get_category function?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get_category, not showing just the NAME of a category AND using comma division’ is closed to new replies.