我一整天都在研究这个问题,还没有找到一个优雅的解决方案,我想做的是在前端(使用Wordpress)的现有帖子上标记一个用户id。流程如下…
- 文章有一个文本输入和提交按钮(前端)。
- 用户(已注册),在文本域中输入标签并提交
- 标签被写入到post中,(理想情况下不需要使用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 );
} ?>
你的问题太宽泛了,需要有人为你建立一个完整的表单(包括处理逻辑)。这不是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 );
}