【问题标题】:Loop through child categories and output custom field on archive.php遍历子类别并在archive.php上输出自定义字段
【发布时间】:2016-12-27 04:21:27
【问题描述】:

我正在使用 Wordpress,并且有具有父/子关系的类别。 我已使用高级自定义字段将自定义字段添加到类别中。

在archive.php 页面上,我想显示每个子类别的标题和子类别的自定义字段。我目前正在输出子类别标题如下 -

<?php $this_cat = get_query_var('cat');
wp_list_categories('child_of=' . $this_cat . '&title_li=&show_option_none=&depth=1&hide_title_if_empty=true');?>

有没有办法既包含输出自定义字段的功能,又包含实际循环遍历子类别并输出字段的方法?

【问题讨论】:

    标签: wordpress foreach archive advanced-custom-fields taxonomy


    【解决方案1】:

    您应该使用税务查询来获取子类别,然后使用 wp 查询遍历它们,如下所示:

    <?php
    
      $this_cat = get_query_var('cat');
    
    $args = array(
        'tax_query' => [
             [
                 'taxonomy' => 'category',
                 'terms'    =>  $this_cat,
             ],
         ],
    );
    
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) : ?>
        <?php while ( $query->have_posts() ) : $query->the_post();
    
        //get your custom field
        $yourField = get_field( 'field_name' ); ?>
    
        <div>
            <?php echo $yourField ?>
        </div>
    
        <?php endwhile; ?>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>
    

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 2017-02-01
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      相关资源
      最近更新 更多