【问题标题】:PHP Notice: Array to string conversion in taxonomy.php on line 3442 and category-template.php on line 1279PHP 注意:taxonomy.php 第 3442 行和 category-template.php 第 1279 行中的数组到字符串转换
【发布时间】:2021-01-18 06:38:45
【问题描述】:

我在我的 Wordpress 循环中收到了这 2 个 PHP 通知,我进行了全面研究,但我不知道如何解决这个问题。该脚本按预期工作,但通知出现在整个页面上。

注意:\wp-includes\taxonomy.php 第 3442 行中的数组到字符串的转换

注意:\wp-includes\category-template.php 第 1279 行中的数组到字符串的转换

我已经搜索了互联网,我认为这可能是导致问题的代码,但我无法弄清楚是什么破坏了它,无论我尝试什么,我似乎都无法修复它。请有人帮我解决这个问题并向我解释这个问题。

     <?php

        if ( $the_query->have_posts() ) : 
$i=1;
            // Start the Loop 
            while ( $the_query->have_posts() ) : $the_query->the_post(); 
          ?>
                <?php 
$terms = get_the_terms( $post->ID, array('date-category', 'programme-category') );
if ( $terms && ! is_wp_error( $terms ) ) :

    $catTag = array();
    foreach ( $terms as $term ) {
        $catTag[] = $term->slug;
    }

    $catTag = implode(" ", $catTag );
    ?>

【问题讨论】:

    标签: php wordpress loops while-loop


    【解决方案1】:

    来自文档:

    检索附加到帖子的分类术语。

    参数

    • $post (int|WP_Post)(必填)帖子 ID 或对象。
    • $taxonomy (字符串)(必需) 分类名称。

    get_the_terms() 期望第二个参数$taxonomy 是一个字符串,但您传递的是一个数组

    $terms = get_the_terms( $post->ID, array('date-category', 'programme-category') );
    

    这就是您看到“数组到字符串转换”错误消息的原因。

    您只能将一个分类法传递给该函数,而不是像您在那里所做的那样传递两个分类法。例如:

    $terms = get_the_terms( $post->ID, 'date-category' );
    

    或:

    $terms = get_the_terms( $post->ID, 'programme-category' );
    

    【讨论】:

      猜你喜欢
      • 2015-09-07
      • 2018-01-26
      • 2015-03-26
      • 2022-01-05
      • 1970-01-01
      • 2013-02-26
      • 2017-08-21
      • 2013-04-18
      • 1970-01-01
      相关资源
      最近更新 更多