我正在尝试打印所有类别的所有帖子,但此时要在类别内显示的帖子将打印所有类别下的所有帖子。这是代码。您可以使用快捷码[bpo\\U microsoft\\U快捷码]。提前感谢:
<?php
function Custom_Post_Type_Grouped_By_Category(){
global $post;
$cat_args = array(
\'orderby\' => \'term_id\',
\'order\' => \'ASC\',
\'hide_empty\' => true,
);
$terms = get_terms(\'Custom Post Type Reg\', $cat_args);
$content = \' \';
$content .= \'<ul >\';
foreach($terms as $taxonomy){
$term_slug = $taxonomy->slug;
$cat_id = $taxonomy->term_id;
$tax_post_args = array(
\'post_type\' => \'Custom Post Type\',
\'posts_per_page\' => 999,
\'order\' => \'ASC\',
\'numberposts\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'Custom Post Type Reg\',
\'field\' => \'slug\',
\'terms\' => $term_slug,
\'id\' => $cat_id
),
)
);
$content .= \'<li >\';
$content .= \'<div >\';
$content .= $taxonomy->name;
$the_query = null;
$the_query = new WP_Query( $tax_post_args );
if ( $the_query->have_posts() ) {
$content .= \'<ul >\';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$content .= \'<li><a href="\' . get_the_permalink() .\'" rel="bookmark">\' . get_the_title() .\'</a></li>\';
}
$content .= \'</ul>\';
}
wp_reset_postdata();
$content .= \'</div>\';
$content .= \'</li>\';
} //end foreach loop
$content .= \'</ul>\';
return $content;
}
add_shortcode(\'shortcode\', \'Custom_Post_Type_Grouped_By_Category\');
?>