猿问

如何通过单击按钮更改帖子类别

我正在尝试创建一个“已批准”按钮以将帖子类别从当前类别更改为“已批准”类别。我不介意它是否重新加载页面。我还想将页面重定向到原始类别中的下一篇文章。


我已经发现了一些关于此的问题,但最终我不知道如何将所有这些整合在一起并工作。


<?php add_shortcode('approved_button', 'brist_approved_button_function');


function brist_approved_button_function() {

ob_start(); ?>


<form method="post" action="approved.php">

    <input type="submit" value="Approved" name="submit"> <!-- assign a name for the button -->

</form>



<?php


wp_set_object_terms( $post_id, intval( $_POST['approved'] ), 'category', false );

$output = ob_get_clean();

return $output;

}?>


繁星点点滴滴
浏览 201回答 1
1回答

慕田峪9158850

我想到了。虽然这对我来说是一个头疼的问题。如果其他人遇到同样的问题,请注意下面的代码。您必须创建选择,然后在您希望帖子更改为的类别上设置“已选择”选项。然后,在 CSS 中,隐藏选择输入,只留下按钮。<?php add_shortcode('approved_button', 'approved_button_function');?><?php function approved_button_function() { ob_start();?>&nbsp; &nbsp; <div class="approval">&nbsp; &nbsp; &nbsp; &nbsp; <form action="" id="update-post" method="post">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php wp_dropdown_categories( "selected='categoryId'&exclude=21&class=approval-select&show_count=1&hierarchical=1&orderby=name&order=ASC&&hide_empty=0&show_option_all=Choose An Option" ); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input class="approval-button" type="submit" name="submit" value="Approve" />&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </div><?php if ( array_key_exists('cat', $_POST )) {&nbsp; &nbsp; global $post;&nbsp; &nbsp; $post_id = $post->ID;&nbsp; &nbsp; wp_set_object_terms( $post_id, intval( $_POST['cat'] ), 'category', false );} ?>
随时随地看视频慕课网APP
我要回答