列出子类别中条目的标题和自定义字段

时间:2017-12-11 作者:user2265915

我有一个自定义分类设置-下载。在下载中,我有几个子类别。我想列出这些条目的标题,条目标题是指向高级自定义字段(文本类型)的链接。我有一些可以循环并打印子类别名称的东西,但我一直在研究如何在每个子类别中显示条目信息。以下是我目前掌握的情况:

<?php
    if ( have_posts() ) : ?>

            <header class="page-header">
                <h1>Subcategories</h1>
            </header><!-- .page-header -->
            <div class="row">
                <div class="col-sm-12">

        <?php       

            /*
             * Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */

                $term = get_queried_object();
                $term_id = $term->term_id;
                $taxonomy_name = $term->taxonomy;

                $termchildren = get_term_children( $term_id, $taxonomy_name );

                echo \'<ul>\';
                foreach ( $termchildren as $child ) {
                    $term = get_term_by( \'id\', $child, $taxonomy_name );
                    echo \'<li><a href="\' . get_term_link( $term, $taxonomy_name ) . \'">\' . $term->name . \'</a></li>\';
                }
                echo \'</ul>\';   

                wp_link_pages( array(
                    \'before\' => \'<div class="page-links">\' . esc_html__( \'Pages:\', \'fernox\' ),
                    \'after\'  => \'</div>\',
                ) );

        the_posts_navigation();

    else :

        get_template_part( \'template-parts/content\', \'none\' );

    endif; ?>

        </div>
    </div>
</div>
我想我只需要在当前的“foreach”中设置一个循环,但我不知道该怎么做。

1 个回复
最合适的回答,由SO网友:David Sword 整理而成

如果我在$termchildren 循环,您需要添加get_posts() tax_query 这将找到所有具有当前循环项的帖子。我认为以下类似的方法也会奏效:

if ( have_posts() ) :

    echo "<h2>Subcategories</h2>";

    // if you\'re on a single post, it will return the post object
    // if you\'re on a page, it will return the page object
    // if you\'re on an archive page, it will return the post type object
    // if you\'re on a category archive, it will return the category object
    // if you\'re on an author archive, it will return the author object
    $term = get_queried_object();

    echo "Current \\$term is: <pre>";
    print_r($term);
    echo "</pre>";


    $termchildren = get_term_children( $term->term_id, $term->taxonomy );

    echo \'<ul>\';
    foreach ( $termchildren as $child ) {
        $childTerm = get_term_by(\'id\', $child, $term->taxonomy );
        echo \'<li><a href="\' . get_term_link( $childTerm, $term->taxonomy ) . \'">\' . $childTerm->name . \'</a>\';

        // posts with this term         
        $childTermPosts = get_posts(array(
          \'post_type\' => \'post\', // WTV your post_type is for Downloads tax
          \'numberposts\' => -1,
          \'tax_query\' => array(
            array(
              \'taxonomy\' => $term->name,
              \'field\' => \'id\',
              \'terms\' => $child,
              \'include_children\' => true
            )
          )
        ));

        // then loops the posts
        if (count($childTermPosts) > 0) {
            echo "<ul>"

            foreach ($childTermPosts as $childTermPost)
                echo "<li><a href=\'".get_permalink( $childTermPost->ID )."\'>{$childTermPost->post_title}</a></li>";

            echo "</ul>"
        }

        echo "</li>";

    } //end foreach

    echo \'</ul>\';   

    //wp_link_pages(...);
    //the_posts_navigation(..);
else :

    echo "get_template_part(template-parts/content) here";
    //get_template_part( \'template-parts/content\', \'none\' );

endif;
我在评论中指出get_queried_object() 根据当前模板文件返回大量内容。

上面的内容没有经过测试,但希望它能给出一个可以做什么的想法。我评论并删除了一些东西,因为有时要缩小问题范围,甚至是缩小你的目标范围,就是最小化代码,让代码吐出当前变量——最后添加所有html和分页等。

结束