0

非常简单的设置,我使用 1 个模板进行多种分类。所以例子:

我有基本类别+ customtypetaxonomy。他们都有条款。我在同一个模板 - category.php 中显示这些术语的帖子。哪个效果很好,我想要它。

但是,我想根据它从中获取帖子的分类法对此模板进行一些小的更改。但我无法弄清楚我需要使用什么功能来获得它。

例如:single_cat_title() 给我这个词的标题(类别)

但我需要的是蛞蝓或身份证。有没有办法做到这一点?像 single_cat_id 或 single_cat_slug 之类的东西。我试过 $cat 但它没有给我任何输出。

有任何想法吗?

4

1 回答 1

0

使用 WP_Query。像这样:

    $ThisQuery = new WP_Query( array( 'category_name'  => 'MyCategory',
                                      'meta_key'       => 'MyCustomFieldValue',
                                      'posts_per_page' => 10 ) ); // Number of posts

    if ( $ThisQuery->have_posts() ) : while ( $ThisQuery->have_posts() ) : $ThisQuery->the_post();
...

    endwhile;
    endif;
    wp_reset_postdata();

修改数组以包含标识您要获取的帖子的元素

另一个例子:

<?php
$Args = array( //Category
  'cat'           => 5,  // Category id.
  'category_name' => 'MyCategory',  // Category Name.
  //Taxonomy
  'tax_query'     => array( 'relation' => 'AND', // Could be OR
                            array( 'taxonomy' => 'MyTaxonomy1',
                                   'field'    => 'MySlug1',
                                   'terms'    => array( 'MyTerm11',
                                                        'MyTerm12' ), ),
                            array( 'taxonomy' => 'MyTaxonomy2',
                                   'field'    => 'MySlug2',
                                   'terms'    => array( 'MyTerm21',
                                                        'MyTerm22',
                                                        'MyTerm23' ), ) ), );
$ThisQuery = new WP_Query( $Args );
if ( $ThisQuery->have_posts() ) : while ( $ThisQuery->have_posts() ) : $ThisQuery->the_post();
  // Do Stuff
endwhile;
endif;
?>
于 2012-11-14T17:55:29.787 回答