主页/WordPress笔记/博客文章/综合文章/如何在 WordPress 中显示相同作者的相关文章

如何在 WordPress 中显示相同作者的相关文章

Bug编译狮

Bug编译狮

在WordPress中,你可以通过设置插件或编写自定义代码来实现显示相同作者相关文章的功能。以下是两种常见的方法:

方法一:使用默认的“相似作者”功能

  1. 登录到您的WordPress后台

  2. 导航到“编辑器”页面。

  3. 找到并打开您希望添加类似文章的页面模板(如“archive.php”)。

  4. 在适当的位置插入以下代码以启用相似作者功能:

<?php
$args = array(
    'post_type' => 'post',
    'author' => get_the_author_meta('ID'),
    'posts_per_page' => 5,
);
$query = new WP_Query($args);

if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();
        echo '<div class="related-article">';
        the_title('<h3>', '</h3>');
        echo '</div>';
    endwhile;
endif;

wp_reset_postdata();
?>

这段代码会从当前文章的作者开始查找最近的文章,并展示5篇文章。请根据需要调整$args中的参数。

示例效果:

当用户访问某个作者的文章页面时,页面上会自动显示该作者其他5篇相关文章。

注意事项:

  • 确保使用的是标准WordPress主题和模板文件,因为不同主题可能有差异。
  • 如果您不熟悉PHP,可以考虑使用一些免费的WordPress插件,如“Related Posts”或“Relevant Posts”,它们通常提供了更简单的方式来实现这一功能。

方法二:使用自定义代码

如果你想要完全控制相关文章的生成方式,可以创建一个自定义函数。例如,创建一个名为get_similar_posts()的函数:

  1. 创建一个新的PHP文件,比如functions.php,然后将以下代码粘贴进去:
function get_similar_posts($author_id, $num) {
    global $wpdb;
    $sql = "SELECT ID FROM {$wpdb->prefix}posts WHERE post_author = %d AND post_status = 'publish'";
    $results = $wpdb->get_results($wpdb->prepare($sql, $author_id), ARRAY_A);

    if (empty($results)) {
        return false;
    }

    // 获取所有相关作者的文章
    foreach ($results as $result) {
        $similar_posts[] = get_post($result['ID']);
    }

    // 根据数量限制返回文章数组
    return array_slice($similar_posts, 0, $num);
}
  1. 在您的主模板文件(如single.php)中调用这个函数:
<?php
$author_id = get_the_author_meta('ID');
$recent_posts = get_similar_posts($author_id, 5);

if (!empty($recent_posts)) {
    echo '<div class="related-posts">';
    foreach ($recent_posts as $post) {
        setup_postdata($post);
        echo '<article>';
        the_title('<h3><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></h3>');
        the_excerpt();
        echo '</article>';
    }
    wp_reset_postdata();
    echo '</div>';
}
?>

这种方法允许你根据具体的需要定制相关文章的显示方式。

这两种方法都可以帮助你在WordPress中有效地显示相同作者的相关文章。选择哪种方法取决于你的具体需求和偏好。

黑板前的Bug语法讲师

黑板前的Bug语法讲师

在 WordPress 中,你可以使用多种方法来显示相同作者的相关文章。以下是一些基本的方法和示例代码:

1. 使用标签和归档功能

原理:

  • 在文章编辑器中,点击“标签”或“归类”按钮。
  • 在出现的下拉菜单中选择一个类别或标签。

示例代码(高级):

<?php
/*
 * Example of using tags and archives.
 */
$categories = get_categories();
foreach ($categories as $category) {
    echo '<h2>' . $category->name . '</h2>';
    echo '<ul class="post-list">';
    $posts = get_posts(array(
        'cat' => $category->term_id,
        'numberposts' => -1, // Show all posts
        'order' => 'DESC',
        'orderby' => 'date'
    ));
    foreach ($posts as $post) {
        echo '<li><a href="' . esc_url(get_permalink($post->ID)) . '">' . get_the_title($post->ID) . '</a></li>';
    }
    echo '</ul>';
}
?>

2. 使用评论过滤器和回帖列表

原理:

  • 首先,在主题模板文件(通常是 functions.phpsingle.php)中添加一个过滤器以处理文章中的评论。
  • 然后,在主题样式表中创建一个 CSS 类用于显示带有相应作者名称的帖子。

示例代码(高级):

// Add a filter to the comments template for displaying author names in the comment list
add_filter( 'comments_template', function() {
    return apply_filters( 'author_list_comments_template', wp_kses_post( '<div id="comment-author" class="comment-author"><strong>' . esc_html( get_comment_author_link() ) . '</strong>: </div>' ) );
} );

// Apply filters to show the author's name on comments
function author_list_comments_template( $template ) {
    global $comment;
    if ( ! is_user_logged_in() || empty( $comment->user_id ) ) {
        return $template;
    }

    $author = get_userdata( $comment->user_id );
    if ( ! $author || ! isset( $author->display_name ) ) {
        return $template;
    }

    return sprintf( '<div id="comment-author" class="comment-author">%s: %s</div>', esc_html( $author->display_name ), esc_html( get_avatar( $author->ID, 30 ) ) );
}

// Example of using the author_list_comments_template filter
<div class="post">
    <div class="entry-meta">
        <?php if ( has_post_format('gallery') ) { ?>
            <span class="meta-nav">[<img src="<?php echo esc_url( get_theme_file_uri('/images/gallery.png') ); ?>" alt="">]</span>
        <?php } else { ?>
            <span class="meta-nav">[<img src="<?php echo esc_url( get_theme_file_uri('/images/avatar.png') ); ?>" alt="">]</span>
        <?php } ?>
    </div>
    <div class="content">
        <?php the_content(); ?>
    </div>
    <footer class="post-footer">
        <div class="entry-meta">
            <?php edit_post_link( __( 'Edit', 'your-plugin' ), '<span class="edit-link">', '</span>' ); ?>
        </div>
    </footer>
</div>

3. 使用嵌套查询和子类

原理:

  • 创建一个自定义函数来获取特定作者的所有文章。
  • 使用嵌套查询遍历这些文章并输出。

示例代码:

/**
 * Get posts by specific author ID
 *
 * @param int $author_id Author ID to get posts for.
 */
function get_posts_by_author( $author_id ) {
    $query_args = array(
        'post_type' => 'post',
        'posts_per_page' => -1, // Show all posts
        'orderby' => 'date',
        'order' => 'ASC',
        'author' => $author_id
    );

    $query = new WP_Query( $query_args );

    return $query;
}

// Example of getting posts by author ID
get_posts_by_author( 1 ); // This will display posts by the user with ID 1

通过以上方法,你可以轻松地在 WordPress 主题中显示相同作者的相关文章。这些建议适用于不同级别的开发者和用户。