【问题标题】:code stopped working after wordpress update 4.8.2wordpress 更新 4.8.2 后代码停止工作
【发布时间】:2017-10-24 12:32:11
【问题描述】:

我尝试了提供的代码,但仍然无法正常工作... 并且仅在更新到 wordpress 4.8.2 后才开始出现问题 我还复制了以下部分的代码,其中我关闭了 foreach 和 if ... 使用 Wordpress 4.7.6 可以完美运行... 任何的想法? 非常感谢您的帮助!

  <?php
        $cat_array = array();
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 9,
            'ignore_sticky_posts' => 1
        );
        $my_query = null;
        $my_query = new WP_Query($args);

        if ($my_query->have_posts())
        {
            while ($my_query->have_posts()):
                $my_query->the_post();
                $cat_args = array(
                    'orderby' => 'none'
                );

                $cats = wp_get_post_terms($my_query->post->ID, 'category', $cat_args);
                foreach($cats as $cat)
                {
                    $cat_array[$cat->term_id] = $cat->term_id;
                }

            endwhile;
            wp_reset_postdata();
        }

        if ($cat_array)
        {
            foreach($cat_array as $cat)
            {
                $category = get_term_by('ID', $cat, 'category');
                $slug = get_term_link($category, 'category');
                $id = $category->term_id;
                $valore = "category_" . $id;
                $colore = get_field('colore_categoria', $valore);
                $immagine = get_field('immagine_ispirazione', $valore);
                $testo_box = get_field('testo_box', $valore);


        ?>

        <div class="colonna clearfix">
            <a href="<?php echo $slug;?>">
                <div class="box">
                    <img src="<?php echo $immagine?>" alt="italia">
                    <div class="overlay">
                    <p><?php echo $testo_box;?></p>
                </div>
                <div class="titolobox"  style="background-color:<?php echo $colore;?>">
                    <h2><?php echo $category->name;?></h2>
                </div>
            </a>
        </div>

        </div>

    <?php
    }


}

wp_reset_query();
?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    使用'ignore_sticky_posts' 而不是'caller_get_posts'。 您可以参考this page更新您的查询

    【讨论】:

      【解决方案2】:

      我不知道您的要求,但我认为您没有关闭 foreach 循环和 if 条件,而且我认为您需要为您应该使用的帖子分配的类别的高级自定义字段值

      p>
      wp_get_post_categories( int $post_id, array $args = array() )
      

      在你的 while 循环中。希望对您有所帮助。

      你应该使用“ignore_sticky_posts”而不是“caller_get_posts”

      【讨论】:

        【解决方案3】:

        使用下面的代码。

        <?php
        $cat_array = array();
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 9,
            'ignore_sticky_posts' => 1
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        
        if ($my_query->have_posts())
        {
            while ($my_query->have_posts()):
                $my_query->the_post();
                $cat_args = array(
                    'orderby' => 'none'
                );
                $cats = wp_get_post_terms($my_query->post->ID, 'category', $cat_args);
                foreach($cats as $cat)
                {
                    $cat_array[$cat->term_id] = $cat->term_id;
                }
        
            endwhile;
            wp_reset_postdata();
        }
        
        if ($cat_array)
        {
            foreach($cat_array as $cat)
            {
                $category = get_term_by('ID', $cat, 'category');
                $slug = get_term_link($category, 'category');
                $id = $category->term_id;
                $valore = "category_" . $id;
                $colore = get_field('colore_categoria', $valore);
                $immagine = get_field('immagine_ispirazione', $valore);
                $testo_box = get_field('testo_box', $valore);
            }
        }
        
        ?>
        

        享受

        【讨论】:

          猜你喜欢
          • 2020-11-23
          • 1970-01-01
          • 1970-01-01
          • 2016-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-21
          相关资源
          最近更新 更多