Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use a shortcode handler in my plugin. The shortcode can have different (optional) parameters. And it should be possible to use for example 2 shortcodes on a static WordPress page with text before, between and after the shortcodes.

An example could be:

    Lorem ipsum dolor sit amet, consetetur sadipscing elitr

        <table>
        <tr>
        <td>[shortcode option="1"]</td>
        <td>[shortcode option="2"]</td>
        </tr>
        </table>

    invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
    Sticky Posts:
    [shortcode option="3"]
m et justo duo dolores et

My coding for the shortcode handler is this:

function show_posts_handler( $atts, $content=null, $code="" ) {
    //code 4 displaying 
        extract( shortcode_atts( array(
            'sticky' => 'false',
            'latest' => 'false',
            'missions' => 'false',
            'count' => '0',
        ), $atts ) );

        if($sticky != 'false')
        {
            show_sticky_posts();
        } else if($latest != 'false'){
            show_lastest_posts($count);
        }else if($missions != 'false'){
            show_lastest_missions($count);
        }else{
            show_all_posts_from_categories();
        }
}

add_shortcode( 'show_posts', 'show_posts_handler' );

The shortcodes should be used in the WYSIWYG editor of pages and not programatically in the template. The problem is "The shortcodes are always displayed before the text".

share|improve this question
    
Did you try your example usage? If so, what was wrong with it? What unexpected behavior did you get? –  chrisguitarguy Jan 7 '13 at 13:21
3  
Nice story, what is the question? –  Eugene Manuilov Jan 7 '13 at 13:25
    
sorry for the confusion ... I had highlighted my question now. Hope that it is a bit clearer now. –  mybecks Jan 7 '13 at 14:22

1 Answer 1

up vote 6 down vote accepted

Without seeing your show_sticky_posts, show_lastest_posts, show_lastest_missions,show_all_posts_from_categories i can guess that the shortcode content is always before the post content beacuse this functions are echoing/outputing the result and a shortcode should return content instead so you can try using output buffer of PHP like this:

function show_posts_handler( $atts, $content=null, $code="" ) {
    //code 4 displaying 
    extract( shortcode_atts( array(
        'sticky' => 'false',
        'latest' => 'false',
        'missions' => 'false',
        'count' => '0',
    ), $atts ) );
    ob_start();
    if($sticky != 'false'){
        show_sticky_posts();
    } else if($latest != 'false'){
        show_lastest_posts($count);
    }else if($missions != 'false'){
        show_lastest_missions($count);
    }else{
        show_all_posts_from_categories();
    }
    $output_string = ob_get_contents();
    ob_end_clean();
    return $output_string;
}

add_shortcode( 'show_posts', 'show_posts_handler' );
share|improve this answer
    
the different option write uses for displaying: get_template_part( 'content-list', get_post_format() ); –  mybecks Jan 7 '13 at 14:20
    
thanks for your answer. I solved my problem. –  mybecks Jan 7 '13 at 14:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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