Reputation: 119
I need to include script code schema only on homepage of my website.
But, I do not know too much about PHP code, I cant filter them based on that so I was wondering is there a way to include script code schema ONLY on homepage URL and not to show it on any other page.
I tried using this :
<?php if( is_front_page() ): ?>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": ...
...
}
</script>
<?php endif; ?>
Any suggestions are appreciated!
Thanks so much!
Upvotes: 0
Views: 866
Reputation: 17
Try this Code in your functions.php
function add_inline_script() {
?>
<?php if( is_home() || is_front_page() ): ?>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(document).ready(
function()
{
});
</script>
<?php endif;
}
add_action( 'wp_footer', 'add_inline_script', 0 );
Upvotes: 0
Reputation: 1319
Use $_SERVER['REQUEST_URI']
. Dump it on your homepage to figure out what your homepage url is, for example for me the url is: http://localhost:3000/index.php
so my $_SERVER['REQUEST_URI']
for that page will be /index.php
. For you depending on the config it can be just /
or any other value. Note that value and use that in your if check:
<?php
var_dump($_SERVER['REQUEST_URI']);
if($_SERVER['REQUEST_URI'] === '/index.php') {
echo 'its homepage';
}
Output on index.php:
string(10) "/index.php" its homepage
Output on other.php:
string(10) "/other.php"
Upvotes: 1