幸亏Brian Fegter. 如果这个答案有帮助,请为Brian\'s answer 就在上面。
这是一个功能齐全的示例,说明了如何通过自己的插件将内容添加到“标题”中。在本例中,我为共享和类似按钮添加了Facebook Open Graph的属性。
只需使用示例代码开头的“Plugin Script”中指定的名称创建一个PHP文件,将其放在一个没有扩展名的同名文件夹中,然后将此文件夹复制到目标“/wp content/plugins”。
然后在“Wordpress”中,刷新“Plugins”,您将看到安装的新插件。只要激活它,你的页面就会开始包含开放图Facebook和Twitter的元数据。
VERY IMPORTANT: PHP文件必须用UTF-8编码,没有BOM表,并且末尾绝对没有字符。必须确保这一点。
<?php
/*
Plugin Name: My Facebook Open Graph Protocol
Plugin Script: my-facebook-open-graph-protocol.php
Plugin URI:
Description: Add Facebook Open Graph Protocol to header
Author: Diego Soto (Thanks to Brian Fegter)
Donate Link:
License: GPL
Version: 0.1-alpha
Author URI: https://wordpress.stackexchange.com/questions/43672/how-to-add-code-to-header-php-in-a-child-theme
Text Domain: myfogp
Domain Path: languages/
*/
/* Copyright 2014 Diego Soto (http://disientoconusted.blogspot.com.ar/)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
add_action(\'wp_head\', \'wpse_43672_wp_head\');
function wpse_43672_wp_head(){
$title = get_the_title() ." ‹ ". get_bloginfo( "name", "display" );
$src = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), array( 90,55 ), false, "" );
$face_metad = get_post_meta(get_the_ID(), "metadescription", true);
$twitter_metad = get_post_meta(get_the_ID(), "metadescription140", true);
if (empty($twitter_metad))
$twitter_metad = $face_metad;
//Close PHP tags
?>
<meta property="og:title" content="<?php echo esc_attr($title); ?>" />
<meta property="og:image" content="<?php echo esc_attr($src[0]); ?>" />
<meta property="og:url" content="<?php the_permalink(); ?>" />
<meta property="og:description" content="<?php if (!empty($face_metad)) echo esc_attr($face_metad); else the_excerpt(); ?>" />
<meta name="twitter:title" content="<?php echo esc_attr($title); ?>" />
<meta name="twitter:image" content="<?php echo esc_attr($src[0]); ?>" />
<meta name="twitter:url" content="<?php the_permalink(); ?>" />
<meta name="twitter:description" content="<?php if (!empty($twitter_metad)) echo esc_attr($twitter_metad); else the_excerpt(); ?>" />
<?php //Open PHP tags
}
?>
任何对插件功能感兴趣的人。
标题将是当前页面名称和网站名称的串联。
如果存在名为“metadescription”的自定义字段,插件将尝试从此字段获取描述。否则,请从摘录中进行描述。
作为图像,插件尝试使用页面上featuredimage的缩略图。