Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取值列表,然后获取所有具有该值的帖子标题_Php_Arrays_Wordpress_Foreach_Meta Query - Fatal编程技术网

Php 获取值列表,然后获取所有具有该值的帖子标题

Php 获取值列表,然后获取所有具有该值的帖子标题,php,arrays,wordpress,foreach,meta-query,Php,Arrays,Wordpress,Foreach,Meta Query,我有一个wordpress循环,它显示某个meta\u查询的所有帖子。唯一的问题是这些值是重复的。例如,如果我有两篇文章的值为“Blue”,那么这两篇文章都会出现在循环中,这使得“Blue”出现两次 我想要的是“蓝色”出现一次,在它下面是所有具有该值的文章标题的列表 以下是我当前的查询: <?php $the_query = new WP_Query(array( 'post_type' => 'post', 'post_status' => 'p

我有一个wordpress循环,它显示某个
meta\u查询的所有帖子。唯一的问题是这些值是重复的。例如,如果我有两篇文章的值为“Blue”,那么这两篇文章都会出现在循环中,这使得“Blue”出现两次

我想要的是“蓝色”出现一次,在它下面是所有具有该值的文章标题的列表

以下是我当前的查询:

<?php 
$the_query = new WP_Query(array(
    'post_type'     => 'post',
    'post_status'   => 'publish',
    'meta_key'      => 'colors',
));

while ( $the_query->have_posts() ) : $the_query->the_post(); 

$colors = get_field('colors');

if( $colors ): foreach( $colors as $color ):  
    endforeach;
    endif; 
    echo' <div><h2>'.$color.'</h2><div>'.get_the_title( $post_id ).'</div></div>';

    endwhile; wp_reset_postdata();?>
echo
$title\u name


我想我需要另一个带有数组的if语句?或者我可能是从错误的方向来做这件事。

您可能想试试这样的东西:

$results = [];
while ( $the_query->have_posts() ) {

    $the_query->the_post(); 
    $colors = get_field('colors');
    if( !empty($colors) ) {

        foreach( $colors as $color ) {  
            $results [$color][]['title'] = get_the_title();
            $results [$color][]['link'] = get_attachment_link();
        }

    }

}

foreach ($results as $color => $posts) {

    echo "<div><h2>{$color}<h2>";

    foreach($posts as $post) {
        echo "<div><a href=\"{$post['link']}">{$post['title']}</a></div>";
    }

    echo '</div>';
}
$results=[];
while($the\u query->have\u posts()){
$the_query->the_post();
$colors=获取_字段(“颜色”);
如果(!空($colors)){
foreach($colors作为$color){
$results[$color]['title']=获取标题();
$results[$color]['link']=get_attachment_link();
}
}
}
foreach($color=>$posts的结果){
回声“{$color}”;
foreach($posts作为$post){
回声“;
}
回声';
}

太棒了!正是我想要的。如果我想让这些标题成为链接,比如
,我需要另一个数组,对吗?你可以使用一个关联数组,就像我更新我的答案一样谢谢,出于某种原因
不知道为什么会这样做。我需要看看你的新代码,但这是一个新问题。好的,谢谢,我将创建一个新的帖子。谢谢你的帮助!
$results = [];
while ( $the_query->have_posts() ) {

    $the_query->the_post(); 
    $colors = get_field('colors');
    if( !empty($colors) ) {

        foreach( $colors as $color ) {  
            $results [$color][]['title'] = get_the_title();
            $results [$color][]['link'] = get_attachment_link();
        }

    }

}

foreach ($results as $color => $posts) {

    echo "<div><h2>{$color}<h2>";

    foreach($posts as $post) {
        echo "<div><a href=\"{$post['link']}">{$post['title']}</a></div>";
    }

    echo '</div>';
}