我如何仅显示直接分类儿童?

发布于 2025-02-03 12:46:19 字数 2139 浏览 4 评论 0原文

我正在为分类法添加一些自定义代码。phpwordpress页面。我不知道如何写PHP。这已经花了3天了,我正在尝试自定义。

这正是我现在想要的,除了我只想展示当前分类法的直接孩子。

有人可以帮助我更改此操作以显示直接的孩子,而不是所有孩子?

<?php
$term_id = get_queried_object()->term_id;
$taxonomy_name = get_query_var( 'taxonomy' ); 
$termchildren = get_term_children( $term_id, $taxonomy_name );
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$url = get_term_link( $child, $taxonomy_name );
$name = $term->name;
$image = get_field('taxonomy_image', $term );
$description = get_term_field( 'description', $term );
echo '<div class="sub-category">';
echo '<a href="' . $url . '">' . $name . '<div class="sub-category-image"><img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '" /></div><div class="sub-category-description">' . $description . '</div></a>';
echo '</div>';
}
?>

背景信息(不知道这是否相关): 这是用插件“ CPT UI”制成的自定义分类“区域”。自定义分类法还包括来自“高级自定义字段”的自定义字段。我想显示分类法,而不是与儿童所有分类法有关的职位。

编辑 我...根据评论中为我提供的链接定制“定制”代码,但是正如我所说,PHP不是我的语言。

我的代码现在看起来像这样:

    <?php
    $term_children = get_terms( 'produktkategori', array( 'parent' => get_queried_object_id(), ) );
        if ( ! is_wp_error( $terms ) ) {
            foreach ( $term_children as $child ) {
            $image = get_field( 'taxonomy_image', $term );
            $description = get_term_field( 'description', $term );
            echo '
            <div class="sub-category">
            <a href="' . get_term_link( $child ) . '">
            <div class="sub-category-image">
            <img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '" /></div>
            <div class="sub-category-description">
            <h3>' . $child->name . '</h3>
            ' . $description . '
            </div>
            </a>
            </div>
            ';
            }
        }
    ?>

实际上,这实际上只给了我直接的分类儿童(我想要的),但是用儿童分类法显示的图像和描述是他们孩子的分类学图像和描述。太...

I am adding some custom code for a taxonomy.php WordPress-page. I do NOT know how to write php. This has taken me 3 days already, and i'm trying to customize it.

It is exactly how I want it now, except I only want to display direct children of the current taxonomy.

Can someone help me change this to show only direct children, not all children?

<?php
$term_id = get_queried_object()->term_id;
$taxonomy_name = get_query_var( 'taxonomy' ); 
$termchildren = get_term_children( $term_id, $taxonomy_name );
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$url = get_term_link( $child, $taxonomy_name );
$name = $term->name;
$image = get_field('taxonomy_image', $term );
$description = get_term_field( 'description', $term );
echo '<div class="sub-category">';
echo '<a href="' . $url . '">' . $name . '<div class="sub-category-image"><img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '" /></div><div class="sub-category-description">' . $description . '</div></a>';
echo '</div>';
}
?>

Background info (don't know if this is relevant):
It is a custom taxonomy "area" made with the plugin "CPT UI". The custom taxonomies also include custom fields from "Advanced Custom Fields". I want to show the taxonomies and not the posts on all taxonomies with children.

EDIT
I... "customized" the code according to the link provided for me in the comments, but as I said, PHP is not my language.

My code now looks like this:

    <?php
    $term_children = get_terms( 'produktkategori', array( 'parent' => get_queried_object_id(), ) );
        if ( ! is_wp_error( $terms ) ) {
            foreach ( $term_children as $child ) {
            $image = get_field( 'taxonomy_image', $term );
            $description = get_term_field( 'description', $term );
            echo '
            <div class="sub-category">
            <a href="' . get_term_link( $child ) . '">
            <div class="sub-category-image">
            <img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '" /></div>
            <div class="sub-category-description">
            <h3>' . $child->name . '</h3>
            ' . $description . '
            </div>
            </a>
            </div>
            ';
            }
        }
    ?>

Which in turn actually gave me only direct taxonomy children (which I wanted) but the image and description showing with the child taxonomies are THEIR child's taxonomy image and description. Sooo...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文