簡體   English   中英

過濾自定義帖子類型時獲取分類描述

[英]Get taxonomy description when filtering custom post type

我正在使用自定義帖子類型,並且正在使用類別進行過濾。 每個類別都有一個描述,我想在選擇類別時顯示該類別的描述,如下所示

我找到了一種從這里顯示分類描述的方法

Html 使用:

<div class="col-lg-9">
                <div class="lista-portofoliu">
                    <div class="row">
                        
                        <?php while ($pquery->have_posts()) : $pquery->the_post();
                            $term_obj_list = get_the_terms(get_the_id(), 'portfolio_category');
                            $term_classes_a = array();
                            $img_data = array();
                            $img_string = ""; ?>
                            <?php foreach ($term_obj_list as $term) {

                                $term_classes_a[] = $term->slug;
                                $meniu_triggers[] = $term->slug;
                                $meniu_labels[] = $term->name;

                                $img_key = 'imagine_' . $term->slug;
                                $img_src = wp_get_attachment_image_src(get_field($img_key, get_the_id()), 'thumbnail');

                                if ($img_src) {
                                    $img_data[] = 'data-' . $img_key . '="' . $img_src[0] . '"';
                                } else {
                                    $img_data[] = 'data-' . $img_key . '="' . wp_get_attachment_image_src(get_field($img_key, get_the_id()), 'full') . '"';
                                }                                    
                                foreach ($img_data as $imagine) :
                                    $img_string .= $imagine;

                                endforeach;;
                            } ?>

                            <?php $term_classes = implode(" ", $term_classes_a);  ?>

                            <div class="col-lg-4 p-1 m-0 item toate <?php echo $term_classes; ?>">
                                <div class=" item-content" <?php echo $img_string; ?> data-imagine_toate="<?php echo get_the_post_thumbnail_url(get_the_id(), 'thumbnail', true); ?>" style="background-image:url('<?php echo get_the_post_thumbnail_url(get_the_id(), 'thumbnail', true); ?>')">
                                    <a href="<?php the_permalink(); ?>" class=" item-overlay">
                                        <span class="item-title">
                                            <?php the_title(); ?>
                                            <br>
                                            <br>
                                        </span>
                                    </a>
                                </div>
                            </div>
                        <?php endwhile;
                        ?>
                        
                        <?php
                        wp_reset_postdata(); ?>
                        
                    </div>
                    <button type="button" id="more_posts" class="btn btn-dark loadMore center-block btn-pachete">Mai mult</button>
                </div>
            </div>

謝謝!

如果您想按投資組合類別對所有內容進行排序,那么您應該首先遍歷投資portfolio_category類別分類中的每個術語。

對於每個術語,創建一個新查詢,您可以在其中使用tax_query僅獲取與我們正在循環的當前術語相關的帖子。

結果將是每個循環都包含類別數據和與該類別相關的帖子。

您可以通過回顯$term->description值來 output 類別描述。

<?php
$terms = get_terms('portfolio_category');
if (!is_wp_error($terms)) :
  foreach ($terms as $term) : ?>

    <div class="row">

    <?php
    $args = [
      'post_type' => 'YOUR_CUSTOM_POST_TYPE',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'tax_query' => [
        [
          'taxonomy' => 'portfolio_category',
          'field' => 'term_id',
          'terms' => $term->term_id
        ]
      ]
    ];

    $query = new WP_Query($args);

    if ($query->have_posts()) : ?>
      
      <div class="col-lg-4">
        <?= $term->name; ?>
        <?= $term->description; ?>
      </div>

      <?php
      while ($query->have_posts()) :
        $query->the_post(); ?>

        <div class="col-lg-4">
          <?php the_title(); ?>
        </div>

      <?php
      endwhile
      wp_reset_postdata();
    endif; ?>

    </div>

  <?php
  endforeach;
endif;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM