Wordpress - 循环内帖子的不同 html [英] Wordpress - Different html for posts within loop

查看:20
本文介绍了Wordpress - 循环内帖子的不同 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么:

在我的循环中编写一个 elseif 语句,为页面上的第一、第二和第三个帖子显示不同的 html 标记.

代码

 

<?php if (have_posts()) : ?><?php $count = 0;?><?php while (have_posts()) : the_post();?><?php $count++;?><?php if ($count == 1) : ?><!-- 这是第一篇文章--><文章<?php post_class() ?>id="post-<?php the_ID(); ?>">//第一篇文章的html<?php the_title();?>//等等</文章><!-- 结束 div 帖子--><?php elseif : ?><?php if ($count == 2) : ?><文章<?php post_class() ?>id="post-<?php the_ID(); ?>">//第二个帖子<?php the_title();?>//等等</文章><!-- 结束 div 帖子--><?php elseif : ?><?php if ($count == 3) : ?><文章<?php post_class() ?>id="post-<?php the_ID(); ?>">//第三个帖子<?php the_title();?>//等等</文章><!-- 结束 div 帖子--><?php 其他:?><文章<?php post_class() ?>id="post-<?php the_ID(); ?>">所有其他帖子<?php the_title();?>等等</文章><!-- 结束 div 帖子--><?php endif;?><?php endwhile;?><?php endif;?>

<!-- 结束 div 内容-->

实际发生的事情:

没什么.我显然做错了.我已经对代码进行了注释,因此您应该对我在这里尝试实现的目标有所了解.也许你能指出我正确的方向!

这是一些工作代码,第一篇文章的标记与其他文章不同.

<?php if (have_posts()) : ?><?php $count = 0;?><?php while (have_posts()) : the_post();?><?php $count++;?><?php if ($count == 1) : ?><!-- 这是第一篇文章--><文章<?php post_class() ?>id="post-<?php the_ID(); ?>">//第一篇文章的html<?php the_title();?>//等等</文章><!-- 结束 div 帖子--><?php 其他:?><文章<?php post_class() ?>id="post-<?php the_ID(); ?>">所有其他帖子<?php the_title();?>等等</文章><!-- 结束 div 帖子--><?php endif;?><?php endwhile;?><?php endif;?>

<!-- 结束div内容-->

我只想将其扩展为第一、第二和第三个帖子具有不同的 html.

解决方案

问题

 <?php if ($count == 3) : ?>

应该是

elseif($count == 3) :

更好的方法

您可以使用 get_template_part 来降低嵌套 if 的复杂性.为 articlepart.php 和 articlepart-1.php 等模板单独创建

if(have_posts()):$count = 0;while (have_posts()) : the_post();$count++;get_template_part('articlepart', $count);终了;万一;

What I'm trying to do:

Write an elseif statement in my loop to display different html markup for the first, second and third posts on the page.

The code

  <div id="content">

        <?php if (have_posts()) : ?>

            <?php $count = 0; ?> 
            <?php while (have_posts()) : the_post(); ?> 
            <?php $count++; ?> 
            <?php if ($count == 1) : ?> 


            <!-- this is the 1st post -->

            <article <?php post_class() ?> id="post-<?php the_ID(); ?>">

                //the html for the first post
                <?php the_title(); ?> //etc

            </article> <!-- end div post -->


            <?php elseif : ?>
            <?php if ($count == 2) : ?>


            <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
                //second post                   
                <?php the_title(); ?> //etc


            </article> <!-- end div post -->


            <?php elseif : ?>
            <?php if ($count == 3) : ?>


            <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
                //third post                    
                <?php the_title(); ?> //etc


            </article> <!-- end div post -->


            <?php else : ?>

        <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
            all the other posts     
            <?php the_title(); ?> etc


        </article> <!-- end div post -->


    <?php endif; ?> 
    <?php endwhile; ?>
    <?php endif; ?>


    </div> <!-- end div content -->

What's actually happening:

Nothing. I'm clearly doing it wrong. I've commented the code so you should have an idea of what I'm trying to achieve here. Perhaps you can point me in the right direction!

Here's some working code, the first post has different markup from the others.

<div id="content">

                <?php if (have_posts()) : ?>

                    <?php $count = 0; ?> 
                    <?php while (have_posts()) : the_post(); ?> 
                    <?php $count++; ?> 
                    <?php if ($count == 1) : ?> 


                    <!-- this is the 1st post -->

                    <article <?php post_class() ?> id="post-<?php the_ID(); ?>">

                        //the html for the first post
                        <?php the_title(); ?> //etc

                    </article> <!-- end div post -->





                    <?php else : ?>

                <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
                    all the other posts     
                    <?php the_title(); ?> etc


                </article> <!-- end div post -->


            <?php endif; ?> 
            <?php endwhile; ?>
            <?php endif; ?>


            </div> <!-- end div content -->

I just want to expand this to have different html for the first, second and third posts.

解决方案

Problem

 <?php elseif : ?>
 <?php if ($count == 3) : ?>

It's should be

elseif($count == 3) :

Better way

you can use get_template_part to reduce the complexity nested if. created separate for templates like articlepart.php and articlepart-1.php etc.

if(have_posts()):
$count = 0;
    while (have_posts()) : the_post();
        $count++;
        get_template_part('articlepart', $count);

    endwhile;
endif;

这篇关于Wordpress - 循环内帖子的不同 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