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've been working on this shortcode w/o any success...

function loop_shortcode( $atts = '' ) {

'<div class="clear"></div>
<div class="childs grid_12"> 
 <?php
 $the_query = new  WP_Query( 
     array(
         "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() ?>
</div>';
 }

 function my_loop_shortcode( $atts ) {
     ob_start(); 
     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

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

2 Answers

up vote 2 down vote accepted

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">'.
                   get_the_post_thumbnail('home-thumb').
                   '<h2 style="margin-bottom:5px">'.
                   get_the_title().
                   '</h2>'.
                   get_the_excerpt().
                   '<a class="read-more" href="'.
                   get_permalink().
                   '">en savoir plus <img src="'.
                   get_bloginfo( 'template_url' ).
                   '/images/read-more.png"></a></div><!--  ends here -->';
    endwhile;
    wp_reset_query();
    $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:

share|improve this answer
    
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? –  andresmijares25 Nov 25 '11 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 –  andresmijares25 Nov 25 '11 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. –  Johannes Pille Nov 25 '11 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 –  andresmijares25 Nov 25 '11 at 19:43
add comment

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

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

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

    $the_query = new WP_Query();
    $the_query->query($atts);

    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');
share|improve this answer
add comment

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.