簡體   English   中英

顯示來自某些ID或類別的分類類別中的所有帖子

[英]Show all post in taxonomy category from certain id or slug

我有一個稱為“電子”的自定義帖子類型,其分類類別為“計算機,電話,筆記本電腦等”

在taxonomy.php模板中,我用此代碼成功獲取了分類名稱,子彈和id。

<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
echo $term->name; // will show the name
echo $term->slug; // will show the slug
echo $term->term_id; // will show the id
?>

因此,如果我在計算機分類頁面中,我得到的是'Computers computers 11'

與任一pf每次生成的值,我怎么能根據其中的“分類頁”即時生成職位。 在計算機分類頁面中標記有“計算機”的帖子,在電話分類頁面中標記有電話的帖子..等等。

像這樣的東西,但用於分類法...

<?php
$catquery = new WP_Query( 'cat=3' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>

您需要使用tax_query來獲取具有特定分類術語的帖子,方法如下:

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'electronics',
            'field' => 'slug',
            'terms' => $term->slug
        )
    )
);
$catquery = get_posts( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM