简体   繁体   中英

Wordpress category.php returning empty

So I'm trying to create a Wordpress Category landing page (category.php) that lists all the articles for a specific category. For some reason, my category.php loop is returning empty (even though articles do exist in the category I clicked on). I've checked a ton of tutorials on how to create this page and my code matches them all, so not sure why this is happening.

Side note, the way my Wordpress is set up is that there are categories under each Custom Post Types.

Any help? Thank you!

<?php get_header(); ?>

<div class="container-fluid cat-landing">

<!--Logic for Category Listings-->

    <div class="row">
        <?php 
        // Check if there are any posts to display
        if ( have_posts() ) : ?>

        <h2 class="title"><?php single_cat_title(); ?> Category</h2>
        <?php
        // The Loop  
         while ( have_posts() ) : the_post();
         ?>
            
         <div class="col-md-3 latest-post post-listing">
                
            <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
            <a href="<?php the_permalink() ?>" class=""><img src="<?php echo $url ?>" /></a>
            
            <div class="desc">
        
                <a href="<?php the_permalink() ?>"><?php the_title();?></a>
            
            </div>
        
        </div>          

        
        <?php endwhile; wp_reset_query(); ?> 
        <?php endif; ?> 
        
        
    </div>  

The following apply to categories also, as both (taxonomies, categories, tags...) uses the same principle. I've made an extensive post about that here, don't hesite to take a look.


In your case when you search for ./fruits/apples you get all posts related to the term apples either displayed on either taxonomy.php or on taxonomy-fruits.php or finally on taxonomy-fruits-aples.php . But what if you want to access ./fruits/ ? Why does accessing fruit gives back a 404.php error? Well because taxonomy.php was intended to displays posts against terms, not posts against taxonomies. This is the normal and intended behaviour.

Source @ Why am I getting a 404 error when trying to reach the taxonomy.php page without any terms, (Wordpress don't see taxonomy page)

EDIT 1.1: I've done testing locally, you're code seems to be running fine on my end.

tht_title(); don't need to be echo out as by default echo is set to true https://developer.wordpress.org/reference/functions/the_title/

Default category uses archive.php as main fallback, you should check if you have an archive file. You can have a look at the template hierarchy here https://developer.wordpress.org/themes/basics/template-hierarchy/ . If a blank page is returned and that you can still access the .fruit/apples category term, then you probably have no post registered in that category.

Basic loop:

<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();

echo the_title().'<br/>';

endwhile; else:

echo 'No posts were found!';

endif; ?>

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