1
votes

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>  
1
what url do you use to call this page? - Marcello Perri
Are you trying to access a category against a term in that category or simply the category page (as a category and posts index) - amarinediary
I'm trying to access a category against a term that's been clicked on. For example, if you were to go on the nytimes.com home page that shows all the latest articles, a category might be listed on top of the title. If you clicked that category, it would list all of the articles pertaining to that term. - evacarr
You need to echo the php tags contents. - Markus Zeller
If you get "No posts were found!" when you run the url "mywebsite.com/category/theactivecategory" that means that you don't have any posts connected with the category "theactivecategory", try using an existing category. - Marcello Perri

1 Answers

0
votes

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; ?>