简体   繁体   中英

add custom metadata in custom page template wordpress

I have problem to add custom metadata to <head></head> custom page template.

I googled a lot and found to add this to my custom page template

get_header();
some code here ......

load mysql data and put that data in function mySEO()

function mySEO() {
echo '
<title>titleeee</title>
<meta name="description" content="Page description. No longer than 155 characters." />
<meta itemprop="name" content="The Name or Title Here">
<meta itemprop="description" content="This is the page description">
<meta itemprop="image" content="http://www.example.com/image.jpg">
<meta name="twitter:site" content="@publisher_handle">
<meta name="twitter:title" content="Page Title">
<meta name="twitter:description" content="Page description less than 200 characters">
<meta name="twitter:creator" content="@author_handle">
<meta name="twitter:image" content="http://www.example.com/image.html">

<meta property="og:title" content="Title Here" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://www.example.com/" />
<meta property="og:image" content="http://example.com/image.jpg" />
<meta property="og:description" content="Description Here" />
<meta property="og:site_name" content="Site Name, i.e. Moz" />';
}
add_action('wp_head','mySEO');
///some code here...
get_footer();

BUT nothing happens i don't put anything in head of page... what is problem?

You should inlcude the values for the data you need at the top of the PAGE, before the HEADER gets called.

That way, the values will be available to populate the vars included in the HEADER.

"VEL" responde to my question:

did you place add_action('wp_head','mySEO'); and mySEO function in functions.php? – vel

and call mySEO the function in your template

complete answer:

this is in function.php: 
function mySEO($title,$description,$image,$url) { echo '
<title>'.$title.'</title>
<meta name="description" content="'.$description.'" />
<meta itemprop="name" content="'.$title.'">
<meta itemprop="description" content="'.$description.'">
<meta itemprop="image" content="'.$image.'">
<meta name="twitter:site" content="@Tw">
<meta name="twitter:title" content="'.$title.'">
<meta name="twitter:description" content="'.$description.'">
<meta name="twitter:creator" content="@Tw">
<meta name="twitter:image" content="'.$url.'">
<meta property="og:title" content="'.$title.'" />
<meta property="og:type" content="article" />
<meta property="og:url" content="'.$url.'" />
<meta property="og:image" content="'.$image.'" />
<meta property="og:description" content="'.$description.'" />
<meta property="og:site_name" content="SITE" />'; 
}
add_action("wp_head","mySEO"); 

in mypage.php (custom page template) just call function mySEO($title,$description,$image,$url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM