首页 视频课程 主题开发课程第05章、文章输出 WordPress按作者查询文章操作方法

WordPress按作者查询文章操作方法

2023-06-11 / 231阅

要按作者查询WordPress文章,您可以使用WP_Query类。以下是示例代码,可以帮助您按照作者名称查询文章,并返回markdown格式:

$args = array(
    'author_name' => 'author_name_here',
    'post_type' => 'post',
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        $author = get_the_author();
        $title = get_the_title();
        $excerpt = get_the_excerpt();
        $permalink = get_permalink();

        $output = "## $title nn";
        $output .= "**Author:** $author nn";
        $output .= "$excerpt nn";
        $output .= "[Read More]($permalink) nn";

        echo $output;
    }
}

wp_reset_postdata(); 

在上面的代码中,替换author_name_here为您要查询的作者名。代码将循环遍历所有符合条件的文章,并生成markdown格式的输出。您可以根据需要自定义输出格式。

阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228

大家谈论
    我的见解
    目录