小冬SEO

wordpress后台设置中增加自定义字段首页seo标题关键词描述

2019-01-11 15:39:33 1447 WordPress笔记

wordpress程序做网站还是非常强大的,几乎所有的地方都是可以二次开发的,现在我们就在网站后台的《设置》栏目中增加类似于seo标题、seo关键词、seo描述、电话、地址等相关信息,具体操作如下:

1、在文件中添加如下代码

找到你使用的主题文件夹中的functions.php文件

//添加常规自定义字段选项
function cfg_register_fields() {
    register_setting( 'general', 'seo_title' );
    register_setting( 'general', 'seo_keywords' );
    register_setting( 'general', 'seo_description' );
    register_setting( 'general', 'seo_dianhua' );
    register_setting( 'general', 'seo_youxiang' );
    register_setting( 'general', 'seo_pinpai' );
    register_setting( 'general', 'seo_houzhui' );
    register_setting( 'general', 'seo_zuoji' );
    register_setting( 'general', 'seo_qq' );
    register_setting( 'general', 'seo_banquan' );
    register_setting( 'general', 'seo_beian' );
    register_setting( 'general', 'seo_shuoming' );
    register_setting( 'general', 'seo_dizhi' );
    register_setting( 'general', 'seo_dizhi2' );
    register_setting( 'general', 'seo_web' );
    register_setting( 'general', 'seo_qita1' );
    register_setting( 'general', 'seo_qita2' );
    add_settings_field( 'seo_title', '<label for="seo_title">seo标题</label>', 'cfg_fields_title', 'general' );
    add_settings_field( 'seo_keywords', '<label for="seo_keywords">seo关键词</label>', 'cfg_fields_keywords', 'general' );
    add_settings_field( 'seo_description', '<label for="seo_description">seo描述</label>', 'cfg_fields_description', 'general' );
    add_settings_field( 'seo_web', '<label for="seo_web">移动端url</label>', 'cfg_fields_web', 'general' );
    add_settings_field( 'seo_dianhua', '<label for="seo_dianhua">电话</label>', 'cfg_fields_dainhua', 'general' );
    add_settings_field( 'seo_zuoji', '<label for="seo_zuoji">座机</label>', 'cfg_fields_zuoji', 'general' );
    add_settings_field( 'seo_qq', '<label for="seo_qq">QQ</label>', 'cfg_fields_qq', 'general' );
    add_settings_field( 'seo_youxiang', '<label for="seo_youxiang">邮箱</label>', 'cfg_fields_youxiang', 'general' );
    add_settings_field( 'seo_pinpai', '<label for="seo_pinpai">品牌</label>', 'cfg_fields_pinpai', 'general' );
    add_settings_field( 'seo_houzhui', '<label for="seo_houzhui">后缀</label>', 'cfg_fields_houzhui', 'general' );
    add_settings_field( 'seo_dizhi', '<label for="seo_dizhi">地址</label>', 'cfg_fields_dizhi', 'general' );
    add_settings_field( 'seo_dizhi2', '<label for="seo_dizhi2">地址2</label>', 'cfg_fields_dizhi2', 'general' );
    add_settings_field( 'seo_banquan', '<label for="seo_banquan">版权</label>', 'cfg_fields_banquan', 'general' );
    add_settings_field( 'seo_beian', '<label for="seo_beian">备案</label>', 'cfg_fields_beian', 'general' );
    add_settings_field( 'seo_shuoming', '<label for="seo_shuoming">说明</label>', 'cfg_fields_shuoming', 'general' );
    add_settings_field( 'seo_qita1', '<label for="seo_qita1">其他功能字段1</label>', 'cfg_fields_qita1', 'general' );
    add_settings_field( 'seo_qita2', '<label for="seo_qita2">其他功能字段2</label>', 'cfg_fields_qita2', 'general' );
}
function cfg_fields_qita1() {
    $value = get_option( 'seo_qita1', '' );
    echo '<input name="seo_qita1" id="seo_qita1" class="regular-text web"    value="' . $value . '"/>';
}
function cfg_fields_qita2() {
    $value = get_option( 'seo_qita2', '' );
    echo '<input name="seo_qita2" id="seo_qita2" class="regular-text web"    value="' . $value . '"/>';
}
function cfg_fields_web() {
    $value = get_option( 'seo_web', '' );
    echo '<input name="seo_web" id="seo_web" class="regular-text web"    value="' . $value . '"/>';
}
function cfg_fields_zuoji() {
    $value = get_option( 'seo_zuoji', '' );
    echo '<input name="seo_zuoji" id="seo_zuoji" class="regular-text title"    value="' . $value . '"/>';
}
function cfg_fields_banquan() {
    $value = get_option( 'seo_banquan', '' );
    echo '<input name="seo_banquan" id="seo_banquan" class="regular-text banquan"    value="' . $value . '"/>';
}
function cfg_fields_beian() {
    $value = get_option( 'seo_beian', '' );
    echo '<input name="seo_beian" id="seo_beian" class="regular-text beian"    value="' . $value . '"/>';
}
function cfg_fields_shuoming() {
    $value = get_option( 'seo_shuoming', '' );
    echo '<input name="seo_shuoming" id="seo_shuoming" class="regular-text shuoming"    value="' . $value . '"/>';
}
function cfg_fields_qq() {
    $value = get_option( 'seo_qq', '' );
    echo '<input name="seo_qq" id="seo_qq" class="regular-text title"    value="' . $value . '"/>';
}
function cfg_fields_dizhi() {
    $value = get_option( 'seo_dizhi', '' );
    echo '<input name="seo_dizhi" id="seo_dizhi" class="regular-text title"    value="' . $value . '"/>';
}
function cfg_fields_dizhi2() {
    $value = get_option( 'seo_dizhi2', '' );
    echo '<input name="seo_dizhi2" id="seo_dizhi2" class="regular-text title"    value="' . $value . '"/>';
}
function cfg_fields_title() {
    $value = get_option( 'seo_title', '' );
    echo '<input name="seo_title" id="seo_title" class="regular-text title"    value="' . $value . '"/>';
}
function cfg_fields_keywords() {
    $value = get_option( 'seo_keywords', '' );
    echo '<input name="seo_keywords" id="seo_keywords" class="regular-text  keywords"   value="' . $value . '"/>';
} 
function cfg_fields_description() {
    $value = get_option( 'seo_description', '' );
    echo '<input name="seo_description" id="seo_description" class="regular-text description"  value="' . $value . '"/>';
} 
function cfg_fields_dainhua() {
    $value = get_option( 'seo_dianhua', '' );
    echo '<input name="seo_dianhua" id="seo_dianhua" class="regular-text dainhua"  value="' . $value . '"/>';
} 
function cfg_fields_youxiang() {
    $value = get_option( 'seo_youxiang', '' );
    echo '<input name="seo_youxiang" id="seo_youxiang" class="regular-text youxiang"  value="' . $value . '"/>';
} 
function cfg_fields_pinpai() {
    $value = get_option( 'seo_pinpai', '' );
    echo '<input name="seo_pinpai" id="seo_pinpai" class="regular-text pinpai"  value="' . $value . '"/>';
} 
function cfg_fields_houzhui() {
    $value = get_option( 'seo_houzhui', '' );
    echo '<input name="seo_houzhui" id="seo_houzhui" class="regular-text houzhui"  value="' . $value . '"/>';
} 
add_filter( 'admin_init' , 'cfg_register_fields' );

