DiggingIntoWordPress

by Chris Coyier & Jeff Starr

Daily archives: Thursday, January 21, 2010

Custom Query Shortcode: Run a Loop inside Any Post/Page

Posted by on

I had the occasion yesterday to have a page with a section on it where it would output a very specific set of other pages, which would need to change dynamically. What I could have done is built a special page template for this page, and inside that template run a query_posts() to get these posts. But I wanted this page to remain editable from the admin. Besides, creating a special page template every time you need to do something like this is too cumbersome. WordPress is extensible enough to do better.

Hence, the Custom Query Shortcode!

Previous Post Link on index.php

Jack Osborne emailed me with a question. His blog layout shows single posts at a time, with a set of arrows to navigate forward and backward in time. This works great on the single.php page where you can use next_post_link() and prev_post_link(), but those don’t work on the index.php file.

Jack wrote up the solution I sent him.

Code is poetry