为WordPress文章自动添加自定义栏目

自定义栏目是WordPress 一个非常强大的功能,借助它我们可以实现很多特殊的功能。本文的一段代码可以为文章自动添加预先设置好的自定义栏目名称及值,而无需手动添加。

WordPress自定义栏目

将下面的代码添加到当前主题的 functions.php 文件:

  1. add_action(\’publish_page\’, \’add_custom_field_automatically\’);
  2. add_action(\’publish_post\’, \’add_custom_field_automatically\’);
  3.  add_custom_field_automatically() {
  4.      ;
  5.     (!wp_is_post_revision()) {
  6.         add_post_meta(, \’field-name\’, \’custom value\’, true);
  7.     }
  8. }

你只需替换其中的自定义栏目名称:field-name,值:custom value

之后,当你发布文章后,相应的自定义栏目名称及值会自动添加到文章中。

代码源自

    © 版权声明
    THE END
    喜欢就支持一下吧
    点赞0 分享
    评论 抢沙发

    请登录后发表评论

      暂无评论内容