by ? If so, you can use many code fragments for your WordPress website in various ways.
But before we understand how to add them, let us briefly understand the meaning of the pattern mark.
What is a pattern mark?
SCHEMA mark is a standardized data structure provided by schema.org. Basically, it will provide information about the current page of
. So their crawler robots can understand what the content is, and build rich fragments on the search results.
The data contained in the mode is presented in the form of key value pair, such as [Telephone \”:\” 152234029 \”]. It can also have multiple inheritance levels. They are very important for improving the
clicks (CTR) of your website, and are also considered one of the best SEO practices.
There are many types of key attributes that can be used with mode. They are based on data and data types based on open and closed -level structures. Visit
SHCEMA.org website to understand all available types currently available.
How to mark the pattern to WordPress by programming?
In the previous article, we discussed how to use the plug -in to add the mode to wordpress. Similarly, we have discussed more ways to mark the mode to add pattern to WordPress through the dashboard and theme.
But now, we will learn how to achieve the same results by programming. In this way, your website will not need to load all the additional code provided by the plug -in. You will only add the code required by your website.
In order to include architecture in a content, we need to add architecture tags to our website HTML output. We can edit the HTML of our website in various ways. The best way is to cover the theme template file on the theme.
You need a sub -theme to include architecture marks in programming. SoIf you haven’t installed it yet, please continue to create and install the theme of the sub -. You can even use one of the sub -theme plugins to create it.
You can add the pattern to wordpress by programming through the two main methods of
:
- Add JSON-LD file to the head
- [
[ 123] Mark the micro -data to HTML output
Both are effective methods. You can freely use methods that are more suitable for your needs.
1. Add json-ld file
JSON-LD file is a specific type of format, based on JSON, which is used to encode link data. Just like it happened on the JSON file, it contains a key value to the list:
{\”phone\”: \”152234029\”, \u0026 quot; name \u0026 quot ;: \u0026 quot;街N 433”, \u0026quot;url\u0026quot;: \u0026quot;https://QuadLayers.com\u0026quot;, \u0026quot;priceRange\u0026quot;: \u0026quot;19 – 90\u0026quot;, \u0026quot;legalName\u0026quot;: \u0026quot;四层\u0026quot;, \u0026quot;@context\u0026quot; : \u0026 quot; http://schema.org\u0026quot ;, \u0026 quot;@type \u0026 quot ;: \u0026 quot; local enterprise \u0026 quot;}
As you can see, even grammar is very similar to JSON files.
You can use multiple JSON-LD generators to obtain files that are more suitable for your needs.
Therefore, the next step is to print this code in HTML
\u0026 lt; head \u0026 gt; .
We can use the following functions in the sub -theme
functions.php file to implement.
Note: Before we continue, make sure you also back up your website. We will modify the core documents of your website, and any changes that do not need may bring it more problems.
add_action(\u0026#39;wp_head\u0026#39;,\u0026#39;QuadLayers_add_schema\u0026#39;);函数QuadLayers_add_schema(){echo \u0026#39;\u0026lt;script type\u003d\u0026quot;application/ld+json\u0026quot;\u0026gt;{“电话”: “152234029”,\u0026quot;name\u0026quot;: \u0026quot;名字\u0026quot;,“地址”:“约翰街N 433”,\u0026quot;url\u0026quot;: \u0026quot;https://QuadLayers.com\u0026quot;,\u0026quot;priceRange\u0026quot;: \u0026quot;19 – 90\u0026quot;,\u0026quot;legalName\u0026quot;: \u0026quot;四层\u0026quot;,\u0026quot;@context\u0026quot;: \u0026quot;http://schema.org\u0026quot;,\u0026quot;@type\u0026quot;: \u0026quot;本地企业\u0026quot;}\u0026#39; ;}
You can see how the json-ld file is included in
\u0026 lt; script \u0026 gt; mark. Just don't forget Update files .
To understand the working principle of the architecture, please note that the previous example code will be added to the website to the website. All pages. Therefore, when a search engine or any other types of robots read it, it will be set to the valid mode of all pages of the script. In this case, it is the entire website.
We can apply JSON code to specific pages by applying some conditional logic to our PHP function. However, the pattern mark can also be included in the internal HTML of our content, allowing us to include complex marks into some very specific content.
2. Add micro -data to HTML
conforming to the modern trend, it is recommended to use the previous method. However, the use of micro -data marks will allow us
to embed mode marks in the template file
. Paste our script to the theme of the sub -theme
Functions.php
file can also work normally. In order to apply the correct mode to each content, we will eventually have a lot of complicated code.
Therefore, to add micro -data markers to HTML, we need to cover your template file on your WordPress website.
2.1. Add the architecture to WP post
In this example, our sub-theme inherited the theme of twenty-one, so the file we need to cover is
Content-Single.php
Essence This is the file we can add our mode to the HTML output. On certain themes, this file may be located under different names or folders.
Create the required folder on your sub-theme (template part and content), and create a new
content-single.php
file in the content folder. Children’s theme/__ template parts __/__ content __/__ content-single.php
Copy/paste the father theme
content- single.php [123 123 ] All code of the file.
The code we want 123] \u0026 lt; Article \u0026 lt; php post_class (); \u0026 gt; \u0026 gt;
Add architecture range and type, as follows.
\u0026 lt; Article itemScope itemtype \u003d \u0026 quot; http://schema.org/article enrich; \u0026 lt; php post_class (); \u0026 gt;
This tells the search engine
\u0026 lt Article \u0026 GT;
The content in the HTML mark is the article type.
After the type and scope of the statement, we can use some standard attributes. For example, the title declaration of the post is the project name:
source code:
\u0026 lt; php the_title (\u0026#39; \u0026 lt; h1 class \u003d \u0026 quot; Entry-Title \u0026 QUOT; \u0026 GT; \u0026#39 ;, \u0026#39; \u0026 lt;/h1 \u0026 gt; \u0026#39;); \u0026 gt;
Add name attributes:
\u0026 lt; php the_title (\u0026 \u0026Tle #39; \u0026 lt; h1 itemprop \u003d \u0026 quot; name \u0026 quot; class \u003d \u0026 quot; entry-tital \u0026 quot; \u0026#39;, \u0026#39;/h1 \u0026 \u0026#39; \u0026 gt; \u0026 gt; \u0026 gt;
In addition, we can use
ArticleBody key attributes to mark the content of our post. Source code:
\u0026 lt; div class \u003d \u0026 quot; entry-content \u0026 quot; \u0026 lt; php content ();
添加articleBody 属性:
\u0026lt;div itemprop\u003d\u0026quot;articleBody\u0026quot; class\u003d\u0026quot;entry-content\u0026quot;\u0026gt;\u0026lt;php内容();
2.2. 向WooCommerce 产品Add mode marking
In a similar way, we can cover the Woocommerce product template file to contain our mode.
The file we need Essence This file must be in the
WooCommerce folder
. If you find it difficult to find it, make sure you set Woocommerce correctly.
Children’s theme __/WooCommerce __/__ Content-SINGLE-PRODUCT.PHP Complete the same process as the above, and create a new file at its location. Define the range and mode type on the main DIV packaging device:
\u0026 lt; divly itemscope itemtype \u003d \u0026 quot; http: //schema.org/product\u0026quot; \u0026 lt; \u0026 lt; php WC_product_class (\u0026#39; \u0026#39;, $ Product); \u0026 GT; \u0026 GT;
We can also use the \"description\" attribute on this file:
\u0026 lt; divlymprop \u003d \u0026 quot; description \u0026 quot; class \u003d \u0026 quot; summary entry \u0026 quot; \u0026 gt;
However, in order to add more marks to our products, we need to cover different files.
For example, to add the price of the product to the architecture, please create a new path on the sub-theme file: WooCommerce/Templates/Single-PRODUCT/Price.php [ 123] Children’s theme __/Woocommerce __/__/single product __/__/__/price.php
will
Price. After the pHP
file is copied to its position, the editor is as follows:
Source code:
\u0026 lt; p class \u003d \u0026 quot; \u0026 lt; php echo esc_attr (apply_filters (apply_filters (apply_filters (apply_filters (apply_filters (apply_filters (Apply_filters ( \u0026#39;woocommerce_product_price_class\u0026#39;, \u0026#39;price\u0026#39; ) ); \u0026gt;\u0026quot;\u0026gt;\u0026lt;php echo $product-\u0026gt;get_price_html(); \u0026gt;\u0026lt;/p\u0026gt;[ 123]
Add mark:
: //schema.org/offer \u0026quot; itmprop \u003d \u0026 quot; price \u0026 quot; class \u003d \u0026 quot; \u0026 lt; php echo esc_attr (apply_filters (\u0026#39; woocommerce_product_pRice_class \u0026#39;, \u0026#39; Price \u0026#39;); \u0026 gt; \u0026 quot; \u0026 lt; php echo $ Product- \u0026 gt_price_html (); \u0026 lt;/p \u0026 gt; We have created a new project with a \”quotation\” type to define the price. This is a clue that we understand how we nested different project types according to the original HTML structure.
You can cover some other files to contain more tags.
title.php
stock.php[123 ] Review.php
yating.php
-
Product -IMAGE.PHP - Conclusion
It is also important to understand that all attributes and types must match the attributes and types defined on the SCHEMA.org website. This also includes attributes with multi -level inheritance.This has concluded how to add a tutorial on how to mark the pattern to WordPress by programming. To this end, you need to have a basic understanding of how the model work and some programming knowledge. - In short, you can use two main ways to add the architecture to your website:
Add json-ld file to the head -
in your website.Adding the JSON-LD file is a very simple method and is considered by most users. All you have to do is print the JSON-LD code to part of\u0026 lt; head \u0026 gt; - However, the use of micro -data marking method to migrate the mode directly into the template file is also very useful for you. If you have needed resources, covering template files can help you customize the website more flexible. You can even add architecture to specific elements of your website, such as posts and WooCommerce products.
At the same time, there are some you canCan be interested and help you improve the post on WordPress website:So can you now use these code fragments to add the architecture to your website? Please tell us if this tutorial is helpful in the comments.
Mark the micro-data mark Adding to HTML output
How to create a custom WordPress plug -in
add the telegram to wordpressEdit HTML