WordPress 自定义小部件 - 保存从自定义帖子类型创建的复选框列表

发布于 2024-12-07 07:37:46 字数 773 浏览 1 评论 0原文

这可能是一个非常标准的事情。我就是一辈子都想不出该怎么做。我检查了各种其他编码做类似的事情,但他们中的大多数似乎以与我不同的方式做事,我不太明白。

基本上我正在创建一个简单的自定义小部件。它从帖子类型中提取所有帖子并将其显示为复选框。我需要保存选择的帖子,然后将其作为数组传递,以便我可以显示所选的帖子。

要以我的表单显示复选框:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2024-12-14 07:37:46

即使代码不是动态的,代码本身也无法工作。
您需要做的就是重命名复选框的名称,否则只能访问最后一个值。例如,您可以这样做:

$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'.$postcount5.'" value="'.$the_id().'";
   if ( $currentid5 == $adboxid ) echo 'checked="yes"';
   echo '/> ';
   the_title();
   echo' </label><br/></p>';

要最终获取值,您需要一个提交按钮并将整个内容包装到带有一组“action”元素的中,例如:

<form name="postselector" action="whereever_you_want_the_user_to_go_next.php">
INSERT HERE ALL THE INPUT CHECKBOXES
AND THE SUBMIT BUTTON
</form>

在whereever_you_want_the_user_to_go_next.php中,您最终可以通过以下方式读取所选项目:

if (isset($_POST['submit'])) {
   $selectedposts = array();
   $i = 0;
   foreach($_POST as $name => $value) {
      if (preg_match('adverts',$name) {
         $selectedposts[$i] = $value;
         $i++;
      }
   }
}

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:

$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'.$postcount5.'" value="'.$the_id().'";
   if ( $currentid5 == $adboxid ) echo 'checked="yes"';
   echo '/> ';
   the_title();
   echo' </label><br/></p>';

And to finally get the values, you need a submit button and wrap the whole thing into a with a set 'action' element like:

<form name="postselector" action="whereever_you_want_the_user_to_go_next.php">
INSERT HERE ALL THE INPUT CHECKBOXES
AND THE SUBMIT BUTTON
</form>

In whereever_you_want_the_user_to_go_next.php you can finally read the selected items by:

if (isset($_POST['submit'])) {
   $selectedposts = array();
   $i = 0;
   foreach($_POST as $name => $value) {
      if (preg_match('adverts',$name) {
         $selectedposts[$i] = $value;
         $i++;
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文