phpwordpress

WordPress if 404 title


I have a WordPress plugin that has the following code inside header.php:

<?php
    if (the_subtitle("","", false) == "") {
        the_title();
    } elseif(is_404()) {
        echo "404";
    } else {
        the_subtitle();
    }
?>

Basically, this should happen:

  1. If subtitle is present, echo subtitle.
  2. If no subtitle is present, echo title.
  3. If 404, echo "404".

But for some reason, when I locate my 404.php page, there is nothing displayed. Why?


Solution

  • If your first check is for subtitle and your 404-page has no subtitle then that part of the if-statement is triggered and all other checks are skipped. By performing the 404 check first things should work as expected.

    <?php
        if (is_404()) {
            echo "404";
        } elseif (the_subtitle("","", false) == "") {
            the_title();
        } else {
            the_subtitle();
        }
    ?>