返回相应的数组值,以便在wordpress循环中使用

时间:2014-04-08 20:07:49

标签: php arrays wordpress

我在wordpress中执行以下代码。简要概述一下我想要实现的目标:我查询数据库以返回一个练习列表,值(练习的代表和集合)被分配给它们并存储在以下数组结构的$ _SESSION中。

Array
(
[0] => Array
    (
        [ExerciseID] => 75
        [Sets] => 
        [Reps] => 
    )

[1] => Array
    (
        [ExerciseID] => 690
        [Sets] => 3
        [Reps] => 3
    )

)

代码:我将练习ID返回到数组中,然后我可以使用它们运行WP_Query。返回结果时,我需要它们在练习中显示reps和sets(数组中的变量)。

<?php
$ids = array();
if(empty($_SESSION['collection']))
{echo"<p>There's nothing in your collection, an add some by searching above</p>";}
else
{
foreach($_SESSION['collection'] as $el)
{
 $ids[] = $el['ExerciseID'];
}
}
// START OF THE QUERY USING THE EXERCISE ID'S FOR DISPLAYING IN THE COLLECTION
$the_query = new WP_Query(array('post__in' => $ids, 'post_type' => 'exercise',   'posts_per_page' =>'100')); ?>


<?php if ( $the_query->have_posts() ) : ?>

<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>


<div class="thumbnail">

<div class="caption" id="<?php the_ID(); ?>">

  <h5>
    <?php the_title(); ?>
  </h5>
  <?php the_post_thumbnail('collection_thumb'); ?>
  </div>
 <br />
  <form action="" method="post">
  <button class="btn btn-primary btn-m" type="submit" name="delete" value="Delete">Delete</button>
</form>
</div>

我正在努力匹配代表和设置,以便与查询的练习一起显示。

通过单击此website上的练习并将其添加到集合中,希望您可以看到我想要实现的目标,为不良解释道歉。

1 个答案:

答案 0 :(得分:1)

将数组索引设置为ExerciseID,如下所示:

if ( isset( $_POST['exerciseExpand'] ) ) {
    $_SESSION['collection'][$_POST['ExerciseID']] = array( 
        'ExerciseID' => $_POST['ExerciseID'],
        'Sets' => $_POST['Sets'],
        'Reps' => $_POST['Reps'] 
    );
}

这将允许您将数据用作: <{1}}和$_SESSION['collection'][$exerciseID]['sets']