我的分类术语页面显示了所有帖子

时间:2018-04-07 作者:user141312

我有两个主要的分类法:1。Libros2。阿桑托斯

和多个分类术语:1。Al Bujari2。粘菌3。La Pacience4。La Fe5。LaCaridad和许多其他使用自定义post类型的分类术语:hadices。

情况是,当我单击与上述任何分类术语相关的slug时,它会显示所有帖子,而不仅仅是与我单击的分类术语相关的帖子。我正在研究分类学。php文件。这是我关于分类法的代码。php:

<?php   $taxes = array( \'libro_de_hadiz\' , \'asunto\');
    $terms = get_terms( $taxes );
    echo $terms;
    $paged = ( get_query_var( \'paged\' ) ) ? absint ( get_query_var( \'paged\' ) ) : 1;
        $args = array( \'post_type\' => \'hadices\', 
                 \'posts_per_page\' => 6, 
                 \'paged\' => $paged, 
                  ));
      $loop =  new WP_Query( $args );

    if ( $loop->have_posts() ) : ?>
    <header class="page-header">
        <?php
            the_archive_title( \'<h1 class="page-title">\', \'</h1>\' );
            the_archive_description( \'<div class="taxonomy-description">\', \'</div>\' );
        ?>
    </header><!-- .page-header -->
<?php endif; ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php
    if ( $loop->have_posts() ) : ?>
        <?php
        /* Start the Loop */
        while ( $loop->have_posts() ) : $loop->the_post();

            /*
             * 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.
             */
            get_template_part( \'template-parts/post/content\', get_post_format() );

            $loop->the_posts_pagination( array(
            \'prev_text\' => twentyseventeen_get_svg( array( \'icon\' => \'arrow-left\' ) ) . \'<span class="screen-reader-text">\' .$loop-> __( \'Previous page\', \'twentyseventeen\' ) . \'</span>\',
            \'next_text\' => \'<span class="screen-reader-text">\' . $loop->__( \'Next page\', \'twentyseventeen\' ) . \'</span>\' . twentyseventeen_get_svg( array( \'icon\' => \'arrow-right\' ) ),
            \'before_page_number\' => \'<span class="meta-nav screen-reader-text">\' . $loop->__( \'Page\', \'twentyseventeen\' ) . \' </span>\',
        ) );

        endwhile;
        $big = 999999999; // need an unlikely integer
        echo paginate_links( array(
                    \'base\' => str_replace( $big, \'%#%\', get_pagenum_link( $big ) ),
                    \'format\' => \'/paged/%#%\',
                    \'current\' => max( 1, get_query_var(\'paged\') ),
                    \'total\' => $loop->max_num_pages
                ) );
else :

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

endif; wp_reset_postdata(); ?>




    </main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>

这是我的自定义帖子和分类的代码:

function cptui_register_my_cpts() {

/**
 * Post Type: Hadices.
 */

$labels = array(
    "name" => __( "Hadices", "twentyseventeen" ),
    "singular_name" => __( "Hadiz", "twentyseventeen" ),
);

$args = array(
    "label" => __( "Hadices", "twentyseventeen" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => false,
    "show_in_menu" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "hadiz", "with_front" => true ),
    "query_var" => true,
    "supports" => array( "title", "editor" ),
    "taxonomies" => array( "libro_de_hadiz", "asunto" ),
);

register_post_type( "hadices", $args );
}

add\\u action(\'init\',\'cptui\\u register\\u my\\u cpts\');

function cptui_register_my_taxes() {

/**
 * Taxonomy: Libros de Hadiz.
 */

$labels = array(
    "name" => __( "Libros de Hadiz", "twentyseventeen" ),
    "singular_name" => __( "Libro", "twentyseventeen" ),
);

$args = array(
    "label" => __( "Libros de Hadiz", "twentyseventeen" ),
    "labels" => $labels,
    "public" => true,
    "hierarchical" => false,
    "label" => "Libros de Hadiz",
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => array( \'slug\' => \'libro_de_hadiz\', \'with_front\' => true, ),
    "show_admin_column" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "show_in_quick_edit" => false,
);
register_taxonomy( "libro_de_hadiz", array( "hadices" ), $args );
register_taxonomy( "asunto", array( "hadices" ), array(
    "name" => __( "Asuntos", "twentyseventeen" ),
    "singular_name" => __( "Asunto", "twentyseventeen" ),
    "label" => __( "Asuntos", "twentyseventeen" ),
    "public" => true,
    "hierarchical" => false,
    "label" => "Asuntos",
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => array( \'slug\' => \'asunto\', \'with_front\' => true, ),
    "show_admin_column" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "show_in_quick_edit" => false,
) );
}

add_action( \'init\', \'cptui_register_my_taxes\' );
我在这个链接中看到了该帖子的答案:Archive template for taxonomy terms

然而,答案只是说要添加一个分类学术语。每个术语的php,在我的情况下,这将是非常乏味的,因为我需要几十个具有类似代码的文件,只需更改tax\\u查询中的术语slug。是否有方法为tax\\u查询中的术语添加动态变量,以仅显示与术语或任何其他解决方案(如果有)相关的帖子?

提前感谢

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

正如安迪所说,WordPress将为您加载正确的帖子。您不需要自定义查询。查看自定义帖子类型或分类法存档时main loop 将已经有基于URL的正确帖子。

因此,您不需要这方面的任何内容:

$taxes = array( \'libro_de_hadiz\' , \'asunto\');
    $terms = get_terms( $taxes );
    echo $terms;
    $paged = ( get_query_var( \'paged\' ) ) ? absint ( get_query_var( \'paged\' ) ) : 1;
        $args = array( \'post_type\' => \'hadices\', 
                 \'posts_per_page\' => 6, 
                 \'paged\' => $paged, 
                  ));
      $loop =  new WP_Query( $args );
那么$loop->have_posts()$loop->the_post() 应该是have_posts()the_post().

还有,你有点忘乎所以,补充道$loop-> 在没有意义的地方。你也应该把它从其他地方移走。

结束

相关推荐

Order posts by taxonomy count

我创建了新的帖子类型(酒店)和分类法(本地化)。我想通过分类法的计数描述循环我的酒店订单。我想按本地化数量对我的酒店进行排序(我指的是从本地化中的更多酒店到更少酒店的订单)。我尝试以下方法: $args = array( \'post_type\' => \'hotels\', \'posts_per_page\' => -1, \'tax_query\' => array( a