简体   繁体   中英

Show custom post type taxonomy with URL in WordPress site

I am working in a WordPress site that has a custom post type of photos . I have added a custom taxonomy caller category . I want to show the custom category name with URL. But for some reason it is showing blank. How can I fix it? Here are my codes I am trying

<?php
$args = array(
    'post_type' => 'photos',
    'post_status' => 'publish',
    'paged' => get_query_var('paged')
);
$the_query = new WP_Query($args);

?>

<?php
if ($the_query->have_posts()):

    while ($the_query->have_posts()):
        $the_query->the_post();
        ?>

        <div>Meal type: <?php echo get_the_term_list($post->ID, 'category', '', ', ', ''); ?></div>                     

    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>

<?php else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

If your question is why the "div" tag in the while loop is appearing blank, that's due to the while loop's conditions not being met. One or more of your arguments are returning false.

<?php
$args = array(
    'post_type' => 'photos',
    'post_status' => 'publish',
    'paged' => get_query_var('paged')
);
$the_query = new WP_Query($args);

?>

<?php
if ($the_query->have_posts()):

    while ($the_query->have_posts()):
        $the_query->the_post();
        ?>

Essentially, WP_Query is calling multiple classes such as WP_Tax_Query and WP_Meta_Query and is doing an SQL request as follows based on the criteria you provided:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  
WHERE 1=1  AND wp_posts.post_type = 'photos' AND ((wp_posts.post_status = 'publish'))  
ORDER BY wp_posts.post_date DESC LIMIT 0, 10

It's searching to see if there are posts that fit the criteria that you specified, a post type of "photos" and a "published" status. I would assume that it's not finding these criteria and is returning with a value of false.

More information:

WP_QUERY

I think the issue is with your custom taxonomy name category , because category is already associated with post type post . So you are getting a blank output.

How to fix :: rename category to photos_cat (or with any name of your wish) .

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM