I try to set it Title Tag. I have an issue how to make it in the correct way. Most of WordPress themes support title-tag but new theme throws an error when validated:
Need help.
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
Should we set it inside functions to detect ‘title-tag’?
Hi @toplisek, what error exactly are you getting? PHP does indeed allow return statements outside functions, in which case the execution of the entire current script is getting stopped (which however might certainly cause your code not to run as intended, or even induce other errors).
In any case, it’s most probably not the theme throwing an error here. You might try putting your title tag logic inside the if block, rather than using an early return guard… so this:
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
// Your title-tag logic
Would become:
if ( current_theme_supports( 'title-tag' ) ) {
// Your title-tag logic
}
PS: Or actually using a function:
function my_title_tag_logic() {
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
// The title-tag logic
}
// Somewhere else
my_title_tag_logic();
To add support for the title tag in your custom WordPress theme, you can use the add_theme_support() function in your theme’s functions.php file. Here’s how you can do it:
In this code, theme_slug_setup is a custom function that calls add_theme_support( 'title-tag' ) to enable support for the title tag. This function is hooked into the after_setup_theme action, which is triggered after the theme is loaded.
After adding this code to your functions.php file, WordPress will automatically manage the title tag for your theme, and you should see the correct title tags in your pages.
Remember to replace theme_slug_setup with a unique function name that matches your theme’s naming conventions to avoid conflicts with other themes or plugins.