Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

FUN! Create a short code from a commonly used loop WordPress


I just had a great idea!

I often use the same loop on a majority of the custom themes I produce, always pulling content from a particular page based on ID.

<em>Since everything is customized for each client, I don’t have to worry about customers changing ID’s.</em>

I am not sure if ‘shortcode’ is the right term, include is just as easy … except I want to pass the ID.


<?php $my_query = new WP_Query("showposts=1&post_type=page&page_id=666"); while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID; the_content(''); endwhile; ?>


<?php echo do_shortcode('[loop id="666"]'); ?>

<em>Any suggestions would be greatly appreciated!</em>

Answers (1)


Maor Barazany answers:

You can just create a function and put it each time in the functions.php file of the theme.

Something like -

function my_custom_loop($pageid) {

$my_query = new WP_Query("showposts=1&post_type=page&page_id=".$pageid); while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID; the_content(''); endwhile; ?>

Than call it when you need -

<?php my_custom_loop('666');?>