【发布时间】:2010-10-28 02:35:27
【问题描述】:
我有一个正在使用的模板,它可以选择在首页的特色部分中显示一组帖子,或者在同一特色区域中显示一组指定的页面。我找到了显示非此即彼的代码,但是 我不确定如何将两者结合在一起并获得一组帖子和页面的列表。
据我了解,query_posts() 会覆盖 Wordpress 在页面上显示的任何项目集,因此在这里,根据主题所处的模式,它会将参数传递给 query_posts() 以获取特定类别的帖子,或者传入一个页面数组:
<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('mytemplate_feat_pages'),
'showposts' => $featured_num
));
} ?>
<!-- Start my loop to display everything-->
<?php if (have_posts()) : while (have_posts()) : the_post();
global $post; ?>
到目前为止,我已经使它更简洁了,但无法克服最后一点如何组合参数说 query_posts(getMyPostsArray().AddList(ohINeedACouplePagesToo())) //Yes ,我知道这看起来像 C# 什么的...我不是 PHP 人..
这里的代码更易读,更接近我想要的:
$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
));
【问题讨论】: