doudu8291 2017-04-25 14:14
浏览 16
已采纳

Wordpress帖子分为两类。 如何在家中展示一次?

I have a loop that displays the posts from Wordpress by category, but some posts are in two categories so on the home page where all the posts are shown, these post appears twice.

Example:

  • "Example Post" is in "Category One" and "Category Two"
  • When you see the posts of "Category One", "Example Post" is there. The same happens when you see the posts of "Category Two".
  • But when you click "see all posts", "Example Post" shows twice (because it's in two categories)

Does anyone know how to "hide" this repeated posts?

This is the loop:

<?php 

    $loop = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => -1 ) );

    if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>


        <?php 
          $my_post_child_cats = array();
            foreach((get_the_category()) as $childcat) { 
                if (cat_is_ancestor_of(3, $childcat)) { ?>


                <div class="item"  data-groups='["all", "<?php echo $childcat->cat_name; ?>"]'>
                    <div class="pindex">
                        <?php if ( has_post_thumbnail() ) { ?>
                            <a href="<?php the_permalink(); ?>">
                            <div class="pimage">
                                <?php the_post_thumbnail(); ?>
                                <div class="pro-title">
                                    <h4><?php echo get_the_title(); ?></h4>
                                    <p>Read more</p></a>
                                </div>
                            </div>
                        <?php } ?>

                    </div>
                </div>

        <?php } } ?>

        <?php endwhile; ?>

    <?php endif;
    wp_reset_postdata();
    ?>

This code will get all the posts and add the category name with this piece: <?php echo $childcat->cat_name; ?>

Then in the home page I've created a filter by category which will take the posts by that $childcat->cat_name;, but when you click "All posts" it will get the posts twice as it has two categories

  • 写回答

2条回答 默认 最新

  • dongyong6332 2017-04-25 20:02
    关注

    You could store each post name in an array and then only output it if it's not already been displayed. Something like this:

    <?php 
    $array[];
    $loop = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => -1 ) );
    
    if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    
        <?php 
          $my_post_child_cats = array();
            foreach((get_the_category()) as $childcat) { 
                if (cat_is_ancestor_of(3, $childcat)) { ?>
    
    
                <div class="item"  data-groups='["all", "<?php echo $childcat->cat_name; ?>"]'>
                    <div class="pindex">
    
                        <?php if ( has_post_thumbnail() && !in_array(get_the_title(), $array) ) { ?>
                            <a href="<?php the_permalink(); ?>">
                            <div class="pimage">
                                <?php the_post_thumbnail(); ?>
                                <div class="pro-title">
                                    <h4><?php echo get_the_title();
                                     $array[] = get_the_title();
                                     ?></h4>
                                    <p>Read more</p></a>
                                </div>
                            </div>
                        <?php } ?>
    
                    </div>
                </div>
    
        <?php } } ?>
    
        <?php endwhile; ?>
    
    <?php endif;
    wp_reset_postdata();
    ?>
    

    You'll possibly need to tweak the code to suit your need but what this code does is essentially grabs the name/title of the post and stores it in an array:

    $array[] = get_the_title();
    

    Then on subsequent loops checks to see if the title of the current looped post is in the array:

    if ( has_post_thumbnail() && !in_array(get_the_title(), $array) )
    

    If it is, then nothing should be output. If it isn't then we output the relevant content and store the title of the post in to the array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度