i have a code which shows random posts from the same categories on the category-page. I use child and parent categories and i only want so show post from the same child-category or the same parent-category. At the Moment the code shows both (posts from parent and child-category) on the child-category-page. I didn't find a solution to fix this problem. I tried with
get_terms( array('parent' => 0 ) );
and also other ways. can somebody help me?
/*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;
}