The problem is, because this code is running in functions.php
without any data being passed to it, it doesn’t know what type of query is running, so it will always perform the else
action.
You could take it out of functions.php
and change your header.php
to directly include the code:
<?php
// ... other header code
function body_schema()
{
$schema="http://schema.org/";
if(is_search())
{
$type = "SearchResultsPage";
} else {
$type="Blog";
}
echo 'itemscope itemtype="' . $schema . $type . '"';
} ?>
<body <?php body_class();?> <?php body_schema(); ?>>
This will work because when the code is running on the page itself, it knows whether the current query is a search query or not.
If you want to keep your function in functions.php
, you will need to pass it a parameter from the header. For example, you could pass the $query
var currently running in header.php
and that way your function in functions.php
knows what query you are asking about.