在 WordPress 主題製作開發中經常會需要在特定的頁面中呼叫出指定的文章或文章列表,接下來小編就來教大家如何呼叫出 WordPress 文章列表。
呼叫網站最新文章:
<?php
query_posts('showposts=10&orderby=new'); //showposts=10 表示 10 篇
while (have_posts()): the_post();
?>
<li><a href="<?php%20the_permalink();%20?>" target="_blank"><?php the_title() ?></a></li> //這裡可以寫成你自己需要的樣式
<?php endwhile; ?>
呼叫隨機文章:
<?php
query_posts('showposts=10&orderby=rand'); //showposts=10 表示 10 篇
while (have_posts()): the_post();
?>
<li><a href="<?php%20the_permalink();%20?>" target="_blank"><?php the_title() ?></a></li> //這裡可以寫成你自己需要的樣式
<?php endwhile; ?>
呼叫某個分類下的最新文章:
<?php
query_posts('showposts=10&cat=1'); //cat=1 為呼叫 ID 為 1 的分類下文章
while (have_posts()) : the_post(); ?>
<li><a href="<?php%20the_permalink()%20?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
排除某個分類下的文章:
<?php
query_posts('showposts=10&cat=-1'); //cat=-1 為排除 ID 為 1 的分類下文章
while (have_posts()) : the_post(); ?>
<li><a href="<?php%20the_permalink()%20?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
以上就是文章列表的呼叫方法,可以將例子中的程式碼結合起來達到你需要的效果。