不用插件调用WordPress随机文章、最新文章、和30天内最热文章。
昨天介绍了不用插件实现WordPress网站统计信息 用WordPress的话尽可能不用插件或者少用插件。今天介绍的是不用插件调用WordPress随机文章、最新文章和30天内最热的文章。
随机文章对于SEO而言是非常有用的,就算你的网站长时间不更新,快照也会经常更新不会停歇。所以说调用随机文章是非常有必要的,一般调用在边栏比较好。具体方法如下:
不用插件调用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>
不用插件调用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>
不用插件调用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>
这些是游子网络正在使用的调用代码,如果用的与游子网络是同一主题这些代码可以直接使用添加在主题模板topbar.php文件中即可。其中里面的核心部分不变,实际应用的时候需要根据自身的主题调整。numberposts=6 即调用6篇文章、showposts=8 即调用8篇文章。可以根据实际情况做出调整。
其中<?php echo mb_strimwidth(get_the_title(), 0, 42, ‘…’); ?> 使用的是 控制WordPress文章标题长度 42代表24个汉字,以此类推。具体请见
控制WordPress文章标题长度的方法
Very useful blog. Thanks for sharing this information. It helps a lot since I’m using wordpress.
You’re welcome
可以调用,但是我的站编码是GBK,WP是UTF,调用过来是乱码。有什么办法可以解决么?
外部调用不是用这种方法的
加入我的主题中去后,博客500错误了,不知道是哪里出问题了呢?
看看是否加对。不会有问题的
我正想给自己添加个随机文章或是经典文章页面或是板块以增加文章的寿命呢。博主的方法很不错
不用插件是最好的
我的就是wordpress 要好好看看哦
路过,好文章,学习中,谢谢你的分享。
很好,学习了,博主,顶你啊
路过,学习了,谢谢你的分享,加油啊