0

我正在制作在线杂志。现在我正在尝试显示我发布的帖子。我已经做到了,但我想要的是,如果一个帖子被归类为“街头风格”,那么如果它被归类为“摄影”,那么循环将完全不同。我已经设法对一个类别做到这一点,我怎样才能对其他类别做同样的事情,并以另一种方式对其进行样式设置?我尝试使用与 相同的代码<?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(); ?>
4

2 回答 2

0

我已经瘦了一点,但这可能是你所追求的

<?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(); ?>
于 2012-09-10T16:01:36.157 回答
0

如果您有很多类别,我建议您使用 switch,它将节省您的时间,并且您的代码看起来会更干净。

http://php.net/manual/en/control-structures.switch.php

于 2012-09-10T14:53:35.950 回答