Wordpress - 将标签添加到现有的 post,前端
Wordpress - Add a tag to an existing post, front-end
我整天都在研究这个问题,还没有找到一个优雅的解决方案,我想做的是在前端将用户 ID 标记到现有的 post 上(使用 Wordpress) .该过程将如下所示...
- post 有一个文本输入和提交按钮(前端)。
- 用户(已注册),在文本字段中输入标签并提交
- 标签被写入 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 );
} ?>
您的问题过于宽泛,需要有人为您构建完整的表单(包括处理逻辑)。这不是 Whosebug 的目的;所以我将回答您 post 标题中存在的简单问题:
How to add a tag to an existing post, from the front-end
答案:
在处理表单时使用 wp_set_post_tags()
将标签添加到 post。例如,如果您的表单输入名为 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 );
}
我整天都在研究这个问题,还没有找到一个优雅的解决方案,我想做的是在前端将用户 ID 标记到现有的 post 上(使用 Wordpress) .该过程将如下所示...
- post 有一个文本输入和提交按钮(前端)。
- 用户(已注册),在文本字段中输入标签并提交
- 标签被写入 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 );
} ?>
您的问题过于宽泛,需要有人为您构建完整的表单(包括处理逻辑)。这不是 Whosebug 的目的;所以我将回答您 post 标题中存在的简单问题:
How to add a tag to an existing post, from the front-end
答案:
在处理表单时使用 wp_set_post_tags()
将标签添加到 post。例如,如果您的表单输入名为 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 );
}