0

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;
}
1

1 Answer 1

0

Use this line of code for listing categories.

   $terms_list = get_terms(array('taxonomy'=>'category', 'hide_empty'=>FALSE, 'parent'=>0));
   print_r($terms_list);
9
  • thanks for your answer. i tryed to substitute my code: $terms = get_the_terms( $post->ID, 'category', 'orderby=count&hide_empty=0&parent=0'); with your code: $terms_list = get_terms(array('taxonomy'=>'category', 'hide_empty'=>FALSE, 'parent'=>0)); print_r($terms_list); ---> than i git an error... where i have to change my code with yours?
    – tom84
    Commented Jan 25, 2017 at 12:25
  • What was the error can you please share that?
    – BlueSuiter
    Commented Jan 25, 2017 at 12:58
  • i got this in the frontend: Array ( [0] => WP_Term Object ( [term_id] => 1 [name] => Allgemein [slug] => allgemein [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw WordPress Development Meta => Array ( ) ) [1] => WP_Term Object ( [term_id] => 7 [name] => Angelmethoden [slug] => angelmethoden [term_group] => 0 [term_taxonomy_id] => 7 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw WordPress Development Meta => Array ( ) ) [2] => WP_Term Object ( [term_id] => 9 .....and so on
    – tom84
    Commented Jan 25, 2017 at 13:20
  • Remove print_r from the code and then run it...
    – BlueSuiter
    Commented Jan 25, 2017 at 13:48
  • did it... now nothing happens... i did not show any posts
    – tom84
    Commented Jan 25, 2017 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.