最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - browser tab showing page title twice

matteradmin3PV0评论

I recently changed themes, but using the same code, so I don't know why it's displaying differently.

The browser tab is showing the page title 2 times. It should show as Page Title - Site Title

How it should look: Filmography - Sabrina Carpenter Online / sabrina-carpenter

But has you can see in the image, it is showing "Filmography" twice. How do I fix this? I have included the code below.

<title>
<?php
// Shows the title of the page or post.
wp_title('');

// If it's an individual entry, you can customize the title here.
if (is_single()) {
    echo ' - ' . get_the_title();
}

// If it is a static page, displays the page title.
if (is_page()) {
    echo ' - ' . get_the_title();
}

// If it is a category, displays the category name.
if (is_category()) {
    echo ' - ' . single_cat_title('', false);
}

// If it is a search, displays the search results.
if (is_search()) {
    echo ' - Results for: ' . get_search_query();
}

// If it is a 404 error, it displays a custom message.
if (is_404()) {
    echo ' - Page Not Found';
}

// At the end, always show the name of the blog.
bloginfo('name');
?>

I recently changed themes, but using the same code, so I don't know why it's displaying differently.

The browser tab is showing the page title 2 times. It should show as Page Title - Site Title

How it should look: Filmography - Sabrina Carpenter Online / sabrina-carpenter

But has you can see in the image, it is showing "Filmography" twice. How do I fix this? I have included the code below.

<title>
<?php
// Shows the title of the page or post.
wp_title('');

// If it's an individual entry, you can customize the title here.
if (is_single()) {
    echo ' - ' . get_the_title();
}

// If it is a static page, displays the page title.
if (is_page()) {
    echo ' - ' . get_the_title();
}

// If it is a category, displays the category name.
if (is_category()) {
    echo ' - ' . single_cat_title('', false);
}

// If it is a search, displays the search results.
if (is_search()) {
    echo ' - Results for: ' . get_search_query();
}

// If it is a 404 error, it displays a custom message.
if (is_404()) {
    echo ' - Page Not Found';
}

// At the end, always show the name of the blog.
bloginfo('name');
?>
Share Improve this question edited Mar 21 at 0:09 mmm 3,8043 gold badges16 silver badges22 bronze badges asked Mar 20 at 18:00 Finding ColorsFinding Colors 1
Add a comment  | 

1 Answer 1

Reset to default 0

Edited to add

Having looked at the source of wp_title(), it looks like it already checks to see if you're on a page, a single post, a category or tag archive page, a search results page, a 404, etc. So a lot of the code is redundant; you might be able to use something like this:

<title>
<?php
wp_title();
echo ' - ';
bloginfo( 'name' );
?>
</title>

...and get the results you want. (As to why your code snippet worked in the old theme: it's possible the old theme was applying filters that the new one does not.)

Original answer

wp_title() will always display the page title in your code, if one is found; then, since is_page() is true (on a page), that code block will also display the page title. (You'll see the same issue on a single post, because there is_single() will return true.)

I'd suggest something like this:

<title>
<?php
// Shows the title of the page or post.
wp_title('');

// If it's an individual entry, you can customize the title here.
if (is_single()) {
    echo ' - ';
}

// If it is a static page, displays the page title.
if (is_page()) {
    echo ' - ';
}

// If it is a category, displays the category name.
if (is_category()) {
    echo ' - ' . single_cat_title('', false);
}

// If it is a search, displays the search results.
if (is_search()) {
    echo ' - Results for: ' . get_search_query();
}

// If it is a 404 error, it displays a custom message.
if (is_404()) {
    echo ' - Page Not Found';
}

// At the end, always show the name of the blog.
bloginfo('name');
?>
</title>

...might work the way you're expecting.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far