Support » Fixing WordPress » New to writing Shortcodes

  • Resolved CallMeAndy

    (@callmeandy)


    Hi dont know if anyone can put me right – a probably a newbie stupid error but…
    writing a shortcode to list child categories based on the name of the page which implements a category and has the same name.
    In this case the Page name is Destinations which is also the name of a top level category.

    function AB_handler_get_child_cats($atts){
    
        $category_name=get_the_title();
        $cat = get_cat_ID($category_name);
    
        if( $cat > 0 ){
    
            extract(shortcode_atts(array(
                        'show_count' => "0",
                        'orderby' =>"name",
                    ), $atts));
    
            $this_category = get_category($cat);
            if (get_category_children($this_category->cat_ID) != "") {
    
                $html = "<div><ul>";
                wp_list_categories("orderby=$orderby&show_count=$show_count&title_li=
                    &use_desc_for_title=1&child_of=".$this_category->cat_ID);
                $html .= "</ul></div>";
            }
            return $html;
        }
    }
    
    add_shortcode('childcats','AB_handler_get_child_cats');

    If I put the following [childcats] or [childcats show_count=1 orderby=’name’] in a text widget titled “Destinations” the output is being rendered prior to the start of the widget border (outside the border).

    What am I doing wrong?

Viewing 6 replies - 1 through 6 (of 6 total)
  • It looks like you are missing the

    echo=0

    parameter in wp_list_categories.

    Michael

    (@alchymyth)

    Forum Moderator

    example:

    $html = "<div><ul>";
     $html .= wp_list_categories("echo=0&orderby=$orderby&show_count=$show_count&title_li=&use_desc_for_title=1&child_of=".$this_category->cat_ID);
    $html .= "</ul></div>";

    Thanks for coming back to me guys.
    However including echo=0 now, as would seem intuitive, is not echoing anything. Its now not rendering any list at all. Previously it was the correct output just in the wrong place.

    Michael

    (@alchymyth)

    Forum Moderator

    have you read my reply with the suggested changes after adding the ‘echo=0’ ?

    Aha Yes I did read it but clearly I missed the $html assignment. Much appreciated your spot on.

    AS

    Resolved

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘New to writing Shortcodes’ is closed to new replies.