怎么調用最新,隨機,熱門,指定分類文章? – wordpress開發

    訪客看完文章后,如果我們在wordpress的文章頁底部或是側邊欄給訪客提供這些內容,就能留下更多訪客,降低跳出率

    在開發wordpress主題的時候,我們需要考慮到的一點是,訪客看完這篇文章后是不會直接就走掉的,而是會看看有沒有類似的或是相關的文章,如果這個時候我們在文章頁的底部或是側邊欄給訪客提供這些內容,就能讓訪客在網站里待的更久,大大降低跳出率,這樣對SEO也是很有好處的。

    WP 文章調用方法,包括調用最新,指定分類,隨機,熱文等代碼,經測試(Muze也測試了下),支持當前最新版的WordPress,這里我把代碼記錄下來,方便自己日后查看,也給大家參考之用。

    調用最新文章

    <?php query_posts('showposts=6&cat=-111'); ?>  <!--顯示篇數和排除分類-->
    <ul>  
    <?php while (have_posts()) : the_post(); ?>  
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
    <?php endwhile;?>  
    </ul>

    調用指定分類文章

    <ul>
    <?php
        $args=array(
            'cat' => 1,   // 分類ID
            'posts_per_page' => 10, // 顯示篇數
        );
        query_posts($args);
        if(have_posts()) : while (have_posts()) : the_post();
    ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        </li>
    <?php  endwhile; endif; wp_reset_query(); ?>
    </ul>

    調用整站隨機文章

    <ul>
    <?php
    $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    調用同分類隨機文章

    <ul>
    <?php
    $cat = get_the_category();
    foreach($cat as $key=>$category){
    $catid = $category->term_id;}
    $args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid ); // 顯示篇數
    $query_posts = new WP_Query();
    $query_posts->query($args);
    while ($query_posts->have_posts()) : $query_posts->the_post();?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;?>
    <?php wp_reset_query(); ?>
    </ul>

    調用整站熱門文章(按評論數)

    <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(); ?>"><?php the_title(); ?></a></li>
    <?php } wp_reset_query();?>
    </ul>

    但以上函數好像只能獲取到日志的標題和鏈接,如果能獲取到圖片就好了。

    如果你還需要一些統計功能,那么還可以看看這篇wordpress開發文章:

    統計WordPress分類目錄的瀏覽量 – wordpress開發

    如果只是想獲取上下文的相關信息的話,可以看看這篇文章:

    wordpress開發:獲取上下篇日志的鏈接、標題、特色圖像

    給TA贊賞
    共{{data.count}}人
    人已贊賞
    ??
    Npcink上的部份代碼及教程來源于互聯網,僅供網友學習交流,若您喜歡本文可附上原文鏈接隨意轉載。
    無意侵害您的權益,請發送郵件至 1355471563#qq.com 或點擊右側 私信:Muze 反饋,我們將盡快處理。
    ?
    購物車
    優惠劵
    搜索
    主站蜘蛛池模板: 无码视频一区二区三区在线观看| 亚洲综合一区国产精品| 国产精品一区二区电影| 日韩精品无码久久一区二区三| 一区高清大胆人体| 中文字幕一区二区区免| 国产一区二区三区不卡在线看| 久久久久人妻精品一区二区三区 | 日韩精品一区二区三区大桥未久| 中文精品一区二区三区四区| 久久久无码精品国产一区| 亚洲av无码天堂一区二区三区 | 无码人妻一区二区三区一| 国产精品一区二区久久不卡| 亚洲视频一区二区| 日韩一区二区三区视频| 国产伦精品一区二区三区视频小说| 中字幕一区二区三区乱码| 精品无码av一区二区三区| 亚洲一区综合在线播放| 亚洲国产国产综合一区首页| 免费一区二区三区| 日本不卡一区二区三区视频| 一本AV高清一区二区三区| 中文字幕一区日韩在线视频| 中文字幕一区二区三区在线播放 | 久久国产精品无码一区二区三区 | 视频一区二区精品的福利| 怡红院美国分院一区二区| 国产一区二区好的精华液| 日韩美一区二区三区| 精品女同一区二区| 日本一区二区三区精品国产| 亚洲毛片不卡av在线播放一区| 日韩电影在线观看第一区| 久久一区二区三区免费| 无码人妻一区二区三区免费n鬼沢| 无码人妻一区二区三区免费n鬼沢| 久久久精品人妻一区二区三区| 91在线精品亚洲一区二区| 免费一本色道久久一区|