如何在Magic Fields Group Foreach循环内迭代?

时间:2015-07-02 作者:RachieVee

好的,所以我用Magic Fields 2 在一个旧的WordPress项目上(不幸的是不是ACF),我在编写PHP时遇到了一些困惑。

基本上,我在一个页面模板上,该模板从其帖子ID调用Magic Fields组,因为这些字段本身属于自定义帖子类型。它们只是显示在页面模板上。

然后,我循环遍历该组的字段,以获得一个ID值,该ID值只是选择另一篇带有魔法字段的文章Related Type field.

在使用var\\u dump进行调试之后,我的问题是,没有为每个选择了相关类型(当前有两个)的帖子提供ID值,而是重复第一个ID两次。

对我来说,这是一个PHP问题与魔术场。So how do I get EACH ID value within the existing foreach without resorting to another foreach? 我需要获取每个ID,然后在以后的PHP中使用这些ID。以下是我所拥有的:

$chall_entries = get_group(\'challenge_entry\', $post_id = 4643);
  foreach($chall_entries as $art_entry): //artisan challenges only
    if( $art_entry[\'challenge_entry_challenge_type\'][1] == \'Artisan\' ):
      global $task_post_id;
      $task_post_id = intval($art_entry[\'challenge_entry_assign_task\'][1]);
      echo $task_post_id; //THIS IS REPEATING THE SAME ID TWICE - WHY? 
      //HOW DO I GET EACH ID AND ITERATE THEM FROM THE MAGIC FIELDS
      //WITHOUT USING YET ANOTHER FOR EACH?
    endif;
  endforeach;

To give an idea of how the Magic Fields are set up on the dashboard, they\'re like this:

Custom Post Type w/ MFs
    Repeating Group of Fields (there are two groups right now)  
        Fields within each group

So if there are TWO groups which is how it is now:

Custom Post Type w/ MFs
    Group A  
        Group A Fields
    Group B  
        Group B Fields
所以问题是A组的字段是重复的,而不是循环的,以便第二次得到B组。。。

<小时/>

CODE UPDATE #1 - 7/6/2015

为了澄清,我们的目标是通过两个独立的MF组进行循环。一个组具有第二个组所需的ID。该ID是创建第二个组的循环所需的参数,但我需要该ID是动态的,而不是硬编码的,因此是第一个组的PHP。以下是;第二组“;如果有帮助:

$artisan_chall_task = get_group(\'task_entry\', $post_id=$task_post_id); //SEE THE ID HERE?
  foreach($artisan_chall_task as $art_chall_task):
    $task_ach_id = intval($art_chall_task[\'task_entry_achievement_id\'][1]); //achievement ID
    $task_ach_link = get_the_permalink($task_ach_id); //achievement link
    echo "<tr>";  
    echo "<td class=\'mobile-first\'>" . $art_chall_task[\'task_entry_task_name\'][1] . "</td>";
    echo "<td class=\'mobile-second\'>" . $art_chall_task[\'task_entry_task_desc\'][1] . "</td>";
    echo "<td class=\'mobile-third\'><div class=\'" . $art_chall_task[\'task_entry_task_difficulty\'][1] . "-stars margin-auto\'></div></td>";
    echo "<td class=\'mobile-fourth\'><a href=\'" . $task_ach_link . "\'>" . get_the_post_thumbnail( $task_ach_id, \'thumbnail\' ) . "</a></td>";
    echo "</tr>"; 
  endforeach;
<小时/>

Problems I\'m having so far with WebToure\'s Tips:

使用var\\u dump从$length变量中得到1,但如果我使用var\\u dump($art\\u entry[\'challenge\\u entry\\u assign\\u task\'),则该数组中有两个项目;这给了我:

      array(1) {
  [1]=&gt;
  string(4) "4644"
}
array(1) {
  [1]=&gt;
  string(4) "4652"
}
因此,带有$length的for循环给了我以下错误:

Undefined offset: 0
我还需要从第一个组中传递ID变量,在第一个组之外用作启动第二个组的参数-如果不使用全局,我怎么做?

<小时/>

CODE UPDATE #2 - 7/6/2015

我正在使用WebToure的函数解决方案。这在模板中:

foreach($chall_entries as $art_entry): //artisan challenges only
  if( $art_entry[\'challenge_entry_challenge_type\'][1] == \'Artisan\' ):
      $task_post_id = intval($art_entry[\'challenge_entry_assign_task\'][1]);
      //I GET MORE THAN ONE ID HERE IF I VAR_DUMP $task_post_id
  endif;
  endforeach;

  //OUT HERE THOUGH VAR_DUMP $task_post_id ONLY GIVES ME FIRST ID?
  //I WANT THE SECOND GROUP\'S LOOPING HERE
  //AND EACH ID GRABBED FROM THE FIRST GROUP WITHOUT BEING IN THAT FOREACH
  print_task_entry( $task_post_id );
这是在函数中。php:

function print_task_entry( $post_id ) {
$artisan_chall_task = get_group( \'task_entry\', $post_id ); // Passing the $post_id

foreach($artisan_chall_task as $art_chall_task):
    $task_ach_id = intval($art_chall_task[\'task_entry_achievement_id\'][1]); //achievement ID
    $task_ach_link = get_the_permalink($task_ach_id); //achievement link
    echo "<tr>";  
    echo "<td class=\'mobile-first\'>" . $art_chall_task[\'task_entry_task_name\'][1] . "</td>";
    echo "<td class=\'mobile-second\'>" . $art_chall_task[\'task_entry_task_desc\'][1] . "</td>";
    echo "<td class=\'mobile-third\'><div class=\'" . $art_chall_task[\'task_entry_task_difficulty\'][1] . "-stars margin-auto\'></div></td>";
    echo "<td class=\'mobile-fourth\'><a href=\'" . $task_ach_link . "\'>" . get_the_post_thumbnail( $task_ach_id, \'thumbnail\' ) . "</a></td>";
    echo "</tr>"; 
endforeach;    
}

<小时/>

The Final Working Solution - 7/7/2015:

看起来问题是PHP和WebToure的混合,WebToure的解决方案很好,我有一个模式插件。因此,似乎所有的结果都是在一个模态中同时打印出来的,而每个都有自己的模态。我只需要确保每个结果都有自己的模态,并使用PHP生成的计数器类。但最终,这是最终结果(没有模式代码或计数器)。

在模板文件中:

$chall_entries = get_group(\'challenge_entry\', $post_id = 4643);
foreach($chall_entries as $art_entry): //artisan challenges only
    if( $art_entry[\'challenge_entry_challenge_type\'][1] == \'Artisan\' ):
        $task_post_id = intval($art_entry[\'challenge_entry_assign_task\'][1]);
        print_task_entry( $task_post_id );
    endif;
endforeach;
在函数中。php:

function print_task_entry( $post_id ) {
$artisan_chall_task = get_group( \'task_entry\', $post_id ); // Passing the $post_id

foreach($artisan_chall_task as $art_chall_task):
    $task_ach_id = intval($art_chall_task[\'task_entry_achievement_id\'][1]); //achievement ID
    $task_ach_link = get_the_permalink($task_ach_id); //achievement link
    echo "<tr>";  
    echo "<td class=\'mobile-first\'>" . $art_chall_task[\'task_entry_task_name\'][1] . "</td>";
    echo "<td class=\'mobile-second\'>" . $art_chall_task[\'task_entry_task_desc\'][1] . "</td>";
    echo "<td class=\'mobile-third\'><div class=\'" . $art_chall_task[\'task_entry_task_difficulty\'][1] . "-stars margin-auto\'></div></td>";
    echo "<td class=\'mobile-fourth\'><a href=\'" . $task_ach_link . "\'>" . get_the_post_thumbnail( $task_ach_id, \'thumbnail\' ) . "</a></td>";
    echo "</tr>"; 
endforeach;    
}

谢谢WebToure!:-)

1 个回复
SO网友:webtoure

Update #2:使用我的第一个选项获取数组中的所有ID,然后循环这些ID。我一直在努力理解您到底想实现什么(使用相同的变量来保存ID,并以某种方式在第一块代码下面执行异步操作,这是“第一组”?!)我只能得出结论,您对PHP的工作原理有一些深刻的误解。

然而,我可以理解,您需要所有ID,然后遍历它们,在这种情况下,我建议的第一个选项是exactly 你需要什么。

$post_ids = array();

foreach ( $chall_entries as $art_entry ): //artisan challenges only
    if ( $art_entry[\'challenge_entry_challenge_type\'][1] == \'Artisan\' ):
        // I have NO IDEA what you mean about: 
        // "Because then Group Loop 1 will repeat for each Group Loop 1 and I don\'t want that to happen"
        $post_ids[] = (int)$art_entry[\'challenge_entry_assign_task\'][1];        
    endif;
endforeach;

foreach ( $post_ids as $post_id ):
    $artisan_chall_task = get_group( \'task_entry\', $post_id ); // Passing the $post_id

    foreach ( $artisan_chall_task as $art_chall_task ):
        $task_ach_id = intval($art_chall_task[\'task_entry_achievement_id\'][1]); //achievement ID
        $task_ach_link = get_the_permalink($task_ach_id); //achievement link
        echo "<tr>";  
        echo "<td class=\'mobile-first\'>" . $art_chall_task[\'task_entry_task_name\'][1] . "</td>";
        echo "<td class=\'mobile-second\'>" . $art_chall_task[\'task_entry_task_desc\'][1] . "</td>";
        echo "<td class=\'mobile-third\'><div class=\'" . $art_chall_task[\'task_entry_task_difficulty\'][1] . "-stars margin-auto\'></div></td>";
        echo "<td class=\'mobile-fourth\'><a href=\'" . $task_ach_link . "\'>" . get_the_post_thumbnail( $task_ach_id, \'thumbnail\' ) . "</a></td>";
        echo "</tr>"; 
    endforeach;

endforeach;

Update #1:

从发布的代码来看,在执行foreach 循环,而不是在每次传递后从“第二组”块执行代码。在我看来,您有两种选择:要么声明array 在“第一组”代码之前$post_ids = array() 并将每个ID推送到“第一组”代码中的数组:

$post_ids[] = intval($art_entry[\'challenge_entry_assign_task\'][1]);
完成后,您可以继续执行以下操作:

foreach ( $post_ids as $post_id ):

    $artisan_chall_task = get_group( \'task_entry\', $post_id ); // Passing the $post_id

    foreach($artisan_chall_task as $art_chall_task):
        $task_ach_id = intval($art_chall_task[\'task_entry_achievement_id\'][1]); //achievement ID
        $task_ach_link = get_the_permalink($task_ach_id); //achievement link
        echo "<tr>";  
        echo "<td class=\'mobile-first\'>" . $art_chall_task[\'task_entry_task_name\'][1] . "</td>";
        echo "<td class=\'mobile-second\'>" . $art_chall_task[\'task_entry_task_desc\'][1] . "</td>";
        echo "<td class=\'mobile-third\'><div class=\'" . $art_chall_task[\'task_entry_task_difficulty\'][1] . "-stars margin-auto\'></div></td>";
        echo "<td class=\'mobile-fourth\'><a href=\'" . $task_ach_link . "\'>" . get_the_post_thumbnail( $task_ach_id, \'thumbnail\' ) . "</a></td>";
        echo "</tr>"; 
    endforeach;

endforeach;
另一种选择是将“第二组”代码包装在一个函数中,该函数接受一个参数,该参数是post的ID:

function print_task_entry( $post_id ) {
    $artisan_chall_task = get_group( \'task_entry\', $post_id ); // Passing the $post_id

    foreach($artisan_chall_task as $art_chall_task):
        $task_ach_id = intval($art_chall_task[\'task_entry_achievement_id\'][1]); //achievement ID
        $task_ach_link = get_the_permalink($task_ach_id); //achievement link
        echo "<tr>";  
        echo "<td class=\'mobile-first\'>" . $art_chall_task[\'task_entry_task_name\'][1] . "</td>";
        echo "<td class=\'mobile-second\'>" . $art_chall_task[\'task_entry_task_desc\'][1] . "</td>";
        echo "<td class=\'mobile-third\'><div class=\'" . $art_chall_task[\'task_entry_task_difficulty\'][1] . "-stars margin-auto\'></div></td>";
        echo "<td class=\'mobile-fourth\'><a href=\'" . $task_ach_link . "\'>" . get_the_post_thumbnail( $task_ach_id, \'thumbnail\' ) . "</a></td>";
        echo "</tr>"; 
    endforeach;    
}
“第一组”的代码如下所示

$chall_entries = get_group(\'challenge_entry\', $post_id = 4643);
foreach($chall_entries as $art_entry): //artisan challenges only
    if( $art_entry[\'challenge_entry_challenge_type\'][1] == \'Artisan\' ):
        // No need for any global variable!
        $task_post_id = intval($art_entry[\'challenge_entry_assign_task\'][1]);
        print_task_entry( $task_post_id );
    endif;
endforeach;

Original:

我发现您的代码存在几个潜在问题:

$art_entry[\'challenge_entry_challenge_type\'][1] == \'Artisan\'

假设您想要art_entry[\'challenge_entry_challenge_type\'] 数组,则该行应为:

$art_entry[\'challenge_entry_challenge_type\'][0] == \'Artisan\'

如您所见,数组中的第一个元素位于索引0处。

其次,不要使用global 关键词:global $task_post_id;. 实际上,您可以一起删除该行。将该变量声明为global 除非你明确知道自己在做什么,否则可能导致不可预测的行为。Global 这意味着,如果在代码中的其他地方使用相同的全局变量和名称变量组合,它可能会被覆盖,并且您的值可能不是您期望的值。

三是intval 在循环过程中,调用可能会对代码性能产生重大影响,所以请避免这种情况。如果确实需要确保该值是整数,可以使用(int). 所以这条线应该是这样的:

$task_post_id = (int)$art_entry[\'challenge_entry_assign_task\'][1];

最后,我不完全清楚您要查找的所有ID是否都在这个数组变量中:$art_entry[\'challenge_entry_assign_task\']

如果它们位于索引处,如$art_entry[\'challenge_entry_assign_task\'][0], $art_entry[\'challenge_entry_assign_task\'][1], $art_entry[\'challenge_entry_assign_task\'][2] 如果您想以增量方式访问特定索引处的值,那么您肯定需要一个循环机制。

我可以从您添加的注释中看到,您确实希望遍历该数组:[...] I don\'t want to manually write [1], [2], [3] etc. How do I iterate through that dynamically? – RachieVee 22 hours ago

在这种情况下是经典for 可以使用循环机制:

$length = count( $art_entry[\'challenge_entry_assign_task\'] ); // Get number of elements in array

for ( $i = 0; $i < $length; $i++ ) {
    $task_post_id = (int)$art_entry[\'challenge_entry_assign_task\'][$i]; // Accesing the element at index $i
    echo $task_post_id;
}
我希望这有助于解决问题。

结束