• Tags , , ,         
  • 2017-12-27  14:57:46        
  • 227 °C    

    这篇文章主要讲的是wordpress文章底部添加相关文章的方法。 为了查找方便,小A汇总了所有wordpress的相关教程,方便大家查找到自己想要的wordpress教程:  wordpress教程汇总


    wordpress几种添加相关文章的方法,主要是将代码添加进single.php的相关位置即可,简单容易操作。

    wordpress根据tag标签添加相关文章:

    
    <h3>相关文章</h3>
    <ul class="related_posts">
    <?php $post_num = 8; $exclude_id = $post->ID;
    $posttags = get_the_tags(); $i = 0;
    if ( $posttags ) {
     $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
     $args = array(
     'post_status' => 'publish',
     'tag__in' => explode(',', $tags),
     'post__not_in' => explode(',', $exclude_id),
     'caller_get_posts' => 1,
     'orderby' => 'comment_date',
     'posts_per_page' => $post_num,
     );
     query_posts($args);
     while( have_posts() ) { the_post(); ?>
     
    <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
     
     <?php $exclude_id .= ',' . $post->ID; $i ++;
     } wp_reset_query();
    }
    if ( $i < $post_num ) { $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
     $args = array(
     'category__in' => explode(',', $cats),
     'post__not_in' => explode(',', $exclude_id),
     'caller_get_posts' => 1,
     'orderby' => 'comment_date',
     'posts_per_page' => $post_num - $i
     );
     query_posts($args);
     while( have_posts() ) { the_post(); ?>
     
    <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
     
    <?php $i++;
     } wp_reset_query();
    }
    if ( $i == 0 ) echo '<li>没有相关文章!</li>';
    ?>
    </ul>
    
    

    具体效果可看文章页面下面的相关文章。

    wordpress根据分类category添加相关文章

    
    <ul id="cat_related">
    <?php
    global $post;
    $cats = wp_get_post_categories($post->ID);
    if ($cats) {
     $args = array(
     'category__in' => array( $cats[0] ),
     'post__not_in' => array( $post->ID ),
     'showposts' => 6,
     '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
     }
     }
     else {
     echo '<li>* 暂无相关文章</li>';
     }
     wp_reset_query();
    }
    else {
     echo '<li>* 暂无相关文章</li>';
    }
    ?>
    </ul>
    
    

    wordpress根据作者author添加相关文章

    
    <ul id="author_related">
    <?php
     global $post;
     $post_author = get_the_author_meta( 'user_login' );
     $args = array(
     'author_name' => $post_author,
     'post__not_in' => array($post->ID),
     'showposts' => 6, // 显示相关文章数量
     '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
     }
     }
     else {
     echo '<li>* 暂无相关文章</li>';
     }
     wp_reset_query();
    ?>
    </ul>
    

     

    本文内容来自:wordpress文章底部添加相关文章的方法 – Break易站


    —Author: Arvin Chen —Web Address: www.breakyizhan.com (Break易站)

     
    转载请保留页面地址:https://www.breakyizhan.com/wpress/1245.html