Forums

[resolved] Show empty category page instead of 404 error (11 posts)

  1. Ryan Paul
    Member
    Posted 4 years ago #

    Hello...

    I want to show an empty category page with 'there are no posts' if the category is empty instead of a 404 error.

    Can someone PLEASE help me?

  2. Ryan Paul
    Member
    Posted 4 years ago #

    I'm not sure if this is the same thing, but if I can't do that... is there a way of doing something like:

    if (is_category('3') && $posts == 0) { echo 'there are no posts'; }

    Can anyone help? I put this in a category template, and it still shows a 404 error on empty category.

  3. Ryan Paul
    Member
    Posted 4 years ago #

    I've tried putting the code for categories into a page template, and then putting that into a static page, but it shows the date, title, edit link and posted by info.

  4. moshu
    Member
    Posted 4 years ago #

    I've tried putting the code for categories into a page template,

    Don't do that. Pages are not for categories.
    For categories use Category_Templates

  5. Ryan Paul
    Member
    Posted 4 years ago #

    I've looked through the documentation, and can't find a way to do this. I want to cry :(

  6. moshu
    Member
    Posted 4 years ago #

    In my category.php the Loop ends like this:

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

    and that's the text displayed when a category is empty.
    (It's true, I don't have a 404 page in the theme folder)

  7. nexia
    Member
    Posted 4 years ago #

    if you want this situation to happen in all the 404 situations, you can simply use the 404.php and duplicate the file you want to replicate in it, like the archive.php or category.php depending on the theme you use...

  8. Ryan Paul
    Member
    Posted 4 years ago #

    moshu... I tried deleting the 404.php but the text I put there, doesn't show up, and nor does a php snippet.

    hi nexia

  9. nexia
    Member
    Posted 4 years ago #

    hi othellobloke, but did you understand my suggestion at least ???... i think i posted it wrong.. lol

  10. Ryan Paul
    Member
    Posted 4 years ago #

    I understood what you mean for sure, but that will give me that page on ANY 404 error.

  11. Ryan Paul
    Member
    Posted 4 years ago #

    I've found out how to fix a 404 error showing on empty category page.

    In wp_includes/classes.php look for the line that starts with:

    if ( (0 == count($wp_query->posts)) && !is_404() && !is_search()

    and add the following after !is_404():

    && !is_category()

    Then what I did was create category-1.php, category-2.php etc and put a custom message on each page.

Topic Closed

This topic has been closed to new replies.

About this Topic