wordpress独立页面调用指定分类文章
wordpress 在做企业模板时,经常需要使用独立页面调用指定分类的文章,下面就整理一下!
这里不截图了,直接分享页面的标准模板代码。
一、get_posts() 方案
<?php
/*
Template Name: 产品展示
*/
?>
<?php get_header(); ?>
<div class="article">
<div class="page_piclist">
<ul class="product">
<?php $yang_pro_catid = "9";//这里设置产品分类ID,也可以在制作模板过程中预留手动填写项:get_option('yang_honor_catid');
$posts = get_posts("category=".$yang_pro_catid."&numberposts=-1");//&orderby=rand
if( $posts ) :
foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li>
<a class="shadow8" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<img border="0" alt="<?php the_title(); ?>" src="<?php echo catch_first_image(); ?>">
<p>TIME:(<?php echo the_time('Y-m-d'); ?>)</p>
<h3><?php the_title(); ?></h3>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div>
<?php include (TEMPLATEPATH . '/sidebar.php'); ?>
<?php get_footer(); ?>
说明一下,get_posts()函数可以自己去 wordpress 官网查看具体参数: CodeX 传送门
这里小提示一下 get_posts() 的几个常用参数:
category 指的是分类ID,而非分类名称;
numberposts 默认是调用5篇日志,如需调用全部,可设置为 -1;
orderby 默认按 post_date 来排序,可以使用 rand 进行随机调用……
二、query_posts() 方案
<?php
/* template name: 模板 - 游戏帮助 */
if(!defined('WP_USE_THEMES')) { exit('Access Denied'); }
get_header();
$category_id = 4; //游戏帮助
$category_name = get_cat_name( $category_id );
$category_link = get_category_link( $category_id );
?>
<div class="article">
<?php
//调用指定文章分类及其它参数
query_posts('cat='. $category_id .'&orderby=modified&order=desc');
if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article class="article-excerpt">
<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); if( is_sticky() ){ echo ' <span style="color:red;">[置顶]</span>';} ?></a></h2>
<div class="art-meta">
<?php the_time('Y.m.d'); ?>
</div>
</article>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
</div>
<?php include (TEMPLATEPATH . '/sidebar.php'); ?>
<?php get_footer(); ?>
好了,今儿就这些吧,有问题,欢迎留言交流!!