How to display a set of combined posts and pages using Wordpress' query_posts() method
I've got a template that I'm using that has the option of showing either a set of posts on the front page in a featured section or, optionally, display a set of specified pages in the same featured area. I found the code where it displays either/or, however I'm not exactly sure how to combine the two together and get a list of a set of posts AND pages together.
It's my understanding that query_posts() overrides whatever set of items Wordpress displays on the page, so here, depending on what mode the theme is in, it passes in parameters to query_posts() to grab posts of a particular category, or passes in an array of pages:
<div id="slides">
<?php global $ids;
$ids = array();
$featured_cat = get_option('mytemplate_feat_cat');
$featured_num = get_option('mytemplate_featured_num');
if (get_option('mytemplate_use_pages') == 'false') query_posts("showposts=$featured_num&cat=".get_cat_ID($featured_cat));
else {
global $pages_number;
if (get_option('mytemplate_feat_pages') <> '') $featured_num = count(get_option('mytemplate_feat_pages'));
else $featured_num = $pages_number;
query_posts(array
('post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC',
'post__in' => get_option('mytem开发者_C百科plate_feat_pages'),
'showposts' => $featured_num
));
} ?>
<!-- Start my loop to display everything-->
<?php if (have_posts()) : while (have_posts()) : the_post();
global $post; ?>
So far, I've made it a little more consise, but can't get over the last little bit of how to combine the parameters to say query_posts(getMyPostsArray().AddList(ohINeedACouplePagesToo())) //Yes, I know this looks like C# or something...I'm not a PHP guy..
here's the code in a little more readable version that's closer to what I want:
$featured_cat = get_option('mytemplate_feat_cat');
//I combined featured_num to get the total number of featured items to display
$featured_num = get_option('mytemplate_featured_num') + count(get_option('mytemplate_feat_pages'));;
query_posts("showposts=$featured_num&cat=".get_cat_ID($featured_cat));
//I think this second line overwrites the first query_posts() :-/
query_posts(array
('post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC',
'post__in' => get_option('mytemplate_feat_pages'),
'showposts' => $featured_num
));
Ryan, why don't you just go ahead with two queries and two loops?
query_posts("post_type=post&showposts=3");
while (have_posts()) { the_post(); }
query_posts(array(
"post_type" => "page",
"post__in" => get_option("mytemplate_feat_pages"),
"showposts" => 5
));
while (have_posts()) { the_post(); }
If you need a certain number of posts and pages to fill in the space, you can also use $wp_query->found_posts
to actually calculate what you have and what you need. This would probably be the easiest solution, since even if you can get posts and pages in SQL, you might have a hard time ordering them, since posts don't have a menu order, while you wouldn't like pages ordered by publish date.
Hope that helped, Cheers!
精彩评论