首页 > 解决方案 > 如何显示 Wordpress 类别页面的自定义分类法?

问题描述

我正在使用高级自定义字段将自定义分类添加到我的类别页面(重命名为作者),虽然我可以公开非分类自定义字段,但对于该类别可能以基本方式拥有的任何分类关系,我无法做到这一点。

我尝试使用指南在 ACF 网站上公开分类法,在 foreach 循环中循环出相关条目,但无济于事。

在帖子上,我使用了“the_terms”并通过绘制帖子 ID 提取了各种自定义分类法,但我无法调整它以在类别页面上使用。

这是我在帖子上使用的并且工作正常:

<?php the_terms( $post->ID, 'century', '', ' , ' ); ?>

因为类别不是帖子,所以“the_terms”在这种情况下不起作用。这是我在 Category 上用来以基本方式显示其自定义分类法的内容:

<?php 
$term = get_queried_object();

$locations = get_field('location', $term);
$century = get_field('century', $term);
$tradition = get_field('tradition', $term);
$characteristics = get_field('characteristics', $term);
$related = get_field('related', $term);

foreach($locations as $location){
echo $location;
} 
foreach($century as $centur){
echo $centur;
} 
foreach($tradition as $traditio){
echo $traditio;
} 
foreach($characteristics as $characteristic){
echo $characteristic;
}
foreach($related as $relate){
echo $relate;
}
?>  

返回的唯一结果是:$location 和 $ Century。Century 不是分类关系,并且 location 确实返回 ID - 我所有的分类关系都返回 ID,因为将其更改为显示 Term Object 会导致页面在使用上述代码时崩溃。这似乎是一个单独但可能相关的问题。

我想要做的是理想地以以下格式循环上述分类法:

<?php 

$category = get_queried_object();
$terms = get_field('location', $category);

if( $terms ): ?>

    <ul>

    <?php foreach( $terms as $term ): ?>

        <h2><a href="<?php echo get_term_link( $term ); ?>"><?php echo $term->name; ?></a></h2>

    <?php endforeach; ?>

    </ul>

<?php endif; ?>

问题是这段代码目前没有返回任何东西。

提前致谢!

编辑:

这是 var_dump($category) 的结果

object(WP_Term)#9457 (17) { ["term_id"]=> int(196) ["name"]=> string(7) "Jan Hus" ["slug"]=> string(7) "jan-hus" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(196) ["taxonomy"]=> string(8) "category" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(11) ["filter"]=> string(3) "raw" ["term_order"]=> string(1) "0" ["cat_ID"]=> int(196) ["category_count"]=> int(11) ["category_description"]=> string(0) "" ["cat_name"]=> string(7) "Jan Hus" ["category_nicename"]=> string(7) "jan-hus" ["category_parent"]=> int(0) }

标签: wordpresswordpress-themingadvanced-custom-fieldscustom-taxonomycustom-wordpress-pages

解决方案


尝试类似下面的方法 - 您必须在get_field- https://www.advancedcustomfields.com/resources/get_field/的第二个参数中使用组合

    $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;  

    $image = get_field('field_5d04699ba10da', $taxonomy . '_' . $term_id);

我使用字段键而不是名称来获取图像字段,但问题的重要部分是第二个参数get_field


推荐阅读