2、然后前端通过以下代码调用

<?php echo get_option('seo_title'); ?>
<?php echo get_option('seo_keywords'); ?>
<?php echo get_option('seo_description'); ?>
<?php echo get_option('seo_dianhua'); ?>
<?php echo get_option('seo_youxiang'); ?>
<?php echo get_option('seo_pinpai'); ?>
<?php echo get_option('seo_houzhui'); ?>
<?php echo get_option('seo_zuoji'); ?>
<?php echo get_option('seo_qq'); ?>
<?php echo get_option('seo_banquan'); ?>
<?php echo get_option('seo_beian'); ?>
<?php echo get_option('seo_shuoming'); ?>
<?php echo get_option('seo_dizhi'); ?>
<?php echo get_option('seo_dizhi2'); ?>
<?php echo get_option('seo_web'); ?>
<?php echo get_option('seo_qita1'); ?>
<?php echo get_option('seo_qita2'); ?>

也有教程是在用户的《个人资料》中添加的,不过这种添加方式我测试过,前端调用会有一定局限性,例如只能在头部模板或者底部模板中调用,自定义的模板中调用就不生效,所以有了现在这种wordpress后台设置中增加自定义字段,任何地方都可以调用,还不快收藏起来。

注意:通过上面的方式,你还可以增加n多个这样的自定义字段,只需要根据上面的规律修改就行了,前端调用格式都一样!

版权保护: 本文由小冬SEO编辑发布,转载请保留链接: http://www.myseoyh.cn/shuo/112.html