我们在编辑WordPress网站内容的时候难免会遇到要调用站内文章的情况,比如调用最新的文章,或者是某个分类下的文章,如果一个个手动调用,那就有点麻烦了,实际上我们可以通过代码来完成,下面和大家分享几种调用不同文章的代码。

1、调用网站最新文章,代码如下:

<?php
  
  query_posts(‘showposts=10&orderby=new’); //showposts=10表示10篇
  
  while(have_posts()): the_post();
  
  ?>
  
  <li><a href=”<?php the_permalink(); ?>”target=”_blank”><?php the_title() ?></a></li> //这里可以写成你自己需要的样式
  
  <?php endwhile; ?>
  
  //这里可以写成你自己需要的样式

2、调用随机文章,复制代码代码如下:

<?php
  
  query_posts(‘showposts=10&orderby=rand’); //showposts=10表示10篇
  
  while(have_posts()): the_post();
  
  ?>
  
  <li><a href=”<?php the_permalink(); ?>”target=”_blank”><?php the_title() ?></a></li> //这里可以写成你自己需要的样式
  
  <?php endwhile; ?>

3、调用某个分类下的最新文章,代码如下:

<?php
  
  query_posts(‘showposts=10&cat=1’); //cat=1为调用ID为1的分类下文章
  
  while(have_posts()) : the_post(); ?>
  
  <li><a href=”<?php the_permalink() ?>”title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>
  
  <?php endwhile; ?>

4、排除某个分类下的文章,代码如下:

<?php
  
  query_posts(‘showposts=10&cat=-1’); //cat=-1为排除ID为1的分类下文章
  
  while(have_posts()) : the_post(); ?>
  
  <li><a href=”<?php the_permalink() ?>”title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>
  
  <?php endwhile; ?>

以上就是调用WordPress文章列表的方法,可以将例子中的代码结合起来达到你需要的效果。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。