为earch.php中的Foreach()提供的参数无效

时间:2015-11-26 作者:leanda

我正在搜索中使用此代码。php模板要显示搜索结果,帖子的格式会根据帖子的词条使用不同的模板部分。这种方法对索引非常有效。php,但我很难让它在搜索中发挥作用。php模板。它可以显示帖子,但会显示Invalid argument supplied for foreach() 线路警告foreach( $terms as $term ) {

有什么想法吗?谢谢

        <div class="container">
            <div class="gutter-sizer"></div>

            <?php

            $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
            global $query_string;

            $query_args = explode("&", $query_string);
            $search_query = array();

            foreach($query_args as $key => $string) {
                $query_split = explode("=", $string);
                $search_query[$query_split[0]] = urldecode($query_split[1]);
            }

            $search_query[\'post_type\'] = \'interpreted\'; // your custom post type
            $the_query = new WP_Query($search_query); ?>

            <?php if ( $the_query->have_posts() ) : ?>
                <div id="isotope-list">

                    <?php while ( $the_query->have_posts() ) : $the_query->the_post();
                    $termsArray = get_the_terms( $post->ID, "type" );  //Get the terms for this particular item
                    $termsString = ""; //initialize the string that will contain the terms
                    foreach ( $termsArray as $term ) { // for each term
                    $termsString .= $term->slug.\' \'; //create a string that has all the slugs
                }
                ?>

                <div class="<?php echo $termsString; ?> item interpreted"> <?php // \'item\' is used as an identifier (see Setp 5, line 6) ?>

                    <?php

                    $terms = get_the_terms( $post->id, \'type\', array( \'parent\' => 0 ) );
                    $terms_slugs = array();
                    foreach( $terms as $term ) {
                        $terms_slugs[] = $term->slug;
                    }

                    if( !empty($terms_slugs) ) :
                        get_template_part( \'blocks/block\', array_pop($terms_slugs) );
                    else : endif;
                    ?>

                </div> <!-- end item -->

            <?php endwhile;  ?>
        </div> <!-- end isotope-list -->

    </div> <!-- end container -->
    <?php cambridgerules_paging_nav(); ?>

<?php else : ?>

    <?php get_template_part( \'content\', \'none\' ); ?>

<?php endif; ?>

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

因为get_the_terms 不会始终返回数组;会的false 如果该职位没有条款,或WP_Error 如果分类不存在-在循环之前始终检查结果是否正常:

$terms_slugs = array();
$terms = get_the_terms( $post->id, \'type\', /* Note: get_the_terms does not take a 3rd argument ==> */ array( \'parent\' => 0 ) );

if ( is_array( $terms ) ) {
    foreach ( $terms as $term ) {
        $terms_slugs[] = $term->slug;
    }
}
更好的是,在这种情况下,您可以使用awesome helperwp_list_pluck():

if ( is_array( $terms = get_the_terms( $post->ID, \'type\' ) ) )
    $terms_slugs = wp_list_pluck( $terms, \'slug\' );
else
    $terms_slugs = array();

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请