This question already has an answer here:

I'm a noob Wordpress Developer and I just created my first Custom Template Page using Advanced Custom Fields and managed to loop.

            $args = array(
                'post_type' => 'art',
                'orderby' => 'title',
                'order' => 'ASC'
            $the_query = new WP_Query( $args );         
        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 

        <?php get_template_part( 'content', 'art' ); ?>

        <?php endwhile; endif; ?>

But I will like to use it not only inside a template page, but anywhere I want. Therefore I need to create a shortcode.


function foobar_func( $atts ){
    return "foo and bar";
add_shortcode( 'foobar', 'foobar_func' );

My question would be: How can i put the loop inside my shortcode?

marked as duplicate by cybmeta, Community Oct 19 '16 at 11:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


I´m doing this since last year and i´m getting the shortcodes working:

   function my_code() {
   return ob_get_clean();   
add_shortcode( 'logos', 'my_code' );

where get_template_part loads the file loop_logos.php and logos is the name shortcode [logos].


When I started to do this I used to name all equal to avoid mistakes, Ex:

function loop_logos() {
       return ob_get_clean();   
    add_shortcode( 'loop_logos', 'loop_logos' );

So I had to worry about to upload the loop_logos.php on the right folder. and call the shortcode with [loop_logos]

  • I used ob_start(); and return ob_get_clean(); and it worked. – A A Oct 19 '16 at 11:22

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