WordPress的显示帖子表格类别菜单处于活动状态

时间:2018-11-13 11:22:44

标签: wordpress categories

在WordPress中,我有一个带有以下代码的右键菜单:

 <?php wp_list_categories( array(
    'orderby'    => 'name',
    'show_count' => false,
    'exclude'    => array( 1,2,3,4 )
) ); ?> 

和显示帖子的查询:

    <?php
       // the query
      $wpb_all_query = new WP_Query(array('post_type'=>'documentos', 'order' => 'ASC', 'post_status'=>'publish'));
    ?>

  <?php if ( $wpb_all_query->have_posts() ) : ?>    
    <h4 class="titDocCat" ><?php single_cat_title(); ?></h4>
                  <ul class="doc"> 

                    <!-- the loop -->
                    <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>


                    <li class="gobierno">
                        <div class="kc_col-sm-9 kc_column kc_col-sm-9 noPad">
                     <h3 class="titleDoc"><?php the_field('titular_del_documento'); ?></h3>
                        <p class="smallDoc"><?php the_field('descripcion_del_documento'); ?></p>
                        </div>

                        <div class="kc_col-sm-3 kc_column kc_col-sm-3 noPad">
                        <a class="btnDownload" href="<?php the_field('archivo'); ?>" download><?php _e( 'Descarga PDF', 'modrox' ); ?></a>
                        </div>

                      </li>
                      <div class="sepGob"></div>
                    <?php endwhile; ?>
                    <!-- end of the loop -->
</ul>

                    <?php wp_reset_postdata(); ?>

                    <?php else : ?>
                    <p><?php _e( 'Sorry, no posts matched your criteria.', 'modrox' ); ?></p>
                    <?php endif; ?>

但是,当我单击菜单中的类别时,始终显示自定义帖子类型的所有帖子。如何只显示从菜单中选择的类别

2 个答案:

答案 0 :(得分:0)

您需要将查询修改为:

    $pargs = array(
        'post_per_page' => '-1',
        'post_type' => 'documentos',
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms' => $term_slugs
            ),
         'post_status'=>'publish'
        );

$wpb_all_query = new WP_Query($pargs);

注意: $term_slugs是类别条形名称。

这是工作代码:

<?php
// find last word from URL ( this will basically give you "rings" )
$cat = basename( parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) );

// the query
$args = array(
    'post_type'      => 'documentos',
    'posts_per_page' => -1,
    'orderby'        => 'DATE',
    'order'          => 'ASC',
    'tax_query'      => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => esc_attr( $cat ),
        ),
    ),
);

// query results
$the_query = new WP_Query( $args );

// display results
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<h2>' . get_the_title() . '</h2>';
    }
    // reset post
    wp_reset_postdata();
}
?>

Reference link

答案 1 :(得分:0)

<?php $wpb_all_query = new WP_Query(array('post_type' => 'documentos', 'order' => 'ASC', 'post_status' => 'publish', 'cat_name' => 'category_slug')); ?>

您应该在查询中添加“ cat_name”参数,并且必须向其中传递类别标签。