无插件实现WordPress文章调用方法(随机/最新/热门)
浩子在前面的”提高WordPress运行性能的7个简单方法“讲到我们要尽可能的减少使用插件的个数。因为我们使用插件越多,约会降低我们网站的运营效率。那我们有些插件必须安装,是安装呢还是怎么办?还有一种方法,我们可以采用无插件方法,直接在代码中插入。比如我将Godaddy优惠码专题博客想添加一个随机文章,那我们应该如何调用呢。我们都知道,直接可以用随机插件就可以了。
但是我不希望这么做,我直接加入无插件版的调用方法,并且把方法分享给大家。
1、无插件调用WordPress最新文章
<ul><li><h3><?php _e(‘最新文章’); ?></h3><?php query_posts(“showposts=6&caller_get_posts=1&orderby=date&order=DESC”); ?><ul><?php if (have_posts()) : while (have_posts()) : the_post(); ?><li><a href=”<?php the_permalink() ?>” title=”<?php the_title() ?>”><?php echo mb_strimwidth(get_the_title(), 0, 42, ‘…’); ?></a></li><?php endwhile; endif; ?></ul></li>
2、无插件调用WordPress30内最热文章
<li><h3><?php _e(‘最热文章’); ?></h3><?php function filter_where($where = ”) { //posts in the last 30 days $where .= ” AND post_date > ‘” . date(‘Y-m-d’, strtotime(‘-30 days’)) . “‘”; return $where; } add_filter(‘posts_where’, ‘filter_where’); query_posts(“showposts=8&v_sortby=views&caller_get_posts=1&orderby=date&order=desc”) ?><ul><?php if (have_posts()) : while (have_posts()) : the_post(); ?><li><a href=”<?php the_permalink() ?>” title=”<?php the_title() ?>”><?php echo mb_strimwidth(get_the_title(), 0, 42, ‘…’); ?> </a></li><?php endwhile; endif; ?><?php wp_reset_query(); ?></ul></li>
3、无插件调用WordPress随机文章
<ul><li><h3><?php _e(‘随机推荐’); ?></h3><ul><?php $rand_posts = get_posts(‘numberposts=6&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, 42, ‘…’); ?></a></li><?php endforeach; ?></ul></ul>
一般网站只需要用到上面三种方法,现在大家可以卸载插件,直接用代码实现了。
本文原创固定地址:http://www.goyouhuima.com/yunying/557.html ,转载请注明出处!