- A+
所属分类:WordPress
与自定义字段相似,都是用钩子get_post_meta()
调用。
首先添加Meta Box数据,自定义字段格式可以是复选框文本数字等多种形式。代码添加到合适位置,如主题meta-box.php
文件,以下为文章添加Schema代码选项。
// 文章SEO添加Schema
$schema_post_meta_boxes =
array(
"schema_blogposting" => array(
"name" => "cms_top",
"std" => "",
"title" => "博客文章Schema",
"type"=>"checkbox"),
"schema_course" => array(
"name" => "cat_top",
"std" => "",
"title" => "课程Schema",
"type"=>"checkbox"),
"post_img" => array(
"name" => "post_img",
"std" => "",
"title" => "杂志布局图文模块",
"type"=>"checkbox"),
"hot" => array(
"name" => "hot",
"std" => "",
"title" => "本站推荐小工具中(有缩略图)",
"type"=>"checkbox"),
"posts" => array(
"name" => "posts",
"std" => "",
"title" => "推荐文章小工具中(无缩略图)",
"type"=>"checkbox"),
);
// 面板内容
function schema_post_meta_boxes() {
global $post, $schema_post_meta_boxes;
//获取保存
foreach ($schema_post_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'] . '', true);
if ($meta_box_value != "")
//将默认值替换为已保存的值
$meta_box['std'] = $meta_box_value;
echo '<input type="hidden" name="' . $meta_box['name'] . '_noncename" id="' . $meta_box['name'] . '_noncename" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
//选择类型输出不同的html代码
switch ($meta_box['type']) {
case 'title':
echo '<h4>' . $meta_box['title'] . '</h4>';
break;
case 'text':
echo '<h4>' . $meta_box['title'] . '</h4>';
echo '<span class="form-field"><input type="text" size="40" name="' . $meta_box['name'] . '" value="' . $meta_box['std'] . '" /></span><br />';
break;
case 'textarea':
echo '<h4>' . $meta_box['title'] . '</h4>';
echo '<textarea id="seo-excerpt" cols="40" rows="2" name="' . $meta_box['name'] . '">' . $meta_box['std'] . '</textarea><br />';
break;
case 'radio':
echo '<h4>' . $meta_box['title'] . '</h4>';
$counter = 1;
foreach ($meta_box['buttons'] as $radiobutton) {
$checked = "";
if (isset($meta_box['std']) && $meta_box['std'] == $counter) {
$checked = 'checked = "checked"';
}
echo '<input ' . $checked . ' type="radio" class="kcheck" value="' . $counter . '" name="' . $meta_box['name'] . '_value"/>' . $radiobutton;
$counter++;
}
break;
case 'checkbox':
if (isset($meta_box['std']) && $meta_box['std'] == 'true') $checked = 'checked = "checked"';
else $checked = '';
echo '<br /><input type="checkbox" name="' . $meta_box['name'] . '" value="true" ' . $checked . ' />';
echo '<label>' . $meta_box['title'] . '</label><br />';
break;
}
}
}
// 创建面板
function schema_post_meta_box() {
global $theme_name;
if (function_exists('add_meta_box')) {
add_meta_box('schema_post_meta_box', '将文章添加到', 'schema_post_meta_boxes', 'post', 'normal', 'high');
}
}
// 保存数据
function save_schema_post_postdata($post_id) {
global $post, $schema_post_meta_boxes;
foreach ($schema_post_meta_boxes as $meta_box) {
if (!wp_verify_nonce($_POST[$meta_box['name'] . '_noncename'], plugin_basename(__FILE__))) {
return $post_id;
}
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) return $post_id;
} else {
if (!current_user_can('edit_post', $post_id)) return $post_id;
}
$data = $_POST[$meta_box['name'] . ''];
if (get_post_meta($post_id, $meta_box['name'] . '') == "") add_post_meta($post_id, $meta_box['name'] . '', $data, true);
elseif ($data != get_post_meta($post_id, $meta_box['name'] . '', true)) update_post_meta($post_id, $meta_box['name'] . '', $data);
elseif ($data == "") delete_post_meta($post_id, $meta_box['name'] . '', get_post_meta($post_id, $meta_box['name'] . '', true));
}
}
// 触发
add_action('admin_menu', 'schema_post_meta_box');
add_action('save_post', 'save_schema_post_postdata');
然后在文章中特定位置引用即可,一般为single.php
文件。
<?php if ( get_post_meta($post->ID, 'schema_blogposting', true) ) : ?><div>自定义代码开始了</div><?php endif; ?>
或者价格自定义字段。
<?php if ( get_post_meta($post->ID, 'pricey', true) ) : ?>
<span class="pricey"><del>市场价:<?php $price = get_post_meta($post->ID, 'pricey', true);{ echo $price; }?>元</del></span>
<?php endif; ?>
独角兽驿站
公众号