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