我能拿到分类的缩略图吗?

时间:2019-11-11 作者:Tyler Soffiantino
<?php
            $args = array(
                \'type\'                     => \'itineraries\',
                \'child_of\'                 => 0,
                \'parent\'                   => \'\',
                \'orderby\'                  => \'name\',
                \'order\'                    => \'ASC\',
                \'hide_empty\'               => 1,
                \'hierarchical\'             => 1,
                \'exclude\'                  => \'\',
                \'include\'                  => \'\',
                \'number\'                   => \'\',
                \'taxonomy\'                 => \'travel_locations\',
                \'pad_counts\'               => false );
            $categories = get_categories($args);
            $attachments = get_children( $args );   

                    foreach ($categories as $category):
// set up a new query for each category, pulling in related posts.
$services = new WP_Query(
    array(
        \'post_type\' => \'itineraries\',
        \'showposts\' => -1,
        \'tax_query\' => array(
            array(
                \'taxonomy\'  => \'travel_locations\',
                \'terms\'     => array( $category->slug ),
                \'field\'     => \'slug\'
            )
        )
    )
);
?> 
<h3><?php echo $category->name; ?></h3>
<ul>
<?php while ($services->have_posts()) : $services->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
// Reset things, for good measure
$services = null;
wp_reset_postdata();
// end the loop
endforeach;
?>
1 个回复
SO网友:HeroWeb512

为了更好地使用,您可以使用高级自定义字段插件

 // get the current taxonomy term
$term = get_queried_object();

// here is the taxonomy image
$image = get_field(\'image\', $term);

You can use free plugin in your template

 URL: https://wordpress.org/plugins/wp-custom-taxonomy-image/
 if (function_exists(\'get_wp_term_image\'))

{
$meta_image = get_wp_term_image($term_id); 
//It will give category/term image url 
}

echo $meta_image; // category/term image url

相关推荐

Display Custom Taxonomy names

我正在尝试显示自定义帖子类型的自定义分类法。因此,此分类法特定于此自定义帖子类型。不幸的是,我无法让它们显示出来。这是我在函数中的代码。php注册自定义分类法:add_action(\'init\', \'products_categories\', 0); function products_categories(){ $labels = array (\'name\' => _x(\'Product Categories\',\'taxonomy general nam