I've been working on this shortcode w/o any success...

function loop_shortcode( $atts = '' ) {

'<div class="clear"></div>
<div class="childs grid_12"> 
 $the_query = new  WP_Query( 
         "post_parent" => "8",
         "post_type" => "page",
         "posts_per_page" => 4,
         "sort_column"   => "menu_order"
 ); ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
   <div id="service-hp">
       <?php the_post_thumbnail("home-thumb") ?>
       <h2 style="margin-bottom:5px"><?php the_title() ?></h2>
       <?php the_excerpt() ?>
       <a class="read-more" href="<?php the_permalink() ?>">en savoir plus <img src="<?php bloginfo( "template_url" ); ?>/images/read-more.png"></a>
   </div> <!--  ends here -->
<?php endwhile; ?>
<?php wp_reset_query() ?>

 function my_loop_shortcode( $atts ) {
     return ob_get_clean(); 

 add_shortcode('my_loop', 'my_loop_shortcode');      

I'm getting no output

I would appreciated your help

Thanks in advance

UPDATE enter image description here

  • You're function doesn't seem to echo or return anything. Also, try using get_posts instead of a new WP_Query. Commented Nov 25, 2011 at 5:17
  • I don't get why to vote down my question... anyway there is always some1 like that around... Commented Nov 25, 2011 at 18:18
  • I didn't down vote it man! Someone who didn't understand it might have. Commented Nov 26, 2011 at 3:50

2 Answers 2


There is a multitude issues wrong with the above. For one, post_parent in the query arguments needs to be an integer. You're assigning it a string. A number of your calls to wordpress functions, such as the_excerpt() and wp_reset_query() are missing ending semicolons. $atts is an associative array of attributes for the shortcode. If you want to use attributes, they need to be extracted in the shortcode function. There is no need to pass them to the looping function, especially in the light of you not having extracted them beforehand. Also, you're not even attempting to use them in it anyway.

Further I don't see why you'd want to separate it into two functions. And I wouldn't include direct markup in functions and use ob_get_clean either, but echo or return the desired result directly. The latter two are more or less personal preferences.

That being said, this will do what you want:

function andrew_loop_shortcode( $atts ) {
    extract( shortcode_atts( array(
        'parent' => 8,
        'type' => 'page',
        'perpage' => 4
    ), $atts ) );
    $output = '<div class="clear"></div><div class="childs grid_12">';
    $args = array(
        'post_parent' => $parent,
        'post_type' => $type,
        'posts_per_page' => $perpage,
        'sort_column'   => 'menu_order'
    $andrew_query = new  WP_Query( $args );
    while ( $andrew_query->have_posts() ) : $andrew_query->the_post();
        $output .= '<div id="service-hp">'.
                   '<h2 style="margin-bottom:5px">'.
                   '<a class="read-more" href="'.
                   '">en savoir plus <img src="'.
                   get_bloginfo( 'template_url' ).
                   '/images/read-more.png"></a></div><!--  ends here -->';
    $output .= '</div>';
    return $output;
add_shortcode('andrewloop', 'andrew_loop_shortcode');

Lines 2 - 6 of the above are not strictly necessary, but add to the functionality of your shortcode.

If you simply use [andrewloop] in a page now, it will display what you are currently aiming at. To only achieve that, you could set the query arguments statically in the shortcode function. With lines 2- 6 however, those are now the default values of the shortcode but they are changeable on the fly without modifying the function again.

With the above, you can now use [andrewloop parent="6" perpage="3"] for instance. The shortcode can thus be used for multiple different queries.

Further reading, in case you care:

  • Hi Johannes, basically I was using a guide for a tutorial, however it seems is not very effective, thanks for your answer, I tried and kind of worked however is giving me a few error that I don't understand why, for sample, is breaking cause it says the get_the_permalink() is not define and also is giving me an echo of the link that weird, and is not taking the H2 tags at all, what do you think it could be causing the errors? Commented Nov 25, 2011 at 18:35
  • I realized there is an error in the function, it's get_permalink, that is solved, however the big issue now is displaying the results like... the functions firsts and then all the clases, I will put a picture in the question for you to see Commented Nov 25, 2011 at 18:40
  • @andrewkthx Wrote it up quickly without proof reading, sorry. Edited the answer, so it's correct for future reference (get_permalink). I don't exactly understand what your problem is now - if you're not satisfied with the markup, just edit the loop. As for how it looks - that's a styling (i.e. css) issue and should be dealt with in your stylesheets. Commented Nov 25, 2011 at 19:16
  • Thanks again Johannes, what I meant is, notice there is something weird cause for sample, the code says '<h2 style="margin-bottom:5px">'. get_the_title() . '</h2>'. however notice the output in the source code I uploaded, is generating something like first the title with the other function's output and then after all, is displaying the divs and tags, the rest it works great for sure Commented Nov 25, 2011 at 19:43

I am not very good with shortcodes because I almost not use these, but here is my contribution.

function loop_shortcode($atts, $content = null) {

        'post_parent' => 8,
            'post_type' => 'page',
            'posts_per_page' => 4,
            'sort_column' => 'menu_order'
    ), $atts);

    $the_query = new WP_Query();

    if ($the_query->have_posts()) : while ($the_query->have_posts()) :  
             $the_query->the_post(); ob_start(); ?>

    <div id="service-hp">
              <?php the_post_thumbnail('home-thumb') ?>
                     <h2 style="margin-bottom:5px"><?php the_title() ?></h2>
                                    <?php the_excerpt() ?>
       <a class="read-more" href="<?php the_permalink() ?>">en savoir plus <img src="<?php bloginfo( "template_url" ); ?>/images/read-more.png"></a>
        </div><!-- /#service-hp -->

 <?php endwhile; endif; wp_reset_query(); 
        $content = ob_get_contents(); ob_end_clean();

                       return $content;


add_shortcode('myloop', 'loop_shortcode');

Your Answer

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

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