網(wǎng)頁制作Webjx文章簡介:在single文章頁使用相關(guān)文章功能的好處是顯而易見的,可以增加網(wǎng)站的粘度的同時(shí),更多地是更方便地為用戶列出了他可能關(guān)心的內(nèi)容。
在single文章頁使用相關(guān)文章功能的好處是顯而易見的,可以增加網(wǎng)站的粘度的同時(shí),更多地是更方便地為用戶列出了他可能關(guān)心的內(nèi)容。一般情況下我們是使用水煮魚的WordPress Related Posts插件來實(shí)現(xiàn)的,那么,在盡量節(jié)約插件的使用數(shù)量的前提下,我們還可以手動(dòng)添加代碼來實(shí)現(xiàn)。
1,相關(guān)文章非插件實(shí)現(xiàn)方法
<?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,創(chuàng)建相關(guān)文章的發(fā)布短代碼
把下面的代碼寫入到你的主題文件夾中的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');以后,你就可以在博客的任意位置插入下面的短代碼,來實(shí)現(xiàn)相關(guān)文章的調(diào)用顯示了。
[AD:施諾斯UV膠水去除氣泡的方法:膠水脫泡機(jī)]
[related_posts]
3,同一分類下的相關(guān)文章
實(shí)現(xiàn)了相關(guān)文章的調(diào)用后,有的人又希望列出的文章和原文是同一分類下的,那么如何實(shí)現(xiàn)呢?
首先,同樣把下面的代碼寫入的主題文件夾中的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; }}之后,調(diào)用自定義的函數(shù)fb_get_cat_related_posts就可以了,該函數(shù)包括3個(gè)參數(shù)
$limit (int) 定義顯示文章數(shù),int型數(shù)據(jù);
$catName (bool) 輸出分類名稱,bool型數(shù)據(jù)( TRUE or FALSE)
$title (string) String for a text before all entries
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選