警告:array_push()期望参数1是第26行给出的array, object


Warning: array_push() expects parameter 1 to be array, object given on line 26

我不完全确定为什么这出现在我的网站页面?我看了一下代码,但那一行似乎没有错。

这行是array_push行有些人说这是因为$taxonomy不是数组?但我真的不确定是什么问题。

网页截图

下面是php文件:

    <?php 
        /*
        Template Name: Portfolio
        */
        ?>
        <?php get_header(); ?>
        <?php
    // Add. Options Variables
    $portfolio_opts = get_post_meta(get_the_ID(), 'portfolio_additional_options', true);
    $is_filtration = is_array($portfolio_opts) && in_array('is_filtration', $portfolio_opts) ? true : false;
    $is_masonry = is_array($portfolio_opts) && in_array('is_masonry', $portfolio_opts) ? 'true' : 'false';
    // Taxonomy Variables
    $taxonomy = 'portfolio_category';
    $taxonomy_term_ID = get_post_meta(get_the_ID(), $taxonomy, true);
    $taxonomy_terms = get_terms( $taxonomy, array(
        'child_of'   => $taxonomy_term_ID,
        'hide_empty' => 0,
        'fields'     => 'ids',
    ) );
    array_push($taxonomy_terms, $taxonomy_term_ID); // add parent category to list
    // WP_QUERY Arguments
    $portfolio_args = array(
        'post_type' => 'portfolio',
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy,
                'field' => 'id',
                'terms' => $taxonomy_terms
            ),
        ),
        'posts_per_page'      => -1
    );
    $portfolio_query = new WP_Query($portfolio_args);
?>
<!-- BEGIN: SITE BODY -->
<section id="site-body" class="sections portfolio padding-size-l">
    <div class="container">
        <?php if ( $is_filtration ) { ?>
        <!-- BEGIN: FILTERATION -->
        <div class="filters-wrap">
            <ul class="filters nostyle">
                <li><a data-filter="*" class="active"><?php esc_html_e('All', 'lamark'); ?></a></li>
                <?php wp_list_categories(array('child_of' => $taxonomy_term_ID, 'title_li' => '', 'style' => 'none', 'taxonomy' => $taxonomy, 'show_option_none'   => '', 'walker' => new Lamark_Walker_Portfolio_Filter())); ?>
            </ul>
        </div>
        <!-- END: FILTERATION -->
        <?php } ?>
        <!-- BEGIN: PORTFOLIO GRID -->
        <section class="grid clearfix" data-col="<?php echo get_post_meta(get_the_ID(), 'portfolio_columns', true); ?>" data-margin="25" data-height="0.8" data-double-height="1.6" data-masonry="<?php echo $is_masonry; ?>">
            <?php if ( $portfolio_query->have_posts() ) : while ( $portfolio_query->have_posts() ) : $portfolio_query->the_post(); ?>
                <?php get_template_part( 'parts/portfolio-index-entry.inc' ); ?> 
            <?php endwhile; else: ?>
                <p class="entry"><?php printf( esc_html__( 'Ready to publish your first entry? <a href="%1$s">Get started here</a>.', 'lamark' ), esc_url( admin_url() ) ); ?></p> 
            <?php endif; ?>
        </section>
        <!-- END: PORTFOLIO GRID -->
    </div>
</section>
<!-- END: SITE BODY -->
<?php get_footer(); ?>

文档说明get_terms() "如果任何$分类法不存在,将返回WP_Error "。

您可以测试$taxonomy_terms以确保它是一个数组,如果不打印它是什么,也许像这样:

if (! is_array($taxonomy_terms)) {
  // show the issue
  die('<pre>'.print_r($taxonomy_terms, 1));
} else {
  // okay, no issue with that array...continue
  array_push($taxonomy_terms, $taxonomy_term_ID); // add parent category to list
}