简体   繁体   English

如何设置交替的WordPress帖子的样式?

[英]How do I style alternating WordPress posts?

I have some posts displaying from a specific category but need them to have a little different styling on every second post. 我有一些特定类别的帖子显示,但需要它们在每第二个帖子上有一些不同的样式。

This code I have displays all posts in a list with the same styling. 我使用的这段代码以相同的样式显示列表中的所有帖子。

How do I edit this code and make it so I can edit the styling of ONLY every second post? 如何修改这些代码,制作它,所以我只能编辑每一秒后的造型?

As it stands there's text on the left and an image on the right - the second posts just need to switch sides - it's simple styling but I'm unsure how to break the posts up to edit #case-left & #case-right are the two divs that need to switch. 就目前而言,左边是文本,右边是图像-第二个帖子只需要切换侧面-这是简单的样式,但是我不确定如何拆分这些帖子以编辑#case-left#case-right是需要切换的两个div。

Thanks in advance. 提前致谢。

<?php // PAGE LINK/TITLE          
if (is_page()) {
    $cat=get_cat_ID($post->post_title); //use page title to get a category ID
    $posts = get_posts ("category_name=case-study&posts_per_page=10");

    if ($posts) {
        foreach ($posts as $post):
        setup_postdata($post); 

?>

    <div class="serve-inner-split"> 
        <div id="case-split">
            <div id="case-left" class=" serve-left">
                <div id="case-study-content">
                    <h1 class="case-study-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                    <p><?php //PULLS IN EXCERPT
                        $my_excerpt = get_the_excerpt();
                        if ( '' != $my_excerpt ) {
                        // Some string manipulation performed
                        }
                        echo $my_excerpt; // Outputs the processed value to the page
                    ?>
                    </p>
                    <a href="<?php the_permalink() ?>" class="header-quote">READ CASE STUDY</a>
                </div>
            </div>
            <div id="case-right" class="serve-grey">
                <?php
                if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
                    the_post_thumbnail();
                } 
                ?>
            </div>
        </div>
    </div>

    <?php endforeach;
    }
}

?> 
<?php // PAGE LINK/TITLE          
if (is_page()) {
  $cat=get_cat_ID($post->post_title); //use page title to get a category ID
  $posts = get_posts ("category_name=case-study&posts_per_page=10");
  if ($posts) {
    $counter = 1;
    $class = '';
    foreach ($posts as $post):
      setup_postdata($post); 
       if($counter%2 == 0) {
          $class = "even-no";
       } else {
        $class = '';
       }

?>
<div class="serve-inner-split <?php echo $class; ?>"> 
                <div id="case-split">
                        <div id="case-left" class=" serve-left">
                            <div id="case-study-content">
                                <h1 class="case-study-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                                <p><?php //PULLS IN EXCERPT
                                    $my_excerpt = get_the_excerpt();
                                    if ( '' != $my_excerpt ) {
                                        // Some string manipulation performed
                                    }
                                    echo $my_excerpt; // Outputs the processed value to the page

                                    ?>
                                </p>
                                <a href="<?php the_permalink() ?>" class="header-quote">READ CASE STUDY</a>
                            </div>
                        </div>
                        <div id="case-right" class="serve-grey">
   <?php
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
    the_post_thumbnail();
} 
?>
                        </div>
                    </div>
</div>


<?php $counter++; ?>
<?php endforeach;
  }
}
?>

Ok, so when editing your question I saw that you want to style it so that the divs switch places. 好的,所以在编辑您的问题时,我看到您想为其设置样式,以便div切换位置。 You can do that like this in php 您可以在php中这样做

<?php // PAGE LINK/TITLE
if (is_page()){}
    $i = 0;
    $cat=get_cat_ID($post->post_title); //use page title to get a category ID
    $posts = get_posts ("category_name=case-study&posts_per_page=10");
    if ($posts) {
        foreach ($posts as $post):
        setup_postdata($post);
    $i++;
    if ($i % 2 == 0):?>
        <div class="serve-inner-split">
            <div id="case-split">
                <?php if ( has_post_thumbnail() ):?>
                <div id="case-right" class="serve-grey">
                    <?php the_post_thumbnail(); ?>
                </div>
                <?php endif; ?>
                <div id="case-left" class=" serve-left">
                    <div id="case-study-content">
                        <h1 class="case-study-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                        <p><?php //PULLS IN EXCERPT
                            $my_excerpt = get_the_excerpt();
                            if ( '' != $my_excerpt ) {
                                // Some string manipulation performed
                            }
                            echo $my_excerpt; // Outputs the processed value to the page

                            ?>
                        </p>
                        <a href="<?php the_permalink() ?>" class="header-quote">READ CASE STUDY</a>
                    </div>
                </div>
            </div>
        </div>
    <?php else: ?>
        <div class="serve-inner-split">
            <div id="case-split">
                <div id="case-left" class=" serve-left">
                    <div id="case-study-content">
                        <h1 class="case-study-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                        <p><?php //PULLS IN EXCERPT
                            $my_excerpt = get_the_excerpt();
                            if ( '' != $my_excerpt ) {
                                // Some string manipulation performed
                            }
                            echo $my_excerpt; // Outputs the processed value to the page

                            ?>
                        </p>
                        <a href="<?php the_permalink() ?>" class="header-quote">READ CASE STUDY</a>
                    </div>
                </div>
                <?php if ( has_post_thumbnail() ):?>
                <div id="case-right" class="serve-grey">
                    <?php the_post_thumbnail(); ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    <?php endif;
?>
<?php endforeach;
wp_reset_postdata();
} ?>

But in my opinion, that's kinda unnecessary. 但是我认为这是不必要的。 You can also do it like this: 您也可以这样:

<?php // PAGE LINK/TITLE
if (is_page()){}
    $i = 0;
    $cat=get_cat_ID($post->post_title); //use page title to get a category ID
    $posts = get_posts ("category_name=case-study&posts_per_page=10");
    if ($posts) {
        foreach ($posts as $post):
        setup_postdata($post);
    $class = '';
    $i++;
    if ($i % 2 == 0){
        $class = 'right_image'
    }
?>
        <div class="serve-inner-split <?php echo $class; ?>">
            <div id="case-split">
                <div id="case-left" class=" serve-left">
                    <div id="case-study-content">
                        <h1 class="case-study-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                        <p><?php //PULLS IN EXCERPT
                            $my_excerpt = get_the_excerpt();
                            if ( '' != $my_excerpt ) {
                                // Some string manipulation performed
                            }
                            echo $my_excerpt; // Outputs the processed value to the page

                            ?>
                        </p>
                        <a href="<?php the_permalink() ?>" class="header-quote">READ CASE STUDY</a>
                    </div>
                </div>
                <?php if ( has_post_thumbnail() ):?>
                <div id="case-right" class="serve-grey">
                    <?php the_post_thumbnail(); ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
<?php endforeach;
wp_reset_postdata();
} ?>

And then float the image the other way (or use absolute positioning or whatever suits you), with the '.right_image' class in the css. 然后使用css中的'.right_image'类以其他方式(或使用绝对定位或任何适合您的方式)浮动图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM