Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 67836

Create a Shortcode for a WP_Query loop

$
0
0

Replies: 0

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

`<?php
$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.

Example: `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?

  • This topic was modified 4 minutes ago by  geisthanen.

Viewing all articles
Browse latest Browse all 67836

Trending Articles