網頁制作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
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答
圖片精選