How can I display older custom post outside the loop in Wordpress?

How can I display the_content from the regular loop and several custom post types on the same page template in WordPress?

  • Answer:

    The simplest way is to put the content from a custom post type into a variable while in a regular loop (as dictated by a template). For example, suppose we are in a Page template, so it looks like this: get_header(); the_post(); <h1><?php the_title(); ?></h1> <?php the_content(); ?> Now I want to output the content from my kittens custom post type: <?php $kittens = get_posts(array('post_type'=>'kittens','posts_per_page'=>-1)); foreach($my_cpt as $kitten) { echo apply_filters('the_content', $kitten->post_content); } ?> What is happening here is that I am loading all my kittens into a $kittens variable, then looping through each kitten obtained thereby, and for each kitten I am outputting the post_content of the kitten after running it through the "the_content" filter, which does all the fancy stuff that WP applies in the the_content() template tag.

Daniel Quinn at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.