国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 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編程

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

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

    圖片精選

    主站蜘蛛池模板: 东乡族自治县| 汾阳市| 呈贡县| 汤阴县| 北辰区| 桦南县| 大新县| 都安| 乌鲁木齐县| 竹溪县| 珠海市| 普兰县| 上思县| 枞阳县| 长春市| 福泉市| 密山市| 乡城县| 潞城市| 马龙县| 洛扎县| 明星| 图木舒克市| 伊吾县| 武城县| 贵定县| 文昌市| 江陵县| 清苑县| 讷河市| 襄汾县| 商洛市| 弋阳县| 新沂市| 天峻县| 安达市| 阳原县| 积石山| 尼玛县| 文山县| 苍山县|