按类别自定义字段显示的Wordpress query_posts


Wordpress query_posts displayed by category custom field

拥有基于wordpress的站点,其中:

  • MainCategory

    • 子类别(custom-field value = custom1)
    • 子类别2(custom-field value = custom2)
    • 子类别3(custom-field value = custom3)
  • MainCategory2

    • 子类别(custom-field value = custom1)
    • 子类别2(custom-field value = custom2)
    • 子类别3(custom-field value = custom3)
  • MainCategory3

    • 子类别(custom-field value = custom1)
    • 子类别2(custom-field value = custom2)
    • 子类别3(custom-field value = custom3)

如您所见,主类别中的所有子类别都是相同的。具有相同的名称(不是slug),并且存在具有相同字段值的自定义字段。

我需要显示的职位是在MainCategory2 subcategory has custom field with value custom2。这可能吗?

注:

我不确定这是否是最好的解决方案,但它有望解决您的问题。

  1. 循环MainCategory2的所有帖子(假设该类别的ID为2)
  2. 检查custom-field的内容是否等于custom2
  3. 定义应该循环的内容(在本例中是blogtitle和link)

代码应该是这样的:

<?php query_posts( 'showposts=20&cat=2&order=ASC' ); ?>
   <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <?php if( get_field('custom-field') == 'custom2' ): ?>
         <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
      <?php else : ?>
      <?php endif; ?>
<?php endwhile; endif; ?>