将自定义帖子类型与帖子或用户相关联

时间:2014-01-09 18:18:48

标签: wordpress post custom-post-type

我有不同用户的帖子,我创建了一个名为“offers”的custom_post_type。当用户登录wordpress时,只能看到自己的帖子和优惠。

是否可以将优惠与用户帖子相关联(用户只能创建一个帖子?

问题是:当帖子的用户有“优惠”时,我可以在index.php和archive.php的帖子中显示图标吗?

1 个答案:

答案 0 :(得分:0)

你似乎想在循环中做一个循环?我不知道这是否是最有效的方法,但我想它会起作用:

<?php 
// First loop
while ( have_posts() ): the_post(); 
$author_id = get_query_var('author');
    $offers = array();

    // Second Loop
    $i = 0;
    $args = array(
        'author' => $author_id,
        'post_type' => 'offers'
    );
    $the_query = new WP_Query( $args );
    if ($the_query->have_posts()) : 
        while ($the_query->have_posts()) : $the_query->the_post(); // check if it has offers, if it has, loop over the offers
            $offers[$i]['title'] = get_the_title(); // or anything you need
            $i++;
        endwhile; // close the loop
    else: // if it has no post
        continue; // we don't display the post
    endif;

    wp_reset_postdata(); 
    ?>
<div class="post">
<?php
the_title(); // or anything you need

foreach ($offers as $offer):
    echo $offer['title']; // we display the title of the offer
endforeach;
?>
</div>
<?php  endwhile; ?>