Support » Plugins » shortcode to show child categories from certain parent category with links

  • Resolved aljddv

    (@aljddv)


    I’ve been wanting to shortcode an output to show a certain post’s sub category of a certain parent category. But I can’t seem to output a link to it. Here’s my current code >>

    function genre($atts) {
    $sep = ”;
    $output = ”;
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(35, $childcat)) {

    $output .= $sep . $childcat->cat_name;
    $sep = ‘, ‘;
    }
    }
    return $output;
    }
    add_shortcode(‘genre’, ‘genre’);

    it returns the list i want but with no link, any help would be really appreciated. 🙂

Viewing 1 replies (of 1 total)
  • Thread Starter aljddv

    (@aljddv)

    after hours of research got it to actually work.. here’s the code in case it’ll be of use to anyway. or not.. whatever.. cheers! >>

    function genre($atts) {
    $sep = ”;
    $output = ”;

    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(5, $childcat)) {

    $url = get_category_link($childcat->cat_ID);
    $output .= $sep . ” < a href= ‘$ url’ >$childcat->cat_name”;
    $sep = ‘, ‘;

    }
    }
    return $output;
    }
    add_shortcode(‘genre’, ‘genre’);

    YOU’RE WELCOME!

Viewing 1 replies (of 1 total)
  • The topic ‘shortcode to show child categories from certain parent category with links’ is closed to new replies.