• Hi, the following function is returning only the category parent when the category child is designated in the post, but if you designate the category parent it ends up blank. How can I also acquire the parent category when the parent category is designated in the post?

    function getCatButton($postID) {
        $postCat = get_the_category($postID);
        $postHTML = '<div class="button">';
        if ($postCat[0]->parent != 0) {
            $cat = get_category($postCat[0]->parent);
            $postHTML = '<a href="'get_category_link($cat->cat_ID)'">'$cat->name'</a>';
        }
        $postHTML = '</div>';
        return $postHTML;
    }
    • This topic was modified 3 years, 5 months ago by wyclef. Reason: Clarifying
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Insert this just before the if () statement:
    $postHTML = '<a href="'. get_category_link($postCat[0]->cat_ID) . '">'. $postCat[0]->name .'</a>';
    This links to the first assigned category regardless. If there is an ancestor category, this will be overwritten by the ancestor’s link. If no ancestor, the assigned category link remains.

    The original $postHTML assignment in your code has syntax errors. Insert concatenation (dot) operators as I’ve done with the add-in line above.

    Thread Starter wyclef

    (@wyclef)

    Just want to be clear this is what you are thinking. I tried it out and it seems to get the category of the post and not the parent category, which can be the parent category if the post has designated that, but if the post has designated a child category of the parent category it will pull the child category.

    function getCatButton($postID) {
        $postCat = get_the_category($postID);
        $postHTML = '<div class="button">';
        $postHTML = '<a href="'. get_category_link($postCat[0]->cat_ID) . '">'. $postCat[0]->name .'</a>';
        if ($postCat[0]->parent != 0) {
            $cat = get_category($postCat[0]->parent);
        }
        $postHTML = '</div>';
        return $postHTML;
    }
    • This reply was modified 3 years, 4 months ago by wyclef.
    • This reply was modified 3 years, 4 months ago by wyclef.
    Thread Starter wyclef

    (@wyclef)

    Ok, I think thanks to your help I have this working now. Do you think there is a way to clean this up at all or if it works just leave it. Needed an else at the end with your line rather than before the if. Does this need an endif; or is that only if I am using an elseif?

    $postCategories = get_the_category($postID);
        $postHTML .= '<div class="buttons">';
        if ($postCategories[0]->parent != 0) {
            $cat = get_category($postCategories[0]->parent);
            $postHTML .= '<a href="'.get_category_link($cat->cat_ID).'" class="btn" title="'.$cat->name.'">'.$cat->name.'</a>';
        }
        else {
            $postHTML .= '<a href="'.get_category_link($postCategories[0]->cat_ID).'" class="btn" title="'.$postCategories[0]->name.'">'.$postCategories[0]->name.'</a>';
        }
        $postHTML .= '</div>';
    
        return $postHTML;
    • This reply was modified 3 years, 4 months ago by wyclef.
    Moderator bcworkz

    (@bcworkz)

    endif isn’t used when you demarcate with curly braces. It’s for this form;

    if ( $truthy ):
      //do something
    else:
      // do the other
    endif;

    This form doesn’t seem significantly different, but it works well when there is a lot of code or HTML between the logic elements, where visually matching curly braces is difficult.

    You don’t need elseif unless there’s a secondary condition that needs to be checked, like what to do if the category were neither a parent nor a child. (No such thing in reality, but as a hypothetical example.) If you find the need for a number of consecutive elseifs for one logic structure, it may make more sense to use a switch/case structure.
    https://www.php.net/manual/en/control-structures.switch.php

    What you have is fine if it works, but if you want to clean it up a bit, you could remove the redundant portions from the logic structure. Let the if/else only handle what’s truly different and place the common elements outside. Something along the lines of:

    $output = "The quick ";
    if ( $truthy ) {
      $ouptut .= "brown";
    } else {
      $output .= "black";
    }
    $output .= " fox jumps over.";

    A little more clarity of purpose this way, but meaningful gains in efficiency are negligible.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get category parent also’ is closed to new replies.