Displaying page title issue

I have a WordPress app that displays a title depending on the page:

<?php
    if (is_404()) {
      echo "404. Page not found.";
    } elseif (is_page('46')) {
      echo "NOT WORKING";
    } elseif (the_subtitle("","", false) == "") {
        the_title();
    } else {
        the_subtitle();
    }
?>

Basically, on the specific page (page 46, which I’ve confirmed by printing out the page id), I want to display a specific title. But for some reason when I visit page 46, I get the title of the page instead, instead of “NOT WORKING”?

Related posts

Leave a Reply

1 comment

  1. If this code is in your functions.php file, you would need an action or filter to make it run at the appropriate time. Here’s an example of what you might add to the functions.php file to achieve the result you’re looking for:

    function to_update_title( $title, $id = null ) {
    
        if (is_404()) {
            return "404. Page not found.";
        } elseif (is_page('46')) {
            return "NOT WORKING";
        } elseif (the_subtitle("","", false) == "") {
            return get_the_title();
        } else {
            return get_the_subtitle();
        }
    
        return $title;
    }
    add_filter( 'the_title', 'to_update_title', 10, 2 );