Search code examples
wordpress

Wordpress custom post loop - Cant get post categories


I can't seem to get the categories for a custom post in the loop I have as per the code below: Can anyone help me with what I am doing wrong please?

<?php
    $ids = array();
    $pages = get_pages("child_of=".$post->ID);
    if ($pages) {
        foreach ($pages as $page) {
            $ids[] = $page->ID;
        }
    }
    $paged = (get_query_var("paged")) ? get_query_var("paged") : 1;
    $args = array(
        "paged" => $paged,
        "post__in" => $ids,
        "posts_per_page" => 50000,
        "post_type" => "knowledge_capsules",
        "orderby" => "date",
        "order" => "DESC"
    );
    query_posts($args);
    if (have_posts()) : while (have_posts()) : the_post(); 
?>
    <div class="kcIndex_feedItemWrapper" id="postID<?php echo get_the_ID(); ?>">
         
    <!-- Need to add in the lightbox content here and fire it open on click -->    
        <a href="#" data-featherlight="#IssueExpandedContent_<?php echo get_the_ID(); ?>">
        <img src="<?php the_field('knowledgecapsule_teaser_thumbimg'); ?>" title="<?php the_title(); ?>" alt="<?php the_title(); ?>">
        <h4><?php the_title(); ?></h4>
        
              
            <div class="categoriesHere">
            <?php 
        $terms = get_the_terms( get_the_ID(), 'knowledge_capsules' );
        if(!empty($terms)){
        foreach($terms as $term){
            echo $term->name.'<br>';
        }
        }
        ?>
            </div>
            
         
            
        </a>
    
    <!-- Expanded content -->
    <div id="IssueExpandedContent_<?php echo get_the_ID(); ?>">        
        <?php the_field('knowledgecapsule_teaser_embeddcode'); ?>
        <p class="viewFullVideoLink"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">View the full video</a></p>
    </div>
        
    </div>
        
    
    

</div>


<?php endwhile; else: ?>
    <!--<h2>Oh No!!</h2>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>-->
<?php endif; ?>

The Custom post functions code is as below:

<?php

function prefix_create_custom_post_type() {
    /*
     * The $labels describes how the post type appears.
     */
    $labels = array(
        'name'          => 'Knowledge Capsules', // Plural name
        'singular_name' => 'Knowledge Capsule'   // Singular name
    );

    /*
     * The $supports parameter describes what the post type supports
     */
    $supports = array(
        'title',        // Post title
        'editor',       // Post content
        'excerpt',      // Allows short description
        'author',       // Allows showing and choosing author
        'thumbnail',    // Allows feature images
        'comments',     // Enables comments
        'trackbacks',   // Supports trackbacks
        'revisions',    // Shows autosaved version of the posts
        'custom-fields' // Supports by custom fields
    );

    /*
     * The $args parameter holds important parameters for the custom post type
     */
    $args = array(
        'labels'              => $labels,
        'description'         => 'Post type post knowledge capsule', // Description
        'supports'            => $supports,
        'taxonomies'          => array( 'category', 'post_tag' ), // Allowed taxonomies
        'hierarchical'        => false, // Allows hierarchical categorization, if set to false, the Custom Post Type will behave like Post, else it will behave like Page
        'public'              => true,  // Makes the post type public
        'show_ui'             => true,  // Displays an interface for this post type
        'show_in_menu'        => true,  // Displays in the Admin Menu (the left panel)
        'show_in_nav_menus'   => true,  // Displays in Appearance -> Menus
        'show_in_admin_bar'   => true,  // Displays in the black admin bar
        'menu_position'       => 5,     // The position number in the left menu
        'menu_icon'           => '/wp-content/themes/genesis-nal-new/includes/structure/KC_icon.svg',  // The URL for the icon used for this post type
        'can_export'          => true,  // Allows content export using Tools -> Export
        'has_archive'         => true,  // Enables post type archive (by month, date, or year)
        'exclude_from_search' => false, // Excludes posts of this type in the front-end search result page if set to true, include them if set to false
        'publicly_queryable'  => true,  // Allows queries to be performed on the front-end part if set to true
        'capability_type'     => 'post' // Allows read, edit, delete like “Post”
    );

    register_post_type('knowledge_capsules', $args); //Create a post type with the slug is ‘knowledge_capsules’ and arguments in $args.
}
add_action('init', 'prefix_create_custom_post_type');

?>

Solution

  • The problem is in your get_the_terms(). You are passing the post type name, when it should be the taxonomy registration.

    Since you are using the default WP category, you need to update your get_the_terms() to:

    <?php 
        $terms = get_the_terms( get_the_ID(), 'category' );
        
        if(!empty($terms)){
           foreach($terms as $term){
               echo $term->name.'<br>';
           }
        }
    ?>