【问题标题】:Wordpress while loop to only get certain term from a custom taxonomyWordpress while 循环仅从自定义分类中获取特定术语
【发布时间】:2018-04-26 15:16:04
【问题描述】:

我正在尝试创建一个自定义页面模板,该模板可以在单个分类术语下提取帖子列表。

我有一个自定义帖子类型(下载) 和自定义分类(download_type)

我希望以下代码仅从 download_type 分类中提取术语“模型”内的帖子。

<?php $args = array (
'post_type' => 'download'
);
$the_query = new WP_Query($args);
?>

<?php $i = 1; ?>
<?php  if ($the_query->have_posts() ): while ($the_query->have_posts() && $i < 5) :$the_query->the_post(); ?>

<a href="<?php the_permalink(); ?>">
<div class="fullwidth_download" style="background-image: url('<?php the_post_thumbnail_url(); ?>')">
<div class="item_meta">
<h2><?php the_title(); ?></h2>
<h3 class="item_price">£<?php the_field('item_price'); ?></h3>
<?php
// vars 
$file_type = get_field('file_type');
// check
if( $file_type ): ?>
<ul class="file_types">
<?php foreach( $file_type as $file_type ): ?>
<li><?php echo $file_type; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>

【问题讨论】:

    标签: php wordpress taxonomy


    【解决方案1】:

    您可以为此使用tax_query 参数:

    <?php
    $args = array(
        'post_type' => 'download',
        'tax_query' => array(
            array(
                'taxonomy' => 'download_type',
                'field'    => 'slug',
                'terms'    => 'mockup',
            ),
        ),
    );
    $the_query = new WP_Query( $args );
    
    // Rest of the code here...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 2012-04-01
      • 1970-01-01
      • 2016-08-01
      • 2016-10-01
      • 1970-01-01
      相关资源
      最近更新 更多