WordPress.org

Forums

[resolved] New to writing Shortcodes (7 posts)

  1. CallMeAndy
    Member
    Posted 3 years ago #

    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?

  2. Birgir Erlendsson (birgire)
    Member
    Posted 3 years ago #

    It looks like you are missing the

    echo=0

    parameter in wp_list_categories.

  3. Michael
    Part-Time Forum Moderator
    Posted 3 years ago #

    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>";
  4. CallMeAndy
    Member
    Posted 3 years ago #

    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.

  5. Michael
    Part-Time Forum Moderator
    Posted 3 years ago #

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

  6. CallMeAndy
    Member
    Posted 3 years ago #

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

    AS

  7. CallMeAndy
    Member
    Posted 3 years ago #

    Resolved

Topic Closed

This topic has been closed to new replies.

About this Topic