我正在制作一本在线杂志。现在我正在尝试显示我发布的帖子。我已经做到了,但我想要的是,如果一篇帖子被归类为"街头风格",那么它的循环将与归类为"摄影"完全不同。我已经为一个类别做了漫画,我怎么能对其他类别做同样的事情,并用另一种方式来设计它呢?我尝试使用与<?php if (is_category( 'Streetstyle' ) || in_category( 'Streetstyle' ) ) { ?>
相同的代码,但后来主题出现了问题,帖子出现了两次。你知道有更好的方法吗?
这是我的代码:
<?php query_posts('posts_per_page=9' . '&orderby=date');
while ( have_posts() ) : the_post(); ?>
<?php if (is_category( 'Streetstyle' ) || in_category( 'Streetstyle' ) ) { ?>
<div <?php post_class('pin streetstyle'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="heading">
<h1>Fashion</h1>
</div>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<div class="heading">
<h1><?php the_title(); ?></h1>
</div>
</a>
</div>
<?php } else { ?>
<div <?php post_class('pin'); ?>>
<h1>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h1>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
the_content('Les mer <br><br>'); ?>
</div>
<?php } ?>
<?php endwhile;
// Reset Query
wp_reset_query(); ?>
如果您有很多类别,我建议您使用switch,这将节省您的时间,并且您的代码看起来会干净得多。
http://php.net/manual/en/control-structures.switch.php
我已经精简了一点,但这可能更像
<?php query_posts('posts_per_page=9' . '&orderby=date');
while(have_posts()) : the_post(); ?>
<?php if(is_category('Streetstyle') || in_category('Streetstyle')) : ?>
<div <?php post_class('pin streetstyle'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="heading">
<h1><?php the_title(); ?></h1>
</div>
</a>
</div>
<?php if(has_post_thumbnail()) the_post_thumbnail(); ?>
<?php else : ?>
<div <?php post_class('pin'); ?>>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php
if(has_post_thumbnail()) the_post_thumbnail();
the_content('Les mer <br><br>');
?>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>