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

In WordPress how can I display multiple custom types on the same page?

  • I have two post types: post and a custom type called slider. I want to put a slider in the header that gets its content from slider and I want post to continue to function as post. I've been up and down the codex. I've read a couple blog articles on Justin Tadlock's site. I can get a loop to do both. But I can't seem to have one loop do one and then the other loop do the other. After the first (slider) loop I use: wp_reset_query(); and wp_reset_postdata(); but that doesn't work either. In short, I'm stumped. Do I have to force a loop to include both/all and then use a conditional to only display the post type I want for the given loop? Yes, I know I should be able to specify the post type I want. However, as mentioned, that's not working - or maybe it's my brain that's not working.

  • Answer:

    You might also consider doing the slider by using post_ID instead of a "generic" query. get_post() doesn't care about post type, only ID. I have a widget for doing this if you're interested just let me know.

Mark Simchock at Quora Visit the source

Was this solution helpful to you?

Other answers

You should be able to do this with two loops as you've already mentioned without an issue, so not knowing what exactly is happening when you try to run it (any errors? nothing appearing in slider, but the posts showing? etc.) then I just have to guess at a possible answer. If this is a slider that is before the main loop, you could just use query_posts(posts_per_page=5&post_type=slider) to run your query for the slider post type, then use the standard loop output to show it [if ( have_posts() ) : while ( have_posts() ) : the_post();], and finish like you mentioned with wp_reset_query(); after the loop ends to then start the main post loop. Or you could use a combination of get_posts($args) and setup_postdata($post) to accomplish the same goal. Check the codex to see if that's what you need. I do this on one of my new WordPress websites - http://NewMediaWorkshop.tv - to run my featured video tutorial header, then output the main loop below to show the normal post feed. Hope this helps!

Brian Hamlett

What kind of page is this? A static page that uses page.php? A blog post page that uses single.php? Something else? It sounds like you are customizing single.php to bring a slider in somewhere above your regular post content. If so I would think you could run the query above the main loop and leave that as it is. As an example I am working on a site now that shows related blog posts on product pages. (They use a custom post type single-products.php) I use my main loop in that page to get the product info, then have a query that pulls in related posts based on which tag was selected on the product page. It pulls in the 3 most recent posts in that tag's term and shows headline and excerpt. Would you be doing something similar but trying to pull in the content (that includes a slider) for one post? Will you use a different slider on each page? Is it just one standard slider that you need to bring in? Is the slider grabbing the content from the other post type? You might also want to consider using custom fields rather than post types. http://www.advancedcustomfields.com/ is good for that. I"m not sure if it makes sense in your case or not, but take a look, it might help. Good luck.

Heidi Cool

The easiest way would be use a WP_query, you can then use the 'post_type' => 'slider' (for example) within the array of arguments to define which custom post type should be used.

Martin Lucas

Try http://wordpress.stackexchange.com/.  This is a more technical site than the codex and http://Stackoverflow.com (the parent website) is one of the best technical help forums online.

David Wen

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.