wordpress教程:代码快速创建自定义字段教程

关于wordpress的自定义字段功能是我们建站中一个非常重要的知识,所以学习wordpress自定义字段是必须的,那么下面教大家wordpress代码快速创建自定义字段。只需要将如下代码添加至function.php即可,简单快捷!

调用方法:

<?php echo mo_get_post_from(); ?>

/*
* post meta from 添加文章来源自定义字段
* ====================================================
*/
// 创建字段信息
$postmeta_from = array(
array(
"name" => "fromname",
"std" => "",
"title" => __('来源名', 'orui').':'
),
array(
"name" => "fromurl",
"std" => "",
"title" => __('来源网址', 'orui').':'
)
);
// 函数连接到指定action(动作)
add_action('admin_menu', '_postmeta_from_create');
add_action('save_post', '_postmeta_from_save');
//创建自定义字段输入框
function _postmeta_from() {
global $post, $postmeta_from;
foreach($postmeta_from as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo'<p>'.$meta_box['title'].'</p>';
echo '<p><input type="text" value="'.$meta_box_value.'" name="'.$meta_box['name'].'_value"></p>';
}
echo '<input type="hidden" name="post_newmetaboxes_noncename" id="post_newmetaboxes_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
}
//创建自定义字段模块
function _postmeta_from_create() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', __('来源', 'haoui'), '_postmeta_from', 'post', 'normal', 'high' );
}
}
// 保存数据
function _postmeta_from_save( $post_id ) {
global $postmeta_from;
if ( !wp_verify_nonce( $_POST['post_newmetaboxes_noncename'], plugin_basenaP M w , V bme(__FILE__) ))
return;
if ( !current_user_can( 'edit_posts', $post_id ))
return;
foreach($postmeta_from as $meta_box) {
$data = $_POST[$meta_box['name'].'_value'];
if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
add_post_meta($post_id, $meta_box['name'].'_value', $data,t s / $ e M 2 true);
elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
update_post_meta($post_id, $meta_box['name'].'_value', $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
}
}
// 调用函数
function mo_get_post_from($pid='', $prevtext='来源:'){
if( !$pid ){
$pid = get_the_ID();
}
$fromname = trim(get_post_meta($pid, "fromname_value", true));
$fromurl = trim(get_post_meta($pid, "fromurl_value", true));
$from = '';
if( $fromname ){
if( $fromurl ){
$from = '<a href="'.$fromurl.'" target="_blank" rel="external nofollow">'.$fromname.'</a>';
}else{
$from = $fromname;
}
$from = ($prevtext).$from;
}
return $from;
}

技术教程

wordpress教程:让 WordPress 文章的网址URL自动生成超链接

2020-12-5 11:58:47

技术教程

WordPress教程:标签云输出文章数量

2020-12-5 11:58:49

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索