简体   繁体   English

使用自定义分类法中的条款获取帖子来显示内容

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

I am having a custom taxonomy say "project-type" which is registered for the custom post "projects" and under that I have terms "catOne" and "catTwo". 我有一个自定义分类法,例如“ project-type”,已为自定义帖子“ projects”注册,在此之下我有“ catOne”和“ catTwo”这两个术语。 Now I want to display all the custom posts that are linked to catOne using the term_id of "catOne", which in my case is 9. 现在,我想显示所有使用“ catOne”的term_id链接到catOne的自定义帖子,在我的情况下为9。

So I am successfully able to loop through all the posts but it is displaying only the ID but not all contents. 因此,我能够成功遍历所有帖子,但只显示ID,而不显示所有内容。

My approach: 我的方法:

$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();

Output I am getting 我得到的输出

<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>

Can someone please help me with where I am going wrong? 有人可以帮我解决我的问题吗?

Instead of using post_class() , the_permalink() , the_title() , and the_excerpt() , you should use the $post object to get the data, just like you did with $post->ID . 与其使用post_class()the_permalink()the_title()the_excerpt()the_excerpt()使用$post->ID ,应使用$post对象获取数据。 The functions you've used should be called only if you're using a loop based on have_posts() . 仅在使用基于have_posts()循环时,才应调用已使用的函数。 You aren't, so replace them with: 您不是,因此将它们替换为:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM