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'm breaking my head trying to create a short code to display certain loop, however I'm totally cut, I can not figure it out how to do it, since it's the first time I try to do it... I do now want to set any parameter, just put exactly as it if... I would appreciate if anyone can light me...

This is the code I want to put into the shortcode

 <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><?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>
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Take a look at my latest plugin ShortCodes UI which lets you create shortcodes without coding, all you have to do is paste your loop inside the right field of the shortcode (PHP) and you are done.

share|improve this answer
    
Probably that would solve the problem, however I'm looking for a built-in solution, thxs anyway –  andresmijares25 Nov 23 '11 at 22:15
add comment

I'm displaying my loops this way:

function loop_shortcode( $atts = '' ) {

   (your shortcode code goes here)

}

function my_loop_shortcode( $atts ) {
         ob_start(); 
         loop_shortcode($atts);
         return ob_get_clean(); 
}         

add_shortcode('my_loop', 'my_loop_shortcode'); 

The "normal" way looks like this:

function my_loop_shortcode( $atts ) {
             return 'your shortcode output';
    }         

    add_shortcode('my_loop', 'my_loop_shortcode'); 

But doesn't cope with loops too well.

Learn more at http://codex.wordpress.org/Shortcode_API

share|improve this answer
    
could you show a sample of how it looks with the loop inside please, I'm kind of lost –  andresmijares25 Nov 23 '11 at 22:16
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.