欢迎光临
我们一直在努力

最常用的五种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调用方法是共享资源网总结的希望对大家有所帮助!

赞(0) 打赏
未经允许不得转载:新起点博客 » 最常用的五种WordPress调用方法


关注公众号『新起点软件管家』

获取最新网络资源及破解软件!
带你玩转各样软件...

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