WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] is_404() is true and have_posts() is also true?! (8 posts)

  1. AndreyPutilov
    Member
    Posted 1 year ago #

    Hello, I have developed my own theme with index.php file but without 404.php

    The problem is present when URL is like mysite.com/meaninglesstext, but everything is good, when URL is mysite.com/category/invalidpostname

    I am testing is_404() and have_posts() at the very beginning of index.php file, no code is included before, but both conditions turn out true;

    I have no self-made post types, I have turned off all plugins. Permalink settings are /%category%/%postname%/ . I tryed to change category and tag prefixes placing casual text, but no effect, though it is not important. Thanks in advance

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    is_404() won't work as it tests for a 404 page. If you just use
    if ( have_posts() ):

    then you can add your 404 text as part of an else: after the main Loop.

  3. AndreyPutilov
    Member
    Posted 1 year ago #

    Ok, I have created a 404.php file, and it solved my problem, but I am still interested, why have_posts returns true.

    Why the first case mysite.com/meaninglesstext produces a query, but another case mysite.com/category/invalidpostname does not.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    I am still interested, why have_posts returns true.

    Does it? Do you have an else: code block?

  5. AndreyPutilov
    Member
    Posted 1 year ago #

    shurely it does, it outputs a list of all new posts. It is not equal to the previous query

  6. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    If posts are being output after the Loop, then you have serious problems in that template file.

  7. AndreyPutilov
    Member
    Posted 1 year ago #

    No, it is not my problem. Look, I have created a clean index.php file with this code

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    The result is http://www.peeep.us/7b74626d

  8. AndreyPutilov
    Member
    Posted 1 year ago #

    Ok, this is a very strange behaviour but I am not looking forward to dealing with it cause a creation of 404.php file is an acceptable solution.

    In any case I want to thank esmi and all the rest because it helped me to sort out what is running in a wrong way and what is not.

Topic Closed

This topic has been closed to new replies.

About this Topic