简体   繁体   English

在foreach循环中增加

[英]increment inside a foreach loop

How do I loop through and output all the values in the array? 如何遍历并输出数组中的所有值? The code I have stops after the first output. 我的代码在第一个输出后停止。

$select_top = array($wpdb->get_results($wpdb->prepare( "SELECT * FROM `wp_top_voted` WHERE `todays_date`= CURDATE() ORDER BY `number_votes` DESC LIMIT 10", OBJECT )));

    if ($select_top){
    $i = 1; 
        foreach($select_top as $select_top_one){
        $select_top_one->post_id;
        $post_id_top[$i] = $select_top_one[$i]->post_id;    
            $number_votes[$i] = $select_top_one[$i]->number_votes;
            $size = array(380,220); 
?> 
<div class="top-layout">
  <div class="top-layout-row">
    <div class="top-layout-cell">
      <?php //remove background color. Make box look like widget. 
       ?> 
  <div class="top-layout-cell"> 
    <h4 class="top-video-title">
      <a href="<?php the_permalink(); ?>">
        <?php echo get_the_title( $post_id_top[$i] ); ?>
      </a>
    </h4>
    <a href="<?php the_permalink(); ?>">
      <?php echo get_the_post_thumbnail( $post_id_top[$i], $size ); ?>
    </a>
    <?php $i++; echo $i++; ?>
  </div>
</div>

You just loop all the results and print them out. 您只需循环所有结果并打印出来。 You don't need to auto increment the loop as the foreach runs for as long as there are results. 只要有结果,您就不需要在foreach运行时自动递增循环。 This seems to be Wordpress, so take a look at the get_posts function https://codex.wordpress.org/Template_Tags/get_posts . 这似乎是Wordpress,所以请看一下get_posts函数https://codex.wordpress.org/Template_Tags/get_posts This function works as what you're trying to do but you only need to insert your $args and then execute You'll have your array and you can loop that. 此函数的作用与您尝试执行的操作相同,但您只需要插入$ args然后执行即可。您将拥有数组,可以循环执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM