如何在 WordPress 文章编辑界面里管理文章相关的元数据

post-meta-data-management

元数据是用来描述数据的数据,在 WordPress 中元数据常用于对文章这样的内容类型做数据上的动态扩展,比如在之前的文章中介绍过的用于记录文章的点击数。WordPress 元数据支持多种数据类型,比如字符串、整数、数组等,使用的时候它们保存在后台的数据库中。

管理文章相关的元数据有两种方法,一种是在文章编辑界面右上角的菜单(三个竖形的点)里选择 “选项” 子菜单,在高级面板里选中 “自定义字段” 这一项,这时就会在文章编辑界面的下方出现自定义字段的管理界面,在这里可以选择要管理的元数据(已经存在),或者创建新的元数据。用这种方法可以方便地创建和管理字符串或整数类型的元数据,但文章元数据在一些场合是动态变化的,而且是随着用户浏览过程中做相应调整的,并不能在文章编辑的时候就静态地确定,同时也不能管理复杂的数据类型(数组)。这时候就需要使用第二种方法,就是编写程序动态管理元数据。

我们在这篇文章里已经介绍了动态创建、修改元数据的方法,详细内容可以去看一下那篇文章。下面主要介绍一下如何在文章编辑界面里面单独管理文章点击数这个元数据的。

首先就是创建一个文章点击数元数据的管理接口,把文章点击数提取并显示出来,代码如下:

function add_post_count_meta_box()
{
    $screens = ['post', 'wporg_cpt'];
    foreach ($screens as $screen) {
        add_meta_box(
            'post_count_box_id',	// 唯一
            'Post Conut Box',		// 标题
            'post_count_box_html',	// 显示管理界面,看下面的函数定义
            $screen			// 文章内容类型
        );
    }
}
add_action('add_meta_boxes', 'add_post_count_meta_box');
function post_count_box_html($post)
{
    $value = get_post_meta($post->ID, 'wpb_post_views_count', true);
    ?>
    <label for="post_count">Post views count:<?php echo $value ?></label>
    <?php
}

如果要编辑文章点击数,可以修改上面的代码,使用 Html <form> 标签,保存文章点击数元数据。把这段代码放到插件的主文件中或者主题的 functions.php 中。

《如何在 WordPress 文章编辑界面里管理文章相关的元数据》有10个想法

  1. I got this web site from my buddy who informed me concerning this site and at the moment this time I am browsing
    this website and reading very informative articles here.

  2. I believe that is one of the such a lot vital information for
    me. And i am happy studying your article. But wanna statement on few normal issues,
    The site taste is wonderful, the articles is in point of fact great : D.
    Good task, cheers

  3. When I initially commented I clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I get 4 emails with the identical comment. Is there any method you may take away me from that service? Thanks!

    1. Hi there,
      I don’t know what’s the situation you are, the theme, plugin etc. If you don’t want to receive notification, just to admin dashboard, then menu Settings->Discussion, and turn off Email me whenever ‘Anyone post comment’, ‘A comment is held for moderation’ checkbox.
      Another way, use filter of notify_moderator and notify_post_author, for example:
      add_filter( ‘notify_moderator’, ‘set_notify_moderator’, 10, 2);
      function set_notify_moderator( $maybe_notify, $comment_id ) {
      return false;
      }
      put code in functions.php, you will not receive email of the pending comments notification.

  4. Youre so cool! I dont suppose Ive learn something like this before. So nice to search out someone with some original ideas on this subject. realy thanks for starting this up. this website is something that’s wanted on the internet, somebody with slightly originality. helpful job for bringing one thing new to the internet!

    1. Hi, thank you for visiting, just a little site, hopefully that can help someone who need it, I am happy with that.

发表评论

邮箱地址不会被公开。 必填项已用*标注