List categories of a custom post

Asked

Viewed 1,603 times

1

I have a Custom post called 'Projects'. On certain pages I need to display only the 'Projects' that have the 'apps' category. So far so good, it displays in a good way. However, when I add tags to this project, I can’t just list the tags for that particular post. So tbm as I can not list all the categories that the post contains. In the code below it returns all categories, all, including those not linked to the post

<?php
      query_posts(
        array(
          'post_type' => 'projects',
          'category_name' => 'apps',
          'showposts' => 3,
          'orderby' => 'date'
          )
        );
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php
       $categories = get_categories($postID);
       var_dump($categories);
?>

1 answer

0


You can use the method wp_get_object_terms() to rescue both the tags as to the Categories, since both are taxonomias. In a minimalist way, it is possible to do

<?php

    if(have_posts()){
        while(have_posts()){
            the_post();

            $tags = wp_get_object_terms( $postID , 'post_tag' );

            var_dump($tags);

            $categories = wp_get_object_terms( $postID , 'category' );

            var_dump($categories);
        }
    }

For the purposes of curiosity, your use of the method get_categories() is incorrect. It returns a list of category objects (hence the error you find), and does not receive the $postID as an argument.

  • 1

    Thank you very much man, I used that same should have come here and closed the post, but that’s right. Valeus

Browser other questions tagged

You are not signed in. Login or sign up in order to post.