我正在尝试构建一个短代码,允许用户选择显示的类别和显示的帖子数量。在我添加“tax\\u查询”之前,该短代码运行良好
add_shortcode( \'latest_post\', \'latest_post_query_shortcode\' );
function latest_post_query_shortcode( $atts ) {
$atts = shortcode_atts( array(
\'post_per_page\' => \'\',
\'category\' => \'\',
), $atts );
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'posts_per_page\'=> 3,
\'tax_query\' => array( array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $categories
), ),
);
$string = \'\';
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) { ?>
<section class="recent-posts clear">
<?php while ( $query->have_posts() ) : $query->the_post() ; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( \'left\' ); ?>>
<? echo \'<a href="\' . get_permalink( $_post->ID ) . \'" title="\' . esc_attr( $_post->post_title ) . \'">\';
echo get_the_title( $_post->ID);
echo \'</a>\';
echo \'<a href="\' . get_permalink( $_post->ID ) . \'" title="\' . esc_attr( $_post->post_title ) . \'">\';
echo get_the_post_thumbnail( $_post->ID, \'medium\' );
echo \'</a>\';
echo \'<a href="\' . get_permalink( $_post->ID ) . \'" title="\' . esc_attr( $_post->post_title ) . \'">\';
echo \'<button>Read More</button>\';
echo \'</a>\';
?>
</article>
<?php endwhile; ?>
</section>
<?php } else {
echo \'No posts found\';
}
/* Restore original Post Data */
wp_reset_postdata();
}
在添加tax\\u查询之前,它将显示三个最新的帖子,而tax\\u查询只显示“找不到帖子”我做错了什么?