wordpress常用函数-随机文章获取和相关文章获取

  • 2019 年 12 月 10 日
  • 筆記

1、wordpress随机获取文章10篇文章,轻松实现wordpress文章的随机调用;

<?php  $rand_posts = get_posts('numberposts=10&orderby=rand');  foreach($rand_posts as $post):  ?>  <!–下面是你想自定义的Loop–>  <li>  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>  </li>  <?php endforeach;?>

2、获取wordpress文章的相关文章代码:

//获取相关文章  <?php  $tags = wp_get_post_tags($post->ID);  if ($tags) {  $first_tag = $tags[0]->term_id;  $args=array(  'tag__in' => array($first_tag),  'post__not_in' => array($post->ID),  'showposts'=>10,  'caller_get_posts'=>1  );  $my_query = new WP_Query($args);  if( $my_query->have_posts() ) {  while ($my_query->have_posts()) : $my_query->the_post(); ?>  <li><a href='<?php the_permalink() ?>' rel='bookmark'  title='<?php the_title_attribute(); ?>'><?php the_title();?>  <?php comments_number(' ','(1)','(%)'); ?></a></li>  <?php  endwhile;  }  }  wp_reset_query();  ?>