WordPress topology gets a category

Hope it will be easy for you to fix something!

I have a custom post type called 'products'.

Using this code, I get the name of each "product":

    <?php 

//Define your custom post type name in the arguments

$args = array('post_type' => 'products');

//Define the loop based on arguments

$loop = new WP_Query( $args );

//Display the contents

while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="entry-title"><?php the_title(); ?></h1>




<?php endwhile;?>

      

Each of these products is within a category. I want to get not only the_title but also the category.

I tried to use <?php the_category(); ?>

but no luck.

Does anyone have a key?

EDIT:

Sorry, I have these created inside my custom post type.

    add_action( 'init', 'create_product_cat_external' );

function create_product_cat_external() {
    register_taxonomy(
        'ExternalProducts',
        'products',
        array(
            'label' => __( 'External Products' ),
            'rewrite' => array( 'slug' => 'externalproducts' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'create_product_cat_internal' );

function create_product_cat_internal() {
    register_taxonomy(
        'InternalProducts',
        'products',
        array(
            'label' => __( 'Internal Products' ),
            'rewrite' => array( 'slug' => 'internalproducts' ),
            'hierarchical' => true,
        )
    );
}

      

which give me external and internal products as categories. But inside them, I have categories that were done on the wordpress backend.

This is what it looks like when I select a product:

backend

+3


source to share


2 answers


This is a little tricky because you have linked two different taxonomies to your post products. Since your taxonomy is hierarchical, it would be easier to put everything in one taxonomy with "Internal Products" and "External Products" at the top level. In this case, you will need to check both of them - I suppose your "products" cannot be "internal" and "external" at the same time:

<?php 
// Your query
$args = array('post_type' => 'products');
$loop = new WP_Query( $args );
// Your loop
while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="entry-title"><?php the_title(); ?></h1>

// Only print terms when the_terms is not false
<?php if (the_terms( $post->ID, 'InternalProducts') !== false) : ?>
<p><?php the_terms( $post->ID, 'InternalProducts', 'Category: Internal Products ', ' / ') ?></p>
<?php endif; ?>

<?php if (the_terms( $post->ID, 'ExternalProducts') !== false) : ?>
<p><?php the_terms( $post->ID, 'ExternalProducts', 'Category: External Products ', ' / ') ?></p>
<?php endif; ?>

<?php endwhile;?>

      



See WordPress Codex for details .

+1


source


Try this code:

<?php 

//Define your custom post type name in the arguments

$args = array('post_type' => 'products');

//Define the loop based on arguments

$loop = new WP_Query( $args );

//Display the contents

while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php
$terms = get_the_terms($post->ID, 'Your Taxonomy');

foreach($terms as $term) {
     echo $term->slug;
     echo $term->name;
     echo $term->term_id;
}

endwhile;
?>

      



I have shown three values ​​for your convenience. Hope this helps you.

0


source







All Articles