如果想在wordpress主題制作過程中調用最新、熱門、隨機文章,可以使用wordpress自帶的widget側邊欄的小工具功能來實現,但是這樣會影響到我們想自定義側邊欄的效果,當然也有一些可以使用插件來實現這個功能的,不過使用插件始終對SEO方面有一定的影響,下面我給大家介紹一種不用插件也能實現.
wordpress最新、熱門、隨機文章的調用方法.
1、最新文章的調用代碼:
- <ul>
- <?php $post_query = new WP_Query(‘showposts=10′);
- while ($post_query->have_posts()) : $post_query->the_post();
- $do_not_duplicate = $post->ID; ?>
- <li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
- <?php endwhile;?>
- </ul>
2、熱門文章的調用代碼:
- <ul>
- <?php
- $post_num = 10; // 設置調用條數
- $args = array(
- ‘post_password’ => ”,
- ‘post_status’ => ‘publish’, // 只選公開的文章.
- ‘post__not_in’ => array($post->ID),//排除當前文章
- ‘caller_get_posts’ => 1, // 排除置頂文章.
- ‘orderby’ => ‘comment_count’, // 依評論數排序.
- ‘posts_per_page’ => $post_num
- );
- $query_posts = new WP_Query();
- $query_posts->query($args);
- while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>
- <li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title //開源軟件:Vevb.com
- (); ?></a></li>
- <?php } wp_reset_query();?>
- </ul>
3、隨機文章的調用代碼:
- <ul>
- <?php
- global $post;
- $postid = $post->ID;
- $args = array( ‘orderby’ => ‘rand’, ‘post__not_in’ => array($post->ID), ‘showposts’
- => 10);
- $query_posts = new WP_Query();
- $query_posts->query($args);
- ?>
- <?php while ($query_posts->have_posts()) : $query_posts->the_post(); ?>
- <li><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php
- the_title_attribute(); ?>”><?php the_title(); ?></a></li>
- <?php endwhile; ?>
- </ul>
從上三種文章的代碼調用方法都測試過,其中最新文章和隨機文章的調用方法是沒有問題的,而熱門文章的調用代碼不知道為什么不能正常顯示,但是刪除了post_password’ => ”,這句代碼就可以正常調用,不知道有沒有那位大神可以留言告訴我原因,還有熱門文章的調用,如果想依瀏覽數來排序又是怎么實現的呢?期高手的出現.
新聞熱點
疑難解答
圖片精選