Inserting schema code to HTML of Homepage

Hi

Can anybody help me how to add a certain code to the HTML of my homepage? I mean I have no separate homepage kind of…my homepage shows latest posts only.

Actually I have to add a Schema code to the HTML of my homepage without any help of a plugin

Thank you in advance

Hi Sansar,

Thanks for writing in!

You can certainly insert any HTML code via Theme hook.
You can write a function in your child theme function file to target the hook and insert your code.
Please check this for help.

Hope this works for you.

Thanks

Hi @basanta Thank you for the useful article. I was unaware of hook facility. But as I am a novice, can u tell me how to insert following piece of Schema code which I need to add to the HTML of my homepage…

< script type=”application/ld+json”>

{

@context”: “http://schema.org”,

@type”: “WebSite”,

“url”: “https://www.sansarlochan.in/”,

“potentialAction”: {

@type”: “SearchAction”,

“target”: “https://query.example.com/search?q={search_term_string}”,

“query-input”: “required name=search_term_string”

}

}

< /script>

I read the whole article but still I am confused. Can you give me full code and can you tell me where to insert that code?

Hi Sansar,

Please follow our child theme setup guide and activate your child theme first. After that, kindly add the code below to functions.php file of your child theme:

function my_custom_schema() { ?>

//Replace this line with your schema and remove '//' infront

<?php
add_action( 'wp_head', 'my_custom_schema' );

The code above uses wp_head filter. Inside that block of code you can add your custom JavaScript code.

For more information about the customization of the theme and adding stuff to the header or different part of the website please kindly refer to customization best practices guide below.

Hope that helps.
Thanks!

I am getting this error after following your method -

Your PHP code changes were rolled back due to an error on line 108 of file wp-content/themes/x-child/functions.php. Please fix and try saving again.

syntax error, unexpected end of file

Hi Sansar,

Sorry for the inconvinicne caused. There was a missing ending curly brace } on the above provided code. Please use the below code and update accordingly.

function my_custom_schema() { ?>

//Replace this line with your schema and remove '//' infront

<?php
}
add_action( 'wp_head', 'my_custom_schema' );

Thanks!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.