繁体   English   中英

使用 WP_Query 显示所有具有自定义分类的帖子

[英]Displaying all posts with custom taxonomy using WP_Query

在此处输入图像描述

我正在尝试显示具有自定义分类法的自定义帖子类型,但我没有任何运气。 什么都没有出现。 我感谢任何帮助。

岗位类型=大学

自定义分类 slug = country

我想使用 WP_Query 显示所有国家/地区列表,因为此分类法中有一些自定义字段。 此外,在点击任何国家时,它应该重定向到包含其详细信息的国家页面

下面是我的代码

<?php
      $args = array(
        'post_type' => 'university',
        'tax_query' => array(
             array(
                 'taxonomy' => 'country'
             )
         )
      );

     $query = new WP_Query($args);

     if ($query->have_posts()) {
        while ($query->have_posts()) {
           $query->the_post();
     ?>

              <a title="<?php the_title(); ?>"> 

                 <h3><?php the_title(); ?></h3>

              </a>
     <?php
        }
     }
     wp_reset_postdata();
    ?>

我已经修改了你的代码,请试试看。

<?php

      $custom_terms = get_terms('country');
      $args = array(
        'post_type' => 'university',
        'tax_query' => array(             
             array(
                'taxonomy' => 'country',
                'field' => 'slug',
                'terms' => $custom_terms[0]->slug, // or the category name e.g. Germany
            ),
         )
      );

     $query = new WP_Query($args);

     if ($query->have_posts()) {
        while ($query->have_posts()) {
           $query->the_post();
     ?>

              <a title="<?php the_title(); ?>"> 

                 <h3><?php the_title(); ?></h3>

              </a>
     <?php
        }
     }
     wp_reset_postdata();
    ?>

我们获取分类法的所有术语,遍历它们,并触发指向属于该术语的每个帖子的标题链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM