最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - WordPress Custom Post Loop

programmeradmin1浏览0评论

I am trying to loop through the custom WordPress posts and have an issue like I add the custom field and want to display it in <li> using loop. I did it successfully but the data/link/category is repeating and I want it to show only once if the category is the same as:

If I have 2 posts with the data1 category then the link will only show data1 once but I have 2 posts with different category then it will show each separately.

Sample Code:

<ul class="filter filter-top">
        <li class="active"><a href="#" data-filter="*">All</a></li>
        <?php
          $portfolio_args_custom = array(
            'post_type' => 'portfolio',
          );
          $portfolio_posts_custom = new WP_Query($portfolio_args_custom);
          while($portfolio_posts_custom->have_posts()) {
            $portfolio_posts_custom->the_post();

       ?>
         <li><a href="#" data-filter=".<?php echo get_post_meta( get_the_ID(), 'Category', true);?>"><?php echo get_post_meta( get_the_ID(), 'Category', true);?></a></li>
          <?php   wp_reset_postdata();
          wp_reset_query();
        }?>
      </ul>
     

The code is working fine but it is repeating the <li> item with the same category. Please provide any documentation/helping link/ suggestion.

Edit:

This code works as I want but It's not clear for me, please explain if possible.

 <ul class="filter filter-top">
            <li class="active"><a href="#" data-filter="*">All</a></li>
            <?php
               $portfolio_args_custom = array(
                'post_type' => 'portfolio',
              );
              $portfolio_posts_custom = new WP_Query($portfolio_args_custom);
              while($portfolio_posts_custom->have_posts()) {
                $portfolio_posts_custom->the_post();
                $categories = get_the_category();

                foreach ( $categories as $key=>$category ) {
          
        
               
           ?>
             <li><a href="#" data-filter=".<?php echo get_post_meta( get_the_ID(), 'Category', true);?>"><?php echo get_post_meta( get_the_ID(), 'Category', true);?></a></li>
              <?php   wp_reset_postdata();
              wp_reset_query();
            }
             } 
             ?>
          </ul>

I am trying to loop through the custom WordPress posts and have an issue like I add the custom field and want to display it in <li> using loop. I did it successfully but the data/link/category is repeating and I want it to show only once if the category is the same as:

If I have 2 posts with the data1 category then the link will only show data1 once but I have 2 posts with different category then it will show each separately.

Sample Code:

<ul class="filter filter-top">
        <li class="active"><a href="#" data-filter="*">All</a></li>
        <?php
          $portfolio_args_custom = array(
            'post_type' => 'portfolio',
          );
          $portfolio_posts_custom = new WP_Query($portfolio_args_custom);
          while($portfolio_posts_custom->have_posts()) {
            $portfolio_posts_custom->the_post();

       ?>
         <li><a href="#" data-filter=".<?php echo get_post_meta( get_the_ID(), 'Category', true);?>"><?php echo get_post_meta( get_the_ID(), 'Category', true);?></a></li>
          <?php   wp_reset_postdata();
          wp_reset_query();
        }?>
      </ul>
     

The code is working fine but it is repeating the <li> item with the same category. Please provide any documentation/helping link/ suggestion.

Edit:

This code works as I want but It's not clear for me, please explain if possible.

 <ul class="filter filter-top">
            <li class="active"><a href="#" data-filter="*">All</a></li>
            <?php
               $portfolio_args_custom = array(
                'post_type' => 'portfolio',
              );
              $portfolio_posts_custom = new WP_Query($portfolio_args_custom);
              while($portfolio_posts_custom->have_posts()) {
                $portfolio_posts_custom->the_post();
                $categories = get_the_category();

                foreach ( $categories as $key=>$category ) {
          
        
               
           ?>
             <li><a href="#" data-filter=".<?php echo get_post_meta( get_the_ID(), 'Category', true);?>"><?php echo get_post_meta( get_the_ID(), 'Category', true);?></a></li>
              <?php   wp_reset_postdata();
              wp_reset_query();
            }
             } 
             ?>
          </ul>
Share Improve this question edited Oct 12, 2020 at 6:31 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Oct 10, 2020 at 10:58 Nayla AkratiNayla Akrati 155 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

First of all why do you call wp_reset_query() on each while iteration? Then we simply will create new $cats array and check if new "category" is not already there = then show and add it to the array.

  <ul class="filter filter-top">
    <li class="active"><a href="#" data-filter="*">All</a></li>
    <?php
      $portfolio_args_custom = array(
        'post_type' => 'portfolio',
      );
      $cats = [];
      $portfolio_posts_custom = new WP_Query($portfolio_args_custom);
      while($portfolio_posts_custom->have_posts()) {
        $portfolio_posts_custom->the_post();
        $cat = get_post_meta( get_the_ID(), 'Category', true);
        if (!in_array($cat, $cats)) {
           $cats[] = $cat;
   ?>
     <li><a href="#" data-filter=".<?php echo $cat;?>"><?php echo $cat;?></a></li>
      <?php } } wp_reset_query(); ?>
  </ul>
发布评论

评论列表(0)

  1. 暂无评论