我有一个代码,在分类页面上显示来自相同类别的随机帖子。我使用子类别和父类别,我只想显示来自同一子类别或同一父类别的帖子。目前,代码在子类别页面上同时显示(来自父类别和子类别的帖子)。我没有找到解决这个问题的方法。我试过了
get_terms( array(\'parent\' => 0 ) );
还有其他方式。谁能帮帮我吗?
/*recent posts*/
function my_posts_from_category( $atts, $content = null ) {
extract(shortcode_atts(array(
\'count\' => 5,
\'verbose\' => \'0\' // \'1\' outputs some debug info
), $atts));
$verbose = ( $verbose !== \'0\' );
$content = \'\';
$current_categories = array();
global $post;
if ( !empty( $post ) ) {
// get all categories of current post
$terms = get_the_terms( $post->ID, \'category\', \'orderby=count&hide_empty=0&parent=0\');
if ( $terms && !is_wp_error( $terms ) ) {
foreach( $terms as $term ) {
/*if ($_term->parent == 0) //check for parent terms only*/
$current_categories[] = $term;
}
}
} else {
if ( $verbose ) $content = \'posts_from_category: no $post...\';
}
foreach ( $current_categories as $category ) {
// get all tags of current post
$found_terms = array();
$terms = get_the_terms( $post->ID, \'post_tag\' );
if ( !is_wp_error( $terms ) && !empty( $terms ) ) {
foreach ( $terms as $term ) {
$found_terms[] = $term->term_id;
}
}
// list of ids to exclude, first entry: current post
$my_exclude_ids = array( $post->ID );
// ids of related posts of current post
$my_related_post_ids = array();
foreach ( $found_terms as $found_term ) {
// get one post with found tag ordered by random
$args = array(
\'posts_per_page\' => 1,
\'orderby\' => \'rand\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_tag\',
\'field\' => \'id\',
\'terms\' => $found_term
)
),
\'suppress_filters\' => false,
\'fields\' => \'ids\',
\'post__not_in\' => $my_exclude_ids // exclude already found posts
);
$my_related_post_id = get_posts( $args ); // actually max. 1 post
if ( !empty( $my_related_post_id ) ) {
$my_related_post_ids = array_merge( $my_related_post_ids, $my_related_post_id );
$my_exclude_ids = array_merge( $my_exclude_ids, $my_related_post_id );
}
}
// get all post ids of current category ordered by date
$args = array(
\'posts_per_page\' => -1,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'id\',
\'terms\' => $category->term_id
)
),
\'suppress_filters\' => false,
\'fields\' => \'ids\',
\'post__not_in\' => $my_exclude_ids // exclude current post
);
$my_post_ids = get_posts( $args );
// result could be empty if all found were also related before
// combine results
$my_post_ids = array_merge( $my_post_ids, $my_related_post_ids );
// mix results in random order, todo, array_splice(len=0)?
shuffle( $my_post_ids );
$posts_lis = array();
foreach ( $my_post_ids as $my_post_id ) {
// add a post
$posts_lis[ \'id_\' . $my_post_id ] = \'<li><a href="\' . get_permalink( $my_post_id ) . \'" title="\' . esc_attr( get_the_title($my_post_id ) ) . \'">\' . get_the_title($my_post_id ) . \'</a> (\' . get_the_date( get_option( \'date_format\' ), $my_post_id ) . \')</li>\';
// limit output
if ( count( $posts_lis ) >= $count ) {
break;
}
}
// build html container
$content .= \'<div class="posts-from-category posts-from-category-\' . $category->slug . \'">\';
$content .= \'<p>Bei Lesern der Kategorie <a href="\' . esc_url( get_term_link( $category->slug, \'category\' ) ) . \'">\' . $category->name . \'</a> ebenfalls beliebt:</p>\';
$content .= \'<ul>\';
$content .= implode( $posts_lis );
$content .= \'</ul>\';
$content .= \'</div>\';
}
return $content;
}