WordPress 自定义小部件 - 保存从自定义帖子类型创建的复选框列表
这可能是一个非常标准的事情。我就是一辈子都想不出该怎么做。我检查了各种其他编码做类似的事情,但他们中的大多数似乎以与我不同的方式做事,我不太明白。
基本上我正在创建一个简单的自定义小部件。它从帖子类型中提取所有帖子并将其显示为复选框。我需要保存选择的帖子,然后将其作为数组传递,以便我可以显示所选的帖子。
要以我的表单显示复选框:
$postcount5 = 0; $featured_query5 = new WP_Query('showposts=5&post_type=adverts');
while ($featured_query5->have_posts()) : $featured_query5->the_post();
$do_not_duplicate[] = get_the_ID();$postcount5++;
$currentid5 = get_the_ID();
echo '<p><label><input type="checkbox" name="adverts" value="';
the_id();
echo'" ';
if ( $currentid5 == $adboxid ) echo 'checked="yes"';
echo '/> ';
the_title();
echo' </label><br/></p>';
一旦我成功保存它们,我应该没问题。我只是不知道如何保存动态创建的复选框列表。提前致谢。
This is probably a pretty standard thing to do. I just can't work out for the life of me how to do it. I checked various other coded doing similar things but most of them seem to do things in a different way from me which I don't quite understand.
Basically I am creating a simple custom widget. It pulls in all the post from a post type and displays them as checkboxes. I need to save which posts were selected and then pass this on as an array so that I can then display the selected posts.
To display the checkboxes in a form I have:
$postcount5 = 0; $featured_query5 = new WP_Query('showposts=5&post_type=adverts');
while ($featured_query5->have_posts()) : $featured_query5->the_post();
$do_not_duplicate[] = get_the_ID();$postcount5++;
$currentid5 = get_the_ID();
echo '<p><label><input type="checkbox" name="adverts" value="';
the_id();
echo'" ';
if ( $currentid5 == $adboxid ) echo 'checked="yes"';
echo '/> ';
the_title();
echo' </label><br/></p>';
Once I have managed to save them I should be fine. I just cannot work out how to save a list of dynamically created checkboxes. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使代码不是动态的,代码本身也无法工作。
您需要做的就是重命名复选框的名称,否则只能访问最后一个值。例如,您可以这样做:
要最终获取值,您需要一个提交按钮并将整个内容包装到带有一组“action”元素的中,例如:
在whereever_you_want_the_user_to_go_next.php中,您最终可以通过以下方式读取所选项目:
The code as it is, wouldn't work even if it wasn't dynamically.
What you need to do is rename the name of the checkboxes too, otherwise only the last value will be accessible. E.g. you could do it like so:
And to finally get the values, you need a submit button and wrap the whole thing into a with a set 'action' element like:
In whereever_you_want_the_user_to_go_next.php you can finally read the selected items by: