麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網站 > CMS建站 > 正文

實現WordPress文章頁面的相關文章列表代碼

2020-03-22 16:51:56
字體:
來源:轉載
供稿:網友
  • 網頁制作Webjx文章簡介:在single文章頁使用相關文章功能的好處是顯而易見的,可以增加網站的粘度的同時,更多地是更方便地為用戶列出了他可能關心的內容。

    在single文章頁使用相關文章功能的好處是顯而易見的,可以增加網站的粘度的同時,更多地是更方便地為用戶列出了他可能關心的內容。一般情況下我們是使用水煮魚的WordPress Related Posts插件來實現的,那么,在盡量節約插件的使用數量的前提下,我們還可以手動添加代碼來實現。

    1,相關文章非插件實現方法


    <?php//for use in the loop, list 5 post titles related to first tag on html' target='_blank'>current post$tags = wp_get_post_tags($post->ID);if ($tags) { echo 'Related Posts'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>5, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php endwhile; }}?>

    2,創建相關文章的發布短代碼
    把下面的代碼寫入到你的主題文件夾中的function.php中

    function related_posts_shortcode( $atts ) { extract(shortcode_atts(array( 'limit' => '5', ), $atts));  global $wpdb, $post, $table_prefix;  if ($post->ID) { $retval = '<ul>'; // Get tags $tags = wp_get_post_tags($post->ID); $tagsarray = array(); foreach ($tags as $tag) { $tagsarray[] = $tag->term_id; } $tagslist = implode(',', $tagsarray);  // Do the querywww.it165.net $q = "SELECT p.*, count(tr.object_id) as count FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < NOW() GROUP BY tr.object_id ORDER BY count DESC, p.post_date_gmt DESC LIMIT $limit;";  $related = $wpdb->get_results($q); if ( $related ) { foreach($related as $r) { $retval .= ' <a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).''; } } else { $retval .= ' No related posts found'; } $retval .= ''; return $retval; } return;}add_shortcode('related_posts', 'related_posts_shortcode');

    以后,你就可以在博客的任意位置插入下面的短代碼,來實現相關文章的調用顯示了。

    [AD:施諾斯UV膠水去除氣泡的方法:膠水脫泡機]

    [related_posts]

    3,同一分類下的相關文章
    實現了相關文章的調用后,有的人又希望列出的文章和原文是同一分類下的,那么如何實現呢?

    首先,同樣把下面的代碼寫入的主題文件夾中的function.php中

    /** * related post with category * @param: int $limit limit of posts * @param: bool $catName echo category name * @param: string $title string before all entries * Example: echo fb_cat_related_posts(); */if ( !function_exists('fb_get_cat_related_posts') ) { function fb_get_cat_related_posts( $limit = 5, $catName = TRUE, $title = '<h3>Recent Pages</h3>' ) {  if ( !is_single() ) return;  $limit = (int) $limit; $output = ''; $output .= $title;  $category = get_the_category(); $category = (int) $category[0]->cat_ID;  if ( $catName ) $output .= __( 'Kategorie: ' ) . get_cat_name($category) . ' ';  $output .= '<ul>';  $args = array( 'numberposts' => $limit, 'category' => $category, );  $recentposts = get_posts( $args ); foreach($recentposts as $post) { setup_postdata($post); $output .= '<a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . ''; }  $output .= '';  return $output; }}

    之后,調用自定義的函數fb_get_cat_related_posts就可以了,該函數包括3個參數

    $limit (int) 定義顯示文章數,int型數據;
    $catName (bool) 輸出分類名稱,bool型數據( TRUE or FALSE)
    $title (string) String for a text before all entries

    PHP編程

    鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表

    圖片精選

    主站蜘蛛池模板: 欧美成人三级大全 | 斗破苍穹在线免费 | 久夜tv| 久久成人午夜视频 | 精品乱码久久久久 | 亚洲一区二区三区高清视频 | 久久我不卡 | 国产亚洲精品yxsp | 亚洲一区二区三区精品在线观看 | 久久久久久久久亚洲精品 | 久久久入口 | 成人毛片免费播放 | 欧美视频国产精品 | 久草在线资源观看 | 一区二区三区视频在线观看 | 精品国产专区 | 色羞羞 | 草莓福利视频在线观看 | 毛片免费在线观看 | 久久精品观看 | 成人三区四区 | 亚洲特黄 | 特级黄色一级毛片 | 久久久久久免费 | 在线高清中文字幕 | 欧美日韩成人一区二区 | 久久精品欧美电影 | 精精国产xxxx视频在线野外 | 男女做性免费网站 | 成人在线视频免费观看 | 国产精品999在线 | 免费看一级片 | 九一免费在线观看 | 国产精品aⅴ | 久久国产精品久久久久久 | 精品国产91久久久久久久妲己 | 久久久一区二区精品 | 久久久国产精品成人免费 | 久久综合给合久久狠狠狠97色69 | 欧美a∨一区二区三区久久黄 | 久成人 |