DevOps运维技术栈

WordPress网站中调用不同文章的代码分享-WordPress建站笔记

我们在编辑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; ?>
退出移动版