簡體   English   中英

使用自定義分類法中的條款獲取帖子來顯示內容

[英]display content using get posts from terms under a custom taxonomy

我有一個自定義分類法,例如“ project-type”,已為自定義帖子“ projects”注冊,在此之下我有“ catOne”和“ catTwo”這兩個術語。 現在,我想顯示所有使用“ catOne”的term_id鏈接到catOne的自定義帖子,在我的情況下為9。

因此,我能夠成功遍歷所有帖子,但只顯示ID,而不顯示所有內容。

我的方法:

$cat_id = 9;

$args = array(
    'post_type' => 'projects',
    'tax_query' => array(
        array(
            'taxonomy' => 'project-type',
            'field'    => 'term_id',
            'terms'    => array( $cat_id )
        ),
    ),
);

    $posts = get_posts( $args );

    foreach ( $posts as $post ) {
    setup_postdata( $post ); ?>

    <div id="post-<?php echo $post->ID; ?> <?php post_class(); ?>">
        <h1 class="posttitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

        <div id="post-content">
        <?php the_excerpt(); ?>
        </div>

   </div>

   <?php } wp_reset_postdata();

我得到的輸出

<div id="post-137 class=" ""="">
        <h1 class="posttitle"><a href=""></a></h1>
        <div id="post-content">
                </div>
   </div>
    <div id="post-135 class=" ""="">
        <h1 class="posttitle"><a href=""></a></h1>
        <div id="post-content">
                </div>
   </div>

有人可以幫我解決我的問題嗎?

與其使用post_class()the_permalink()the_title()the_excerpt()the_excerpt()使用$post->ID ,應使用$post對象獲取數據。 僅在使用基於have_posts()循環時,才應調用已使用的函數。 您不是,因此將它們替換為:

暫無
暫無

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

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