Using Custom Fields:
如果需要更具体的内容(可能是产品,其中每个页面都需要特定的图像),可以为每个页面使用自定义字段,例如og值:
1.-转到编辑页面,选中自定义字段复选框,以便在编辑器下方看到它
2.-输入新的自定义字段


应该是这样的
name
是你的
$key

3.-保存或更新页面。
4.-将此代码添加到函数中。php
function opengraph_tags() {
/* we set the URL */
$og_url = get_permalink();
/* we set the site name */
$og_site_name = get_bloginfo();
/* we set the image */
/* we check for a featured image */
global $post;
$og_img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), \'medium\');
$key = \'description\';
/* MY CUSTOM VALUE I WANT TO PUT FROM THE CONTENT TO THE HEADER */
$og_description = get_post_meta($post->ID, $key , true);
?>
<meta property="og:description" content="<?php echo $og_description; ?>"/>
<meta property="og:type" content="website" />
<meta property="og:url" content="<?php echo $og_url ?>"/>
<meta property="og:site_name" content="<?php echo $og_site_name ?>"/>
<meta property="og:image" content="<?php echo $og_img_src[0]; ?>"/>
<?php
}
add_action(\'wp_head\', \'opengraph_tags\', 5);
你可以看到我正在用
$key
它可以是您想要的任何内容,也可以获取您想要的所有自定义值,只要您创建了这些值,您也可以使用简单的
if(empty($myValue))
, 如果检查收割台,您将看到如下内容:

现在,如果您想在标题中使用其他值。php甚至页脚。php只需设置自定义字段并使用以下代码获取值:
<?php
global $post;
$key = \'description\';
/* MY CUSTOM VALUE I WANT TO PUT FROM THE CONTENT TO THE HEADER */
$og_description = get_post_meta($post->ID, $key , true);
?>