dongmacuo1193 2015-11-06 12:12 采纳率: 0%
浏览 16

如何设置WordPress的交替帖子?

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.

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;
    }
}

?> 
  • 写回答

2条回答 默认 最新

  • douliedu335997 2015-11-06 12:31
    关注
    <?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;
      }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值