Wordpress - 将标签添加到现有帖子前端

时间:2015-08-18 20:10:59

标签: php wordpress frontend

我整天都在研究这个并没有找到一个优雅的解决方案,我正在尝试将用户ID标记到前端的现有帖子上(使用Wordpress)。该流程将遵循以下广告......

  • 帖子有一个文本输入和提交按钮(前端)。
  • 用户(已注册),在文本字段中输入标签并提交
  • 标签写入帖子,(理想情况下不使用ajax进行刷新,但不是必需的。)

似乎它应该是直截了当但我找不到任何相关信息,提前谢谢。

编辑:工作,最终代码看起来像这样,谢谢!

<form name="primaryTagForm" id="primaryTagForm" method="POST" enctype="multipart/form-data" >
    <fieldset>
        <input type="text" name="newtags" id="newtags" value="true" />
        <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
        <button class="button" type="submit"><?php _e('Tag Product', 'framework') ?></button>
    </fieldset>
</form>

<?php 
    // If a user is logged in, and a form has been submitted with new tags
    if ( is_user_logged_in() && isset( $_POST['newtags'] ) ) {
    // Add the tags
    wp_set_post_tags(get_the_ID(), sanitize_text_field($_POST['newtags']), true    );
} ?>

1 个答案:

答案 0 :(得分:1)

您的问题过于宽泛,需要有人为您构建整个表单(包括处理逻辑)。这不是StackOverflow的用途;所以我会回答帖子标题中存在的简单问题:

  

如何从前端

向现有帖子添加标签

答案:

在处理表单时,使用wp_set_post_tags()向帖子添加标签。例如,如果表单输入名为newtags,则可以使用以下内容:

// If a user is logged in, and a form has been submitted with new tags
if ( is_user_logged_in() && isset( $_POST['newtags'] ) ) {
    // Add the tags
    wp_set_post_tags(get_the_ID(), sanitize_text_field($_POST['newtags']), true );
}