最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

shortcode - How to retrieve category of a post in have_post loop?

matteradmin4PV0评论

I am using a CPT (post_type => 'portfolio_type', taxonomy => 'portfolio_cat'). Under this CPT, I have design, web development category. I am trying to get the post with the category. My best try,

function callback_for_portfolio_shortcode(){
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'portfolio',
            'field' => 'id',
            'terms' => $parent_cat
        )
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) :
        while ($query->have_posts()): $query->the_post();
            $post_id = get_the_ID();
            $post_categories = wp_get_post_categories( $post_id );
            $cat_str = '';

            //loop through the category and get a string of category
            foreach($post_categories as $c){
                $cat = get_category( $c );
                $cat_str .= $cat->name;
            }
            $html .= '<h4>'.$query->post->post_title.'</h4>'; //works fine 
            $html .= '<h4>'.substr(get_field('job_description', $id), 0, 100).'</h4>'; //works fine 
            $html .= '<h4>'.post_permalink().'</h4>'; //works fine 
            $html .= '<h4>'.$cat_str.'</h4>'; //does not work    
        endwhile;
    endif;
    return $html;
}

$cat_str is empty. What am I missing here? Thanks.

I am using a CPT (post_type => 'portfolio_type', taxonomy => 'portfolio_cat'). Under this CPT, I have design, web development category. I am trying to get the post with the category. My best try,

function callback_for_portfolio_shortcode(){
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'portfolio',
            'field' => 'id',
            'terms' => $parent_cat
        )
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) :
        while ($query->have_posts()): $query->the_post();
            $post_id = get_the_ID();
            $post_categories = wp_get_post_categories( $post_id );
            $cat_str = '';

            //loop through the category and get a string of category
            foreach($post_categories as $c){
                $cat = get_category( $c );
                $cat_str .= $cat->name;
            }
            $html .= '<h4>'.$query->post->post_title.'</h4>'; //works fine 
            $html .= '<h4>'.substr(get_field('job_description', $id), 0, 100).'</h4>'; //works fine 
            $html .= '<h4>'.post_permalink().'</h4>'; //works fine 
            $html .= '<h4>'.$cat_str.'</h4>'; //does not work    
        endwhile;
    endif;
    return $html;
}

$cat_str is empty. What am I missing here? Thanks.

Share Improve this question edited Apr 5, 2019 at 7:54 Abdullah Raihan Bhuiyan asked Apr 5, 2019 at 6:30 Abdullah Raihan BhuiyanAbdullah Raihan Bhuiyan 34 bronze badges 2
  • You use the wrong operator to concatenate strings, you should replace it with .=. $cat_str .= $cat->name; – nmr Commented Apr 5, 2019 at 7:04
  • just a typo mistake. sorry! – Abdullah Raihan Bhuiyan Commented Apr 5, 2019 at 7:54
Add a comment  | 

1 Answer 1

Reset to default 3

wp_get_post_categories() and get_category() are specifically for the category taxonomy. You're using a custom taxonomy, portfolio_cat, so you need to use get_the_terms() to get categories.

$categories = get_the_terms( $post_id, 'portfolio_cat' );
$cat_str    = '';

foreach ( $post_categories as $category ) {
    $cat_str .= $category->name;
}

If you just want a comma separated list of category links, you can use get_the_term_list() to simplify the code:

$cat_str = get_the_term_list( $post_id, 'portfolio_cat', '', ', ', '' );

Or, if you don't want links, you can do this instead:

$categories = get_the_terms( $post_id, 'portfolio_cat' );
$cat_str    = implode( ', ', wp_list_pluck( $categories, 'name' ) );

Also, instead of $query->post->post_title you should really just be using get_the_title();.

Post a comment

comment list (0)

  1. No comments so far