Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Arrays 在类别归档页面中获取分类_Arrays_Wordpress_Taxonomy - Fatal编程技术网

Arrays 在类别归档页面中获取分类

Arrays 在类别归档页面中获取分类,arrays,wordpress,taxonomy,Arrays,Wordpress,Taxonomy,我想在一个分类页面(归档)中显示与帖子相关的所有分类法 我有以下代码 <?php $cat1 = $cat; $args = array( 'posts_per_page' => -1, 'category' => $cat1 );?> $myposts = get_posts( $args ); foreach ( $myposts as $post ) { $product_terms = wp_get_object_

我想在一个分类页面(归档)中显示与帖子相关的所有分类法

我有以下代码

<?php 
    $cat1 = $cat;
    $args = array( 'posts_per_page' => -1, 'category' => $cat1 );?>
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) {
        $product_terms = wp_get_object_terms($post->ID, 'Brands');
        echo '<ul>';
        foreach(array_unique($product_terms) as $term) {
            echo '<li><a href="'.get_term_link($term->slug, 'Brands').'">'.$term->name.'</a></li>'; 
        }
        echo '</ul>';
    }
?>

$myposts=get_posts($args);
foreach($myposts作为$post){
$product_terms=wp_get_object_terms($post->ID,'Brands');
回声“
    ”; foreach(数组\唯一($product\术语)作为$term){ 回音“
  • ”; } 回声“
”; } ?>

但它返回分类法的重复项。我尝试了array_unique,但它不起作用。你能帮忙吗。谢谢

将循环分开,如下所示:

<?php 
    $cat1 = $cat;
    $args = array( 'posts_per_page' => -1, 'category' => $cat1 );?>
    $myposts = get_posts( $args );
    $myterms = array();
    foreach ( $myposts as $mypost ) {
        foreach ( wp_get_object_terms($mypost->ID, 'Brands') as $term ) {
            $myterms[ $term->term_id ] = $term;
        }
    }
    echo '<ul>';
    foreach( $myterms as $term ) {
        echo '<li><a href="'.get_term_link($term->slug, 'Brands').'">'.$term->name.'</a></li>'; 
    }
    echo '</ul>';
?>

$myposts=get_posts($args);
$myterms=array();
foreach($myposts作为$mypost){
foreach(wp_get_object_terms($mypost->ID,'Brands')作为$term){
$myterms[$term->term\u id]=$term;
}
}
回声“
    ”; foreach($myterms作为$term){ 回音“
  • ”; } 回声“
”; ?>

如果有许多术语,上述方法可能会导致PHP内存错误。以下是另一种方法:

<?php 
    $cat1 = $cat;
    $args = array( 'posts_per_page' => -1, 'category' => $cat1 );?>
    $myposts = get_posts( $args );
    $visited_term_ids = array();
    echo '<ul>';
    foreach ( $myposts as $mypost ) {
        foreach ( wp_get_object_terms($mypost->ID, 'Brands') as $term ) {
            if ( ! isset( $visited_term_ids[ $term->term_id ] ) ) {
                echo '<li><a href="'.get_term_link($term->slug, 'Brands').'">'.$term->name.'</a></li>'; 
                $visited_term_ids[ $term->term_id ] = TRUE;
            }
        }
    }
    echo '</ul>';
?>

$myposts=get_posts($args);
$VISTER_term_ids=array();
回声“
    ”; foreach($myposts作为$mypost){ foreach(wp_get_object_terms($mypost->ID,'Brands')作为$term){ 如果(!isset($VISTER\U term\U id[$term->term\U id])){ 回音“
  • ”; $VISTER\u term\u id[$term->term\u id]=真; } } } 回声“
”; ?>
感谢frnhr的回复。不幸的是,你的建议不起作用。我错在哪里?感谢您的支持它到底是如何崩溃的(我不是说我不知道它是如何工作的,我在这里编写的代码没有经过测试)?将
var\u dump($myterms)
放在第一个循环之后,查看输出,这应该会提供一些错误线索。没有具体错误,只是页面没有显示,或者更好的是,我得到了标题,然后就没有了。。。。但是在你的第二个建议之后,我有:数组(295){[0]=>object(stdClass){11933(9){[“term\u id”]=>string(3)“471”[“name”]=>string(4)“Asus”[“slug”]=>string(4)“Asus”[“term\u group”]=>string(1)“term\u-taxonomology\u-id”=>string(3)“481”[“taxonomology”]=>string(6)“Marche”[“description”[“description”]=>string(0)”如果我删除唯一的数组()它向我展示了分类法列表(品牌或Marche(意大利语)),但它仍然是重复的