2016-10-27 39 views
-1

我不完全確定爲什麼出現在我的網頁上?我查看了代碼,但在這條線上看起來並不錯。警告:array_push()期望參數1是數組,在第26行給出的對象

而行是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

你有你爲了解決這個問題,需要的所有信息。你想做什麼?什麼沒有用? – Dekel

回答

1

documentationget_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 
} 
相關問題