自定义文章类型wordpress中的自定义标题标记
我有一个问题: 我创建了一个自定义的帖子类型。 我想有一个自定义的标题标签(一个CutomPostMeta) 怎么做 我试着在网上学习教程和其他问题,但一无所获 我也尝试过修改header.php,但它是show“default”wordpress自定义文章类型wordpress中的自定义标题标记,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我有一个问题: 我创建了一个自定义的帖子类型。 我想有一个自定义的标题标签(一个CutomPostMeta) 怎么做 我试着在网上学习教程和其他问题,但一无所获 我也尝试过修改header.php,但它是show“default”wordpress 您能帮我吗?在自定义邮件类型中添加自定义元框 将此添加到您的functions.php-将您的自定义帖子类型更改为自定义帖子类型 function add_page_custom_meta_box() { $boxTitle = 'Custom
您能帮我吗?在自定义邮件类型中添加自定义元框 将此添加到您的functions.php-将您的自定义帖子类型更改为自定义帖子类型
function add_page_custom_meta_box() {
$boxTitle = 'Custom Title';
$postType = 'YOUR_CUSTOM_POST_TYPE';
add_meta_box(
'page_custom_meta_box', // $id
$boxTitle, // $title
'show_page_custom_meta_box', // $callback
$postType, // $post_type
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_page_custom_meta_box');
$prefix = 'custom_';
$custom_meta_fields_page = array(
array(
'label' => 'Custom Title',
'desc' => 'Custom Title of the Page.',
'id' => $prefix.'title',
'type' => 'text'
),
);
function show_page_custom_meta_box() {
global $custom_meta_fields_page, $post;
// Use nonce for verification
echo '<input type="hidden" name="page_custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
echo '<table class="form-table">';
// Begin the field table and loop
foreach ($custom_meta_fields_page as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th>
<style>label[for=custom_link-text-to]{color:#00bbe2; text-transform:uppercase;}</style>
<label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
} //end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
function save_page_custom_meta($post_id) {
global $custom_meta_fields_page;
// verify nonce
if (!wp_verify_nonce($_POST['page_custom_meta_box_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_meta_fields_page as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($field['type'] == 'tripple_repeatable')
$new = array_values($new);
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_page_custom_meta');
函数add\u page\u custom\u meta\u box(){
$boxTitle=‘自定义标题’;
$postType='YOUR_CUSTOM_POST_TYPE';
添加元框(
“页面自定义元框”,//$id
$boxTitle,//$title
'显示页面\自定义\元\框',//$callback
$postType,//$post\u类型
'正常',//$context
“高”);/$priority
}
添加动作(“添加元框”、“添加页面自定义元框”);
$prefix='custom_';
$custom\u meta\u fields\u page=数组(
数组(
“标签”=>“自定义标题”,
“desc'=>”页面的自定义标题。“,
'id'=>$prefix.'title',
'键入'=>'文本'
),
);
函数show_page_custom_meta_box(){
全局$custom_meta_fields_page,$post;
//使用nonce进行验证
回声';
回声';
//开始字段表并循环
foreach($custom\u meta\u fields\u页面为$field){
//获取此字段的值(如果此帖子存在)
$meta=get_post_meta($post->ID,$field['ID'],true);
//以开始表格行
回声'
标签[for=custom_link-text-to]{color:#00bbe2;文本转换:大写;}
“.$field['label']”
';
开关($field['type'])){
案例“文本”:
回声'
。$field['desc'].';
打破
}//结束开关
回声';
}//结束foreach
echo“”;//结束表
}
函数save_page_custom_meta($post_id){
全局$自定义\元\字段\页面;
//立即验证
如果(!wp\u verify\u nonce($\u POST['page\u custom\u meta\u box\u nonce'],basename(\uuu FILE\uuuu)))
返回$post_id;
//检查自动保存
if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE)
返回$post_id;
//检查权限
如果('page'=$\u POST['POST\u type']){
如果(!当前用户可以($edit\u page',$post\u id))
返回$post_id;
}elseif(!当前用户可以('edit_post',$post_id)){
返回$post_id;
}
//循环遍历字段并保存数据
foreach($custom\u meta\u fields\u页面为$field){
$old=get_post_meta($post_id,$field['id'],true);
$new=$_POST[$field['id']];
如果($field['type']=='tripple\u repeatable')
$new=数组_值($new);
如果($new&&$new!=$old){
更新发布元($post\u id,$field['id'],$new);
}elseif(''=$new&$old){
删除后元($post\u id,$field['id'],$old);
}
}//结束foreach
}
添加动作(“保存帖子”、“保存页面”自定义元数据);
然后,无论您希望标题显示在何处:
<?php if ($custom_title = get_post_meta($post->ID, 'custom_title', true)){echo $custom_title;}?>