ถ้าเรียก category ใด category หนึ่ง นอก loop
<? $category = get_the_category(); $args = array( 'numberposts' => 20,'category' => $category[0]->cat_ID ); $lastposts = get_posts( $args ); foreach($lastposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?>
ธีมที่ทำหน้า index.php ใส่โค๊ดนี้
พอกดหน้า category มันก็แยกเอง ไม่ได้ใช้อะไรมากกว่านี้
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
// อันนี้ คือ โชว์โพสล่าสุด ตามจำนวนที่กำหนดไว้ เช่น 10 โพส(ค่าdefault)
ใส่ code ใน sidebar.php ก็ได้ หรือ index.php<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>
<?php $count = 1; $query1 = new WP_Query();$query1->query('showposts=3&cat='. $category->cat_ID .''); ?><?php while ($query1->have_posts()) : $query1->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count++; endwhile; wp_reset_query(); ?><?php $a = 1; $query1 = new WP_Query();$query1->query('showposts=12&cat=4&orderby=desc'); ?>
<?php while ($query1->have_posts()) : $query1->the_post(); ?>
<h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
<?php endwhile; ?>ต้องการแสดงในหน้าอื่นที่ไม่ใช่ในหน้า category คับ เพราะงั้นมันเลยต้อง query ใหม่
—
กด category เข้าไป ก็จะเป็นโพสที่อยู่ในหมวดนี้
เช่น กด category 1 ก็จะเจอ post1, post2, post3, …. ลิสลงมาเรื่อยๆ
สรุปได้แล้ว
<?php query_posts('cat=3&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</li>
<?php the_excerpt(); ?>
<p><a href="<?php the_permalink() ?>" >readmore</a></p>
<?php endwhile; ?>