警告:array_push()期望参数1是数组,第26行给出的对象

时间:2016-10-27 02:14:07

标签: php arrays warnings

我不完全确定为什么会出现在我的网页上?我看了一下代码,但在那一行看起来并不错。

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

Screen Shot Web Page

这是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(); ?>

1 个答案:

答案 0 :(得分:1)

documentation表示 get_terms()&#34;如果任何$ taxonomies不存在,将返回WP_Error。&#34;

您可以测试 $ 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
}