Support » Fixing WordPress » Create a Shortcode for a WP_Query loop

  • 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?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Create a Shortcode for a WP_Query loop’ is closed to new replies.