• Resolved Krystof

    (@krystof-stulc)


    Hi,
    I wanted to implement a shortcode that would list all child categories assigned to a post. The name of parent category is passed as a shortcode parameter. I wrote a function in functions.php of the child theme and registered the shortcode. So far so good it works when I use the shortcode alone.
    However, when I try to write the shortcode in a text, something like:

    “The project is in these phases: [cats1 type=”project-phase”]”

    ¨Then the categories are always displayed first and then all the text.

    Is there a way how to place the category listing in a correct position?

    Here is the code I have:

    function cats1($type){
        extract(shortcode_atts(array(
            'type' => 'type'
        ), $type));
      $category = get_category_by_slug($type);
      $catid = $category->term_id;
      /*return $catid;*/
        $args = array(
    	'orderby'            => 'name',
    	'order'              => 'ASC',
            'style'              => 'none',
    	'show_count'         => 0,
    	'hide_empty'         => 1,
    	'use_desc_for_title' => 0,
    	'child_of'           => $catid,
    	 );
      $categories = get_categories($args);
       foreach($categories as $category) {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "Look at all %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> | ';
        }
    }
    add_shortcode('cats1', 'cats1');
Viewing 1 replies (of 1 total)
  • Thread Starter Krystof

    (@krystof-stulc)

    I solved it by changing echo to return…

    function cats1($type){
        extract(shortcode_atts(array(
            'type' => 'type'
        ), $type));
      $category = get_category_by_slug($type);
      $catid = $category->term_id;
      /*return $catid;*/
        $args = array(
    	'orderby'            => 'name',
    	'order'              => 'ASC',
      'style'              => 'none',
    	'show_count'         => 0,
    	'hide_empty'         => 1,
    	'use_desc_for_title' => 0,
    	'child_of'           => $catid,
    	 );
      $categories = get_categories($args);
       foreach($categories as $category) {
        /*echo '| <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "Podívejte se na v?echny %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';*/
        $str .='| <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "Podívejte se na v?echny %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
        /*echo '<p> Description:'. $category->description . '</p>';*/
        /*echo $category->count;*/
        }
        /*echo '<br>';*/
        return $str;
    }
    add_shortcode('cats1', 'cats1');
Viewing 1 replies (of 1 total)
  • The topic ‘display category shortcode in a correct possition’ is closed to new replies.