PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > CMS教程 > wordpress教程 > 正文

    wordpress如何调用文章置顶、随机、热门等内容

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:wordpress如何调用文章置顶、随机、热门等内容??B5教程网​跟大家讲一下常用的几种wordpress调用方法。第一种:最新文章调用  这个是我...
    wordpress如何调用文章置顶、随机、热门等内容??B5教程网​跟大家讲一下常用的几种wordpress调用方法。

    第一种:最新文章调用
     
      这个是我们在用wordpress程序时经常要用到的,以下是调用代码:
    <?php $rand_posts = get_posts(‘numberposts=8&orderby=post_date’); foreach( $rand_posts as $post ) : ?>
    <li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php echo mb_strimwidth(get_the_title(), 0, 50, ‘…’); ?></a></li>
    <?php endforeach; ?>
     
      请注意,上面红色数字8是代表调用的文章条数,这个您可以自己调整的。而后面的0,50这个是调用文章的标题显示为50个字节,也就是文章标题最多显示25个字。
     
      第二种:随机文章调用
     
      这个也是我们在用wordpress程序时经常用到的,因为这个可以让你的页面时刻都是不同内容的,对于优化上是有一定好处的,具体代码如下:
    <?php $rand_posts = get_posts(‘numberposts=8&orderby=rand’); foreach( $rand_posts as $post ) : ?>
    <li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”> <?php echo mb_strimwidth(get_the_title(), 0, 50, ‘…’); ?> </a></li>
    <?php endforeach; ?>
     
      同上面提到的一样,红色数字8是代表调用的文章条数,这个您可以自己调整的。而后面的0,50这个是调用文章的标题显示为50个字节,也就是文章标题最多显示25个字。
     
      第三种:热门文章调用
     
      这个相信大家都知道,这个的调用可以显示我们网站最多浏览的文章,也就是用户最关注的文章。以下为调用代码:
    <?php $postslist = get_posts(‘numberposts=8&order=DESC&orderby=comment_count’); foreach( $postslist as $post ) : ?>
    <li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php echo mb_strimwidth(get_the_title(), 0, 50, ‘…’); ?></a></li>
    <?php endforeach; ?>
     
      同上面两个一样,红色数字8是代表调用的文章条数,这个您可以自己调整的。而后面的0,50这个是调用文章的标题显示为50个字节,也就是文章标题最多显示25个字。
     
      第四种:置顶文章调用
     
      这个大家就都不陌生了,这个是很多网站都有用到的,wordpress的置顶文章代码是分为两个部分的,我这里就给大家说一下。
    <?php
    $sticky = get_option(‘sticky_posts’);
    rsort( $sticky );
    $sticky = array_slice( $sticky, 0, 5);
    query_posts( array( ‘post__in’ => $sticky, ‘caller_get_posts’ => 1 ) );
    if (have_posts()) :
    while (have_posts()) : the_post();
    ?>
     
    <li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>” rel=”bookmark”><?php the_title(); ?></a></li>
    <?php endwhile; endif; ?>
     
      第五种:调用指定分类
     
      我们有时候在用wordpress程序时希望自己某一页面调用指定的分类,下面这段代码就可以让你在使用wordpress时,指定调用某个分类下的文章,代码如下:
     
    <?php query_posts(‘cat=15&posts_per_page=8&caller_get_posts=1′); ?>
    <?php while (have_posts()) : the_post(); ?>
     
    <li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”> <?php echo mb_strimwidth(get_the_title(), 0, 50, ‘…’); ?> </a></li>
    <?php endwhile; ?>
     
      上面这段代码中的cat=15中的15代表的是分类ID,我们只需要修改这个数字为自己的分类ID就可以了;而数字8代表的是调用文章条数。这个你可以自己调整为自己想要显示的文章条数。
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-103-623-1.html
    相关热词搜索: wordpress