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
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 asthe_excerpt()
andwp_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, butecho
orreturn
the desired result directly. The latter two are more or less personal preferences.That being said, this will do what you want:
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:
WP_Query
classI am not very good with shortcodes because I almost not use these, but here is my contribution.