【问题标题】:Wordpress - Add a tag to an existing post, front-endWordpress - 为现有帖子添加标签,前端
【发布时间】:2015-08-18 20:10:59
【问题描述】:

我整天都在研究这个并没有找到一个优雅的解决方案,我想做的是在前端的现有帖子上标记一个用户 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    );
} ?>

【问题讨论】:

    标签: php wordpress frontend


    【解决方案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 );
    }
    

    【讨论】:

    • 不是要广泛道歉,还是习惯了 StackOverflow 的礼仪。但是,您的答案正是我想要的,谢谢!,我的最终代码在上面(在标准的 single.php 页面循环中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    相关资源
    最近更新 更多