在我的循环中插入自定义帖子类型

时间:2011-07-08 作者:Kapitol

从我的角度来看,我有一个复杂的循环。代码如下。

<!-- *** Check Mass List (Custom Post Type) *** -->

<!-- *** End Mass List (Custom Post Type) *** -->     
<?php if (have_posts()) : ?>
<?php $new = \'first\'; ?>
<?php while (have_posts()) : the_post(); ?>
<?php if( $new == \'first\' && !is_paged() ) : $new = \'not-first\';  ?>
<p class="timedate"><?php the_date(\'F jS, Y\'); ?></p>
<article id="post-<?php the_ID(); ?>" class="latest">
<h1><?php the_category(\', \'); ?></h1>
<?php ($post == $posts[0] && !is_paged()) ? the_content(\'\', \'FALSE\') : the_excerpt() ; ?>
<p><?php the_tags(\'Tags: \', \', \', \'<br>\'); ?></p>
</article>
<?php else : ?>
<p class="timedate"><?php the_date(\'F jS, Y\'); ?></p>
<article id="post-<?php the_ID(); ?>">
<h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php the_category(\' \'); ?></a></h1>
<div class="shortstuff">
<?php $tags = get_the_tags();
$html = \'<div class="post_tags">\';
foreach ( $tags as $tag )
{
$tag_link = get_tag_link( $tag->term_id );

$html .= "<a href=\'{$tag_link}\' title=\'{$tag->name} Tag\' class=\'{$tag->slug}\'>     <h2>{$tag->name}</h2></a>";
$html .= "<p>$tag->description</p>"."";
}

$html .= \'</div>\';
echo $html;?>
<?php ($post == $posts[0] && !is_paged()) ? the_content(\'\', \'FALSE\') : the_excerpt() ; ?>
</div>
</article>
<?php endif; ?>    
<?php endwhile; ?>
<nav>
<p>
<?php posts_nav_link(\'&nbsp;&bull;&nbsp;\'); ?>
</p>
</nav>
<?php else : ?>
<article>
<h1>Not Found</h1>
<p>Sorry, but the requested resource was not found on this site.</p>
<?php get_search_form(); ?>
</article>
<?php endif; ?>
您可能已经阅读过这个循环,它执行以下操作:显示最新发布的帖子,然后继续显示较旧的帖子。最新的帖子以另一种样式显示,而不是旧帖子。

我想为自定义帖子类型添加第三个检查到该循环中。因此wordpress会检查所有3个参数,然后根据日期的顺序显示它们;发布时间。

输出逻辑的示例可以是。7月5日-最新,自定义帖子类型,其他帖子。7月6日-自定义帖子类型,最新帖子,然后是最旧帖子。

因此,一切都如我的代码在示例中所示。唯一的变化是循环检查,以查看当天是否存在自定义的post类型,如果存在,则将其插入到输出中。

已编辑*

1 个回复
最合适的回答,由SO网友:Wyck 整理而成

要更改默认或主循环,可以添加query_posts 循环运行之前。

http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query (参数)

例如,在上面的代码中,包含所有帖子,包括您将编写的自定义帖子类型;

<?php query_posts( \'post_type=any\'); ?>
<?php while (have_posts()) : the_post(); ?>
//rest of your code from above
如果您只想包含所有默认帖子和一个名为“mass\\u list”的自定义帖子类型,我建议在这种情况下使用WP Query over Query\\u posts。

<?php $query = new WP_Query( array( \'post_type\' => array( \'post\',  \'mass_list\' ) ) ); ?>
<?php while (have_posts()) : the_post(); ?>
    //rest of your code from above
您可以通过查看上面链接中WP Query的参数来深入查看,因为您可以看到WP Query的选项允许许多自定义输出。

结束

相关推荐

Custom Author Loop

本质上,我想做的是与wp\\u list\\u authors标记具有相同的效果,但我不想只列出名称或他们的帖子数量,而是希望能够使用ID进行循环。最终,我想有一个页面与每个作者,与他们的名字,描述,化身和链接。我可以弄清楚具体情况,我只需要一种方法来做如下事情:foreach $author as $author->ID 提前感谢,皮特