5

I'm trying to create a shortcode base on the following code:

<ul class="filter">
<li>Filter:</li>
<li class="link-store"><a href="<?php echo get_permalink( 23 ); ?>">All</a></li>
    <?php global $post; $thispage = $post->ID; // grabs the current post id from global and then assigns it to thispage ?>
    <?php $pagekids = get_pages("child_of=".$thispage."&sort_column=menu_order"); // gets a list of page that are sub pages of the current page and assigns then to pagekids ?>

    <?php if ($pagekids) { // if there are any values stored in pagekids and therefore the current page has subpages ?>
        <ul>
            <?php wp_list_pages("depth=1&title_li=&sort_column=menu_order&child_of=".$thispage); // display the sub pages of the current page only ?>
        </ul>
            <?php } elseif($post->post_parent)
               $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); if ($children) { // if there are no sub pages for the current page ?>
        <ul>
            <?php echo $children; ?>
        </ul>
            <?php } ?>
        </ul>

Out of the question, this is a really good resource since it returns the child pages and even brother's pages.

ok back to the game.... I trying to create a short code based on this, however if not working, so far I have this:

function filter_store ( $atts ) {
global $post; $thispage = $post->ID;
if ($pagekids) {
   $output .= '<ul>';
      wp_list_pages("depth=1&title_li=&sort_column=menu_order&child_of=".$thispage);
       $output .= '</ul>';   
            } elseif($post->post_parent) 
           $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); if ($children) {
       $output .= '<ul>';
            echo $children;
       $output .= '</ul>';
            }
        return $output;
    }

   add_shortcode('filter', 'filter_store');

I have spent around 3 hours trying to find where the error is, and my head is about to explode, I would appreciate any kind of help on this.

Thank in advance.

3 Answers 3

6

A shortcode has to return just a string, you should not print something like in wp_list_pages() or echo. From Shortcode API:

The return value of a shortcode handler function is inserted into the post content output in place of the shortcode macro. Remember to use return and not echo - anything that is echoed will be output to the browser, but it won't appear in the correct place on the page.

2
  • any recommendation? Mar 28, 2012 at 3:44
  • Well, yes: Return a string, don’t use echo or wp_list_pages without 'echo' => 0.
    – fuxia
    Mar 28, 2012 at 12:07
2

Your problem is that shortcode content appears before other content instead of where you want? If yes, you can solve this by:

function filter_store()
{
    ob_start();
    ...
    echo $children;
    ...
    $ReturnString = ob_get_contents();
    ob_end_clean();
    return $ReturnString;
}
1
  • 1
    You can combine the last three lines of the function into one statement: return ob_get_clean();
    – shea
    Feb 14, 2019 at 12:53
1

The Output section of https://codex.wordpress.org/Shortcode_API recommends this pattern.

function my_shortcode() {
    ob_start();
    ?> <HTML> <here> ... <?php
    return ob_get_clean();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.