网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站建设 > WordPress
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 112.87.85.4
Old 2015-06-14, 06:26 AM
Jarvisburg Jarvisburg is offline
初级会员
 
Join Date: 2010-07-16
Posts: 2
Jarvisburg 现在声名狼藉
Default wordpress显示多个分类的文章

显示多个分类下的文章可以这样提取:
<?php query_posts(array('posts_per_page'=>10,'orderby'=>'rand','category__and'=>array(67,70)));?>
<?php if(have_posts()):while(have_posts()):the_post(); ?>
<li><span class="grayz">· </span><a href="<?php the_permalink(); ?>" target="_blank" class="ab"><?php echo cut_str($post->post_title,45); ?></a></li>
<?php endwhile;?>
<?php else:?>
<p>Sorry,no posts matched your criteria.</p>
<?php endif;wp_reset_query();?>

首先把要读取的分类目录的ID找出,放在array数组中。
借助强大的query_posts函数实现随机(’orderby’ => ‘rand’)读取十篇(’showposts’ =>10)在指定分类目录(’category__in’ => array(7,17))下的文章。

最后调用wp_reset_query();因为query_posts指定规则后不止是对它下一个LOOP生效,可能导致全站其他LOOP调用失效,wp_reset_query可以用来清空wp_query规则。

下面详细介绍wordpress下使用率最高的分类目录系列参数( 注意:下划线是“__”不是“_”):
cat:分类ID

根据ID显示单个分类

举例:分类ID4(以及该分类目录下的子分类目录)的文章:
query_posts('cat=4');

根据ID显示多个分类 举例:分类ID1、2、3下的文章:query_posts('cat=1,2,3');
排除某一分类中的文章


举例:显示除分类ID为3的文章外的所有文章,被排除的分类ID以减号(’-')作为前缀。


query_posts('cat=-3');

category_name:分类名称


query_posts('category_name=Staff Home');

category__and:同时存在于多个分类里
举例:既是分类ID1,也是分类ID5的文章:

query_posts(array('category__and' => array(1,5)));

举例:获取当前分类与分类ID5的文章
query_posts(array('category__and'=>array(get_cat_id(single_cat_title('',false)),5)));

single_cat_title():用于显示或返回当前页面的分类名称,不能用于LOOP循环。有两个参数可以使用。前缀$prefix用于在分类名称前加入相应的内容,比如文字、图片等,例句中就是在分类名称前加入了“当前分类:”。是否显示$display,是布尔值,为TRUE则直接用于显示标题,而FALSE则用于PHP操作,下面例句将最能说明问题。(如果使用了汉字,请用 UTF8格式保存,使用ANSI时,汉字为乱码)。

get_cat_ID():用于获取指定分类的ID,参数只有一个$cat_name,如果错误返回0,否则返回分类ID。这里拿做读取当前分类列表里的例子作为说明。.’<br />’是为了方便查看效果而设置的换行。第一句是获取分类ID的关键所在。这句话只能使用在是分类的页面中,也就是is_category为返回为真(1)才能生效。

category__in:存在于一系列分类之中
举例:属于分类ID1或分类ID5的文章:

query_posts(array('category__in' => array(1,5)));

category__not_in:不存在一系列分类之中
举例:不属于分类ID1,且不属于分类ID5的文章:

query_posts(array('category__not_in' => array(1,5)));
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 05:36 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.