纯代码为WordPress添加相关文章的几种方法

修改主题文件前记得先备份,有问题再还原。

1、在主题函数文件functions.php里面添加下面的内容

/**
* Related posts
*
* @global object $post
* @param array $args
* @return
*/
function wcr_related_posts($args = array()) {
global $post;

// default args
$args = wp_parse_args($args, array(
'post_id' => !empty($post) ? $post->ID : '',
'taxonomy' => 'category',
'limit' => 3,
'post_type' => !empty($post) ? $post->post_type : 'post',
'orderby' => 'date',
'order' => 'DESC'
));

// check taxonomy
if (!taxonomy_exists($args['taxonomy'])) {
return;
}

// post taxonomies
$taxonomies = wp_get_post_terms($args['post_id'], $args['taxonomy'], array('fields' => 'ids'));

if (empty($taxonomies)) {
return;
}

// query
$related_posts = get_posts(array(
'post__not_in' => (array) $args['post_id'],
'post_type' => $args['post_type'],
'tax_query' => array(
array(
'taxonomy' => $args['taxonomy'],
'field' => 'term_id',
'terms' => $taxonomies
),
),
'posts_per_page' => $args['limit'],
'orderby' => $args['orderby'],
'order' => $args['order']
));

include( locate_template('related-posts-template.php', false, false) );

wp_reset_postdata();
}

2、新建一个related-posts-template.php文件,添加以下内容:

<?php if (!empty($related_posts)) { ?>
<div class="related-posts">
<h3 class="widget-title"><?php _e('相关文章', ''); ?></h3>

<ul class="related-posts-list">
<?php
foreach ($related_posts as $post) {
setup_postdata($post);
?>
<li>
<a class="title" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php if (has_post_thumbnail()) { ?>
<div class="thumb">
<?php echo get_the_post_thumbnail(null, 'medium', array('alt' => the_title_attribute(array('echo' => false)))); ?>
</div>
<?php } ?>
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php } ?>
</ul>
<div class="clearfix"></div>
</div>
<?php
}

另一个版本:注意这里的 target=”_blank” 添加到了a标签中还添加了 rel=”noopener” 为安全起见

<!--相关文章代码-->
<?php if (!empty($related_posts)) { ?>
    <div class="related-posts" style="margin: 20px 0;">
        <h3 class="widget-title"><?php _e('相關文章', ''); ?></h3>

        <ul class="related-posts-list" style="margin-top: 20px; padding: 0; list-style-type: none;">
            <?php
            foreach ($related_posts as $post) {
                setup_postdata($post);
            ?>
            <li style="margin-bottom: 10px;">
                <!-- 注意这里的 target="_blank" 添加到了a标签中还添加了 rel="noopener" 为安全起见-->
                <a class="title" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" target="_blank"  rel="noopener" style="text-decoration: none;">
                    <?php if (has_post_thumbnail()) { ?>
                    <div class="thumb" style="margin-bottom: 10px;">
                        <?php echo get_the_post_thumbnail(null, 'medium', array('alt' => the_title_attribute(array('echo' => false)))); ?>
                    </div>
                    <?php } ?>
                    <h4 style="margin: 0 0 5px; padding: 0;"><?php the_title(); ?></h4>
                </a>
            </li>
            <?php } ?>
        </ul>
        <div class="clearfix" style="clear: both;"></div>
    </div>
<?php } ?>

调用方法
如果要调用相关文章代码,只需要在你主题合适的位置,通常是添加到single.php文件里面。

1、显示3个相关文章内容代码

<?php wcr_related_posts(); ?>

显示5条这样写,数字5是要显示的文章数量

<?php wcr_related_posts( array( 'limit' => 5 ) ); ?>

这种相关文章是根据分类来展示的。

2、根据文章TAG来展示相关文章

<?php
wcr_related_posts(array(
'taxonomy' => 'post_tag',
'limit' => 6
));
?>

数字6自行修改,为展示文章数量。

3、根据分类的热门评论文章展示

<?php
wcr_related_posts(array(
'limit' => 6,
'orderby' => 'comment_count',
'order' => 'ASC'
));
?>

4、根据特定文章的TAG展示相关文章

<?php
wcr_related_posts(array(
'limit' => 6,
'taxonomy' => 'post_tag',
'post_id' => 145
));
?>

最后,你可能还需要自己修改下css样式让相关文章更好看。

 

还有其他一些方法:https://blog.naibabiji.com/skill/wordpress-xiang-guan-wen-zhang.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注