WordPress:添加字数统计作为所有帖子的字段

发布于 2025-01-15 01:44:00 字数 426 浏览 4 评论 0原文

在functions.php中,我创建了一个小函数来计算帖子中的单词数:

function prefix_wcount(){
    global $post;
    ob_start();
    the_content();
    $content = ob_get_clean();
    return sizeof(explode(" ", $content));
    $word_count = prefix_wcount();
}

现在我想向包含该数字的所有帖子添加一个字段。为此,我添加了

add_post_meta($post_id, $meta_key, $meta_value, $unique);

“This”,但并未添加此字段。我需要做什么才能实现这一点?

多谢

have in functions.php I have created a small function that counts words within a post:

function prefix_wcount(){
    global $post;
    ob_start();
    the_content();
    $content = ob_get_clean();
    return sizeof(explode(" ", $content));
    $word_count = prefix_wcount();
}

Now I would like to add a field to all posts that contains this number. For that I added

add_post_meta($post_id, $meta_key, $meta_value, $unique);

This does not add this field however. What do I need to do to make this happen?

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2025-01-22 01:44:00

您可以先创建一个元框并简单地显示它。从技术上讲,您不需要可编辑字段。

这样您可能需要调整您的初始字数统计功能。

// Create a meta box to display the word count

function wordcount_meta_box() {
    add_meta_box( 'box-1', __( 'Post Word Count', 'wcf' ), 'show_word_count', 'post' );
}
add_action( 'add_meta_boxes', 'wordcount_meta_box' );


// Create callback function to display data in meta box

function show_word_count( $post ) {
    $word_count = prefix_wcount( $post ); // Get the word count
    echo $word_count; // Display the count
}

You could create a meta box first and simply display it. Technically you wouldn't need an editable field.

With this you may need to adjust your initial word count function.

// Create a meta box to display the word count

function wordcount_meta_box() {
    add_meta_box( 'box-1', __( 'Post Word Count', 'wcf' ), 'show_word_count', 'post' );
}
add_action( 'add_meta_boxes', 'wordcount_meta_box' );


// Create callback function to display data in meta box

function show_word_count( $post ) {
    $word_count = prefix_wcount( $post ); // Get the word count
    echo $word_count; // Display the count
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文