岑輝宇SEO博客:分享搜索引擎優化的心得和經驗,充當新手SEO道路上的照明燈,讓SEO不再沒方向,歡迎加入SEO交流群:241865235,微信公眾號:qq945748441(每天前兩名關注公眾號的朋友,免費分析網站)

如何調用wordpress相關文章

建站全套視頻 貴陽SEO 1524℃ 0評論

很多seoer都知道相關文章對一個網站優化的重要性,就拿貴陽seo這個網站來說側邊欄調用的是最新文章和隨機文章。其實就是采用的wordpress相關文章的調用,對于現在的草根站長來說制作一個符合自己喜歡的側邊欄還是有一定的難度的。今天岑輝宇給大家總結了三個wordpress相關文章調用的方法:
方法一:文章根據標簽相關(用SQL獲取)

相關原理:首先獲取改篇文章的所有標簽,接著獲取這些標簽下的 n 篇文章,那么這 n 篇文章就是與該文章相關的文章了。現在可以見到的WordPress相關文章插件都是使用的這個方法。這里我們通過SQL語句來直接讀取數據庫,隨機獲取 10篇相關的文章記錄。下面是實現的代碼:
<h3>該文章的相關文章</h3>
<ul>
<?php
$all_tags = wp_get_post_tags($post->ID);
if ($all_tags) {
$tag_list = ”;
foreach ($all_tags as $tag)
{
// 獲取標簽列表
$tag_list .= $tag->term_id.’,’;
}
$tag_list = substr($tag_list, 0, strlen($tag_list)-1);
$related_posts = $wpdb->get_results(”
SELECT post_title, ID
FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
WHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
AND ID = object_id
AND taxonomy = ‘post_tag’
AND post_status = ‘publish’
AND post_type = ‘post’
AND term_id IN (” . $tag_list . “)
AND ID != ‘” . $post->ID . “‘
ORDER BY RAND()
LIMIT 10″);
// 以上代碼中的 10 為限制只獲取10篇相關文章
// 通過修改數字 10,可修改你想要的文章數量
if ( $related_posts ) {
foreach ($related_posts as $related_post) {
?>
<li><a href=”<?php echo get_permalink($related_post->ID); ?>” rel=”bookmark” title=”<?php echo $related_post->post_title; ?>”>
<?php echo $related_post->post_title; ?></a></li>
<?php } } else { ?>
<li>暫無相關文章</li>
<?php } } ?>
</ul>

方法二:根據文章的分類獲取相關文章
本方法是通過獲取該文章的分類id,然后獲取該分類下的文章,來達到獲取相關文章的目的。我們用SQL語句來直接讀取數據庫,隨機獲取10篇相關文章記錄。下面是實現的代碼:
<h3>相關閱讀推薦</h3>
<ul>
<?php
$data = wp_get_post_categories($post->ID);
if ($data) {
$related = $wpdb->get_results(”
SELECT post_title, ID
FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id
AND {$wpdb->prefix}term_taxonomy.taxonomy = ‘category’
AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
AND {$wpdb->prefix}posts.post_status = ‘publish’
AND {$wpdb->prefix}posts.post_type = ‘post’
AND {$wpdb->prefix}term_taxonomy.term_id = ‘” . $data[0] . “‘
AND {$wpdb->prefix}posts.ID != ‘” . $post->ID . “‘
ORDER BY RAND()
LIMIT 10″);
if ( $related ) {
foreach ($related as $related_post) {
?>
<li><a href=”<?php echo get_permalink($related_post->ID); ?>” rel=”bookmark” title=”<?php echo $related_post->post_title; ?>”>
<?php echo $related_post->post_title; ?></a></li>
<?php } } else { ?>
<li>暫無相關文章</li>
<?php } }?>
</ul>

方法三:根據作者相關獲取文章(這個比較少用,因為基本都是我們自己發的)
該方法是獲取該文章作者的其他文章來充當相關文章,代碼如下:
<h3>該作者的相關文章</h3>
<ul>
<?php
$post_author = get_the_author_meta( ‘user_login’ );
$args = array(
‘author_name’ => $post_author,
‘post__not_in’ => array($post->ID),
‘showposts’ => 10, // 顯示相關文章數量
‘orderby’ => date, // 按時間排序
‘caller_get_posts’ => 1
);
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post(); update_post_caches($posts); ?>
<li><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>
<?php endwhile; else : ?>
<li>暫無相關文章</li>
<?php endif; wp_reset_query(); ?>
</ul>

轉載請注明:岑輝宇SEO博客 » 如何調用wordpress相關文章

喜歡 (2)or分享 (0)
發表我的評論
取消評論
表情
(29)個小伙伴在吐槽
  1. Los expertos creen que en momentos de estrés, la actividad en las secciones menos esenciales del cerebro, incluso en aquellas que controlan la excitación, comienza a disminuir. ?Sabes que es un simulacro? El hombre no debería mostrar ninguna postura demasiado grande de espera, dentro de la terapia de pareja, se puede obtener una mejora.
    匿名2019-06-22 12:15 回復
  2. hello, your website is really good. We do appreciate your give good results
    匿名2019-06-18 01:42 回復
  3. Thanks for the information, I rarely find what I’m looking for… finally an exception!
    匿名2019-06-17 07:53 回復
  4. Some times its a pain in the ass to read what blog owners wrote but this site is very user friendly ! .
    匿名2019-06-17 05:16 回復
  5. I simply want to tell you that I am very new to blogging and site-building and actually liked your web-site. Almost certainly I’m going to bookmark your site . You certainly have exceptional posts. Appreciate it for revealing your web site.
    learn this here now2019-06-13 02:49 回復
  6. Research suggests that these two conditions may share a similar pathogenesis, with similar types of immune cells and cytokines driving the diseases and common genetic risk factors. People with alopecia areata who have only a few patches of hair loss often experience a spontaneous, full recovery, without the need for treatment.
    匿名2019-05-31 04:59 回復
  7. Tener problemas de erección de vez en cuando no es necesariamente un motivo para preocuparse.
    匿名2019-05-30 04:06 回復
  8. La prevalencia de la impotencia sexual aumenta con la edad y con la presencia de otras enfermedades, como diabetes, hipertensión, enfermedades cardiacas, obesidades, etc.
    匿名2019-05-26 01:47 回復
  9. Los inhibidores de la fosfodiesterasa V no producen la erección, independientemente del deseo sexual, aunque intensifican la respuesta eréctil ante la estimulación sexual.
    匿名2019-05-25 04:44 回復
  10. aaa
    匿名2018-12-01 22:22 回復
1 2 3
福彩3d今日字谜