作者广告
作者广告

7b2主题通过自定义栏目增加文章来源

前言:

介绍如何利用自定义栏目给主题添加文章转载来源。

后台添加自定义栏目选项

主题functions.php内添加如下代码:

if ( !class_exists(‘myCustomFields’<span%20class=”br0″>) ) {

 

class myCustomFields {

/**

* @var string $prefix 自定义栏目前缀,一个完整的自定义栏目是需要前缀+name的,比如我的前缀是git_,name下面有baidu_submit,那么完整的自定义栏目就是git_baidu_submit.

*/

var $prefix = <span%20class=””>;

/**

* @var array $postTypes 这是自定义面板的使用范围,这里一般就是在文章以及页面

*/

var $postTypes = array( “page”, “post”, “product” , “gallery” );

/**

* @var array $customFields 开始组件自定义面板数组

*/

var $customFields = array(

array(

“name” => “f”,

“title” => “转载来源名字”,

“description” => “这里可以输入文章转载名字,不填写的话,不显示转载标签”,

“type” => “text”,

“scope” => array( “post”, “page” ),

“capability” => “edit_posts”

),

array(

“name” => “furl”,

“title” => “转载来源链接”,

“description” => “这里可以输入您的转载来源链接”,

“type” => “text”,

“scope” => array( “post”, “page” ),

“capability” => “edit_posts”

)

);

/**

* PHP 5 Constructor

*/

__construct() {

add_action( ‘admin_menu’<span%20class=””>, array( $this, ‘createCustomFields’ ) );

add_action( ‘save_post’<span%20class=””>, array( $this, ‘saveCustomFields’ ), 1, 2 );

}

/**

* 创建一组你自己的自定义栏目

*/

createCustomFields() {

if ( function_exists( ‘add_meta_box’ ) ) {

foreach ( $this>postTypes as $postType ) {

add_meta_box( ‘my-custom-fields’<span%20class=””>, ‘主题文章发布选项’<span%20class=””>, array( $this, ‘displayCustomFields’ ), $postType, ‘normal’<span%20class=””>, ‘high’ );

}

}

}

/**

* 在文章发布页显示出来面板

*/

displayCustomFields() {

global $post;

?>

<div class=“form-wrap”>

<?php

wp_nonce_field( ‘my-custom-fields’<span%20class=””>, ‘my-custom-fields_wpnonce’<span%20class=””>, false, true );

foreach ( $this>customFields as $customField ) {

// Check scope

$scope = $customField[ ‘scope’ ];

$output = false;

foreach ( $scope as $scopeItem ) {

switch ( $scopeItem ) {

default: {

if ( $post>post_type == $scopeItem )

$output = true;

break;

}

}

if ( $output ) break;

}

// 检查权限

if ( !current_user_can( $customField[‘capability’<span%20class=”br0″>], $post>ID ) )

$output = false;

// 通过则输出

if ( $output ) { ?>

<div class=“form-field form-required”>

<?php

switch ( $customField[ ‘type’ ] ) {

case “checkbox”: {

// Checkbox 组件

echo ‘<label for=”‘<span%20class=””> . $this>prefix . $customField[ ‘name’ ] .‘” style=”display:inline;”><b>’<span%20class=””> . $customField[ ‘title’ ] . ‘</b></label> ‘<span%20class=””>;

echo ‘<input type=”checkbox” name=”‘<span%20class=””> . $this>prefix . $customField[‘name’<span%20class=”br0″>] . ‘” id=”‘<span%20class=””> . $this>prefix . $customField[‘name’<span%20class=”br0″>] . ‘” value=”1″‘<span%20class=””>;

if ( get_post_meta( $post>ID, $this>prefix . $customField[‘name’<span%20class=”br0″>], true ) == “1” )

echo ‘ checked=”checked”‘<span%20class=””>;

echo ‘” style=”width: auto;” />’<span%20class=””>;

break;

}

case “textarea”:

case “wysiwyg”: {

// Text area

echo ‘<label for=”‘<span%20class=””> . $this>prefix . $customField[ ‘name’ ] .‘”><b>’<span%20class=””> . $customField[ ‘title’ ] . ‘</b></label>’<span%20class=””>;

echo ‘<textarea name=”‘<span%20class=””> . $this>prefix . $customField[ ‘name’ ] . ‘” id=”‘<span%20class=””> . $this>prefix . $customField[ ‘name’ ] . ‘” columns=”30″ rows=”5″>’<span%20class=””> . htmlspecialchars( get_post_meta( $post>ID, $this>prefix . $customField[ ‘name’ ], true ) ) . ‘</textarea>’<span%20class=””>;

// WYSIWYG

if ( $customField[ ‘type’ ] == “wysiwyg” ) { ?>

<script type=“text/javascript”>

jQuery( document ).ready( () {

jQuery( “<?php echo $this->prefix . $customField[ ‘name’ ]; ?>” ).addClass( “mceEditor” );

if ( typeof( tinyMCE ) == “object” && typeof( tinyMCE.execCommand ) == “function” ) {

tinyMCE.execCommand( “mceAddControl”, false, “<?php echo $this->prefix . $customField[ ‘name’ ]; ?>” );

}

});

</script>

<?php }

break;

}

default: {

// Plain text field

echo ‘<label for=”‘<span%20class=””> . $this>prefix . $customField[ ‘name’ ] .‘”><b>’<span%20class=””> . $customField[ ‘title’ ] . ‘</b></label>’<span%20class=””>;

echo ‘<input type=”text” name=”‘<span%20class=””> . $this>prefix . $customField[ ‘name’ ] . ‘” id=”‘<span%20class=””> . $this>prefix . $customField[ ‘name’ ] . ‘” value=”‘<span%20class=””> . htmlspecialchars( get_post_meta( $post>ID, $this>prefix . $customField[ ‘name’ ], true ) ) . ‘” />’<span%20class=””>;

break;

}

}

?>

<?php if ( $customField[ ‘description’ ] ) echo ‘<p>’<span%20class=””> . $customField[ ‘description’ ] . ‘</p>’<span%20class=””>; ?>

</div>

<?php

}

} ?>

</div>

<?php

}

/**

* 保存自定义栏目数据

*/

function saveCustomFields( $post_id, $post ) {

if ( !isset( $_POST[ ‘my-custom-fields_wpnonce’ ] ) || !wp_verify_nonce( $_POST[ ‘my-custom-fields_wpnonce’ ], ‘my-custom-fields’ ) )

return;

if ( !current_user_can( ‘edit_post’<span%20class=””>, $post_id ) )

return;

if ( ! in_array( $post>post_type, $this>postTypes ) )

return;

foreach ( $this>customFields as $customField ) {

if ( current_user_can( $customField[‘capability’<span%20class=”br0″>], $post_id ) ) {

if ( isset( $_POST[ $this>prefix . $customField[‘name’<span%20class=”br0″>] ] ) && trim( $_POST[ $this>prefix . $customField[‘name’<span%20class=”br0″>] ] ) ) {

$value = $_POST[ $this>prefix . $customField[‘name’<span%20class=”br0″>] ];

// Auto-paragraphs for any WYSIWYG

if ( $customField[‘type’<span%20class=”br0″>] == “wysiwyg” ) $value = wpautop( $value );

update_post_meta( $post_id, $this>prefix . $customField[ ‘name’ ], $value );

} else {

delete_post_meta( $post_id, $this>prefix . $customField[ ‘name’ ] );

}

}

}

}

 

} // End Class

 

} // End if class exists statement

 

