Foreach仅显示一个结果,而不是全部

时间:2019-01-25 07:10:40

标签: php wordpress

我正在尝试仅从一个类别中选择元素并将其显示在页面上。 目前,我在该类别中有5个帖子,但在页面上我仅看到一个。

那是为什么?

这是我的尝试

PageLoad

当我 protected void Page_Load(object sender, EventArgs e) { if (Session["UserEmployee"] != null) { userEmployeeNumber = Convert.ToString(Session["UserEmployee"]); GetUserData(); ShowNotification("Welcome! Mr/Mrs " + EmployeeID.UserName.ToString() + "", WarningType.Success); } } private void GetUserData() { using (SqlConnection con = new SqlConnection(Base.GetConnection)) { con.Open(); using (SqlCommand cmd = new SqlCommand("SELECT [UserName],[UserContact],[UserEmpNum],[UserDesignation],[UserDepartment] FROM TableUserProfile WHERE UserEmpNum=@UserEmpNum", con)) { cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("UserEmpNum", userEmployeeNumber); SqlDataReader r = cmd.ExecuteReader(); while (r.Read()) { tbName.Value = r["UserName"].ToString(); EmployeeID.UserName = tbName.Value.ToString(); tbMobileNo.Value = r["UserContact"].ToString(); tbEmployeeNo.Value = r["UserEmpNum"].ToString(); tbDesignation.Value = r["UserDesignation"].ToString(); tbDepartment.Value = r["UserDepartment"].ToString(); } } } } 时,我只会看到一个结果。这个:

<?php
$args = array(   
    'showposts'=>-1,
    'category_name' => 'custom-page',
); 
$query = new WP_Query( $args ); 
$aSolutionsePost = array();
if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $aSolutionsePost = array();
            $query->the_post();
            $aSolutionsePost['title'] = $query->post->post_title;
            $aSolutionsePost['content'] = $query->post->post_content;
        }
}
?>

<div class="col-4 ">
    <ul class="price">
    <?php if(!empty($aSolutionsePost)){?>

        <?php foreach($aSolutionsePost as $item){ ?>
        <li class="header"><?php echo $item->title; ?></li>
        <li class="grey"><?php echo apply_filters('the_content',$item->content);?></li>
        <?php }?>               
    <?php }?>    
    </ul>
</div>

4 个答案:

答案 0 :(得分:1)

while ( $query->have_posts() )循环中的以下代码行:

$aSolutionsePost = array();

每次循环都覆盖$aSolutionsePost的值。您可能想要这样的东西:

while ( $query->have_posts() ) {
    $query->the_post();
    $aSolutionsePost[] = array('title' => $query->post->post_title,
                               'content' => $query->post->post_content);
}

请注意,在回显结果的代码中,您将数组元素视为对象,而不是关联数组。只需将这些行更改为此:

<li class="header"><?php echo $item['title']; ?></li>
<li class="grey"><?php echo apply_filters('the_content',$item['content']);?></li>

但是如果您希望保持该代码不变,则可以将分配行更改为:

$aSolutionsePost[] = (object)array('title' => $query->post->post_title,
                                   'content' => $query->post->post_content);

答案 1 :(得分:0)

同时检查内部设置的数组,

您每次都重新创建$aSolutionsePost,因此它将被重置。 解决方案是创建新数组并将其附加到$aSolutionsePost,请检查以下代码段。

while ( $query->have_posts() ) {
    $tmpSolutionsePost = array();
    $query->the_post();
    $tmpSolutionsePost['title'] = $query->post->post_title;
    $tmpSolutionsePost['content'] = $query->post->post_content;
    $aSolutionsePost[] = $tmpSolutionsePost;
}

答案 2 :(得分:0)

您实际上是在循环中使用新值更新同一数组。

$finalData =array(); 
 while ( $query->have_posts() ) {
            $aSolutionsePost = array();
            $query->the_post();
            $aSolutionsePost['title'] = $query->post->post_title;
            $aSolutionsePost['content'] = $query->post->post_content;
            $finalData[] = $aSolutionsePost;
        }

现在遍历$finalData

答案 3 :(得分:0)

这是因为您要一次又一次地将元素添加到数组的同一键,这就是为什么数组中只有1个元素的原因,因此foreach仅显示1个元素。 您可能应该采用2d阵列来解决此问题。