张力-实战网络营销导师
专注于网络营销推广+Wordpress建站,只做有思想的高价值网站,只提供有担当的营销服务!
文章496浏览3639763本站已运行1360

wordpress常用的几种调用方法

 最近张力用wordpress程序折腾了两套模板了,对于wordpress程序也玩了好几个月了,对于它的一些调用手法上也了解了一些。今天这篇文章就是跟大家讲一下常用的几种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; ?>

上面这段代码中$sticky, 0, 5中的5代表的是置顶文章调用条数,<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a>代表的是调用的文章链接和标题。注意:大家在添加置顶文章时,一定不要忘了最后的<?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代表的是调用文章条数。这个你可以自己调整为自己想要显示的文章条数。

以上这五种wordpress调用方法,也是张力自己在用wordpress程序做站时有经常用到的,希望对大家有所帮助!

您还未添加分享代码,请到主题选项中,添加百度分享代码!

张力


Warning: Undefined variable $comment_i in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/comments.php on line 19
广告
发表我的评论
取消评论
表情 签到
流汗坏笑撇嘴大兵流泪发呆抠鼻吓到偷笑得意呲牙亲亲疑问调皮可爱白眼难过愤怒惊讶鼓掌

Hi,您需要填写昵称和邮箱!

(11)个小伙伴在吐槽
  1. 医药商业行业报告
    医药商业行业报告
    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
       未知   未知
    2013-01-12 23:10

    学习学习,收货不错啊

  2. 无锡贷款
    无锡贷款
    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
       未知   未知
    2012-12-18 08:41

    对代码还是缺少了解

  3. 陈松松视频营销
    陈松松视频营销
    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
       未知   未知
    2012-12-16 10:44

    谢谢张力大哥分享,在网上找了好长时间,哈哈今天这里看到了呃,谢谢哦!

  4. 麦田
    麦田
    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
       未知   未知
    2012-12-15 23:49

    不错的主题,我用了之后遇到2个问题:
    1个是边栏调用的最新评论不会显示。
    2置顶文章只可以显示一篇,我自己置顶了3篇还是一篇,而且在分类目录页也不显示,还请楼主赐教。

    拜谢

    • 张力
      张力
      Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

      Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

      Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

      Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
         未知   未知
      2012-12-15 23:53

      @麦田 请QQ联系我!

  5. 名装网
    名装网
    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
       未知   未知
    2012-12-15 16:49

    不错,学习了

  6. 河南加盟代理网
    河南加盟代理网
    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
       未知   未知
    2012-12-15 11:54

    不太懂,不过可以学习。

  7. 润初颜
    润初颜
    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
       未知   未知
    2012-12-15 11:18

    好东西,学习了!

  8. 凌妏娅
    凌妏娅
    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
       未知   未知
    2012-12-14 16:16

    这个还不是很懂,都是别人给做好的

  9. 足球比分
    足球比分
    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
       未知   未知
    2012-12-14 10:43

    还不错哦

  10. 苏州旅行社
    苏州旅行社
    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 141

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 151

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 156

    Warning: Undefined variable $useragent in /www/wwwroot/zhangliseo/wp-content/themes/ZL-ZMT/functions/admin/comment.php on line 161
       未知   未知
    2012-12-14 09:10

    持续关注此次的教学过程,也欢迎博主及时分享.

隐藏边栏