// Instantiate the class

if ( class_exists(‘myCustomFields’<span%20class=”br0″>) ) {

$myCustomFields_var = new myCustomFields();

前台显示:

子主题目录内新建文件夹

template-parts

,然后复制主题内

/template-parts/cotent.php

文件到此目录下,然后编辑134行

<div class=“longwb t-r fl”>

<button @click=“showLongWeiBo”><span><i class=iconfont zrz-icon-font-tupian”></i>海报</span></button>

</div>

在<button @click=”showLongWeiBo”>后添加代码:

<?php

$f = get_post_meta($post>ID, ‘f’<span%20class=””>, true);

if ( $f ) echo ‘<a rel=”nofollow” target=”_blank” href=”‘<span%20class=””> . get_post_meta($post>ID, ‘furl’<span%20class=””>, true) . ‘”><button><i class=”iconfont zrz-icon-font-beizhuyitianxie”></i> 来源:’<span%20class=””> .get_post_meta($post>ID, ‘f’<span%20class=””>, true) . ‘</button></a>’<span%20class=””>;

?>

大功告成。

在线交流反馈:[su_button url=”/forums/forum/52302″ target=”blank” style=”stroked” background=”#cfd2d6″ color=”#2b2929″ size=”4″]7b2柒比贰主题论坛[/su_button]

给TA打赏
共{{data.count}}人
人已打赏

【龙萱坤诺】声明:

龙萱坤诺所有资源来源于网络及用户分享或为本站原创,仅限用于学习和研究,如有侵权请邮件联系站长!不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站概不负责!

 

龙萱坤诺欢迎您投稿发表文章,更有龙萱坤诺奖励和额外收入!根据投稿内容质量,可获邀入驻本平台,开启您的站长之路!

 

如有链接无法下载、失效或广告,请到圈子反馈!

© 转载请声明:转载自“龙萱坤诺"

网站开发

7b2主题文章添加编辑按钮【根据教程可添加扩展其它链接】

2019-3-6 16:21:49

网站开发

7b2柒比贰主题启用后提示SWOOLEC Loader ext not installed解决教程

2019-3-6 16:37:13

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

热门专栏

文章发布排行

更多
  • 官方编辑

    仁师lv9

    文章

    6176

    评论

    4

    粉丝

    6

    关注

    0

    龙萱坤诺官方编辑员
  • 玩机技巧

    初学子lv0

    文章

    819

    评论

    2

    粉丝

    3

    关注

    0

    分享最新、最热的玩机教程。有需要的教程也可以圈子发帖提需求呦。
  • 微客创赢

    坛圣lv13

    文章

    338

    评论

    47

    粉丝

    16

    关注

    4

    程序员小白
  • wordpress建站

    俊才lv3

    文章

    233

    评论

    15

    粉丝

    3

    关注

    0

    Wordpress建站服务商
  • 龙萱坤诺

    坛圣lv13

    文章

    143

    评论

    87

    粉丝

    10

    关注

    5

    龙萱坤诺管理员