首页 > 解决方案 > 获取作者在空间分类中的帖子

问题描述

我想在特定类别中获取作者的帖子并将其显示在作者页面中我尝试了此代码但它不起作用,这是什么问题?我的代码:

<?php
$url= get_site_url();
$author_id = get_the_author_meta('ID');
$args = array(
    'author' => $author_id,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'cat' => '161,181,157',
            'post_count' => '3',
        ),
    ),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
    $query->the_post();
    $postid = get_the_ID();
    echo '<a href='. $url .'?p='. $postid .'>' . get_the_title() . '</a>';
}
?>

标签: phpwordpress

解决方案


试试下面的代码。

$url = get_site_url();
$author_id = get_the_author_meta('ID');
$args = array(
    'author' => $author_id,
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array(161,181,157)
            
        ),
    ),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
    $query->the_post();
    $postid = get_the_ID();
    echo '<a href='. $url .'?p='. $postid .'>' . get_the_title() . '</a>';
}

推荐阅读