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

时间:2015-09-06 16:52:43

标签: php wordpress

我有一个自定义分类法说“项目类型”,它是为自定义帖子“项目”注册的,根据该条目,我有术语“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>

有人可以帮助我解决我的错误吗?

1 个答案:

答案 0 :(得分:2)

您应该使用the_permalink()对象来获取数据,而不是使用the_title()the_excerpt()$post$post->ID。与have_posts()。只有在您使用基于post_class()的{​​{3}}时,才应调用您已使用的功能。你不是,所以用以下代码替换它们:

相关问题