Wordpress 不同级别的职位的无序列表

Wordpress 不同级别的职位的无序列表,wordpress,Wordpress,我想创建一个无序列表,其中包含一个类别中的最后3篇文章,每一篇文章都有一个不同的类 这是我目前的代码: <?php query_posts('cat=3'); ?> <?php while(have_posts()) : the_post(); ?> <li> <strong><?php the_title(); ?></strong> <br /

我想创建一个无序列表,其中包含一个类别中的最后3篇文章,每一篇文章都有一个不同的类

这是我目前的代码:

<?php query_posts('cat=3'); ?>
    <?php while(have_posts()) : the_post(); ?>
        <li>
            <strong><?php the_title(); ?></strong>
            <br />
            <?php the_content(); ?>
        </li>
    <?php endwhile; ?>
<?php wp_reset_query(); ?>



  • 请教我如何为每个LI添加不同的类。举例来说,我想要红色、蓝色和棕色的类名。

    您只需要一个方法来返回用于li元素的类。假设您的css中有一个红色、蓝色和棕色的类,您可以这样做:

    <?php 
    query_posts('cat=3');
    $elt_count = 0;
    while(have_posts()) : 
        the_post();
        $elt_count++;
    ?>
        <li class="<?php get_class_for_li_elt($elt_count); ?>">
             <strong><?php the_title(); ?></strong>
             <br />
             <?php the_content(); ?>
        </li>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    
    
    
    您可以对所有帖子使用以下代码、特殊类:

    <li class="myspecialclassname_<?php the_ID(); ?>">
    

    Hi Eric,这将返回以下错误:解析错误:语法错误,在中时出现意外的T\u。无论如何,谢谢你的时间。嗨,埃里克,不幸的是没有。错误现在是:解析错误:语法错误,意外的T_变量。是的,我现在没有php设置。这些只是语法错误。在线测试。现在应该可以工作了。最后。嗨,埃里克,我试过了,但结果是一个空列表。其他想法;-)?这一定是因为您的函数具有\u post返回false。不幸的是,我不能在这方面帮助你。所有职位的特殊课程都在运作,但不幸的是,这不是一个选项。第一级必须始终为“红色”,第二级为“蓝色”,第三级为“棕色”。不幸的是,动态颜色类的代码不起作用。结果是空的。对不起,我来晚了。首先,必须在functions.php上创建一个get_color()函数。这个函数包括这样的if:get_color($id){if($id%3==0){return“red”}elseif($id%3==1){return“blue”}elseif($id%3==2){return“brown}}Hi Hkulekci,我试过了,但给出了一个解析错误:语法错误,意外的“}”错误。我的功能如下:函数get_color($id){if($id%3==0){return“red”}elseif($id%3==1){return“blue”}elseif($id%3==2){return“brown”}}我对wordpress很有经验,可以这样修改代码。我希望这不会出现任何错误。我没有使用functions.php文件。您可以使用它来实现get\u color()function.Hi Hkulekci。我尝试了您编辑的代码。现在显示的帖子没有错误,但每个LI中的class属性都保持为空。
    
    <?php 
        function get_color($id) { 
            if ($id%3 == 0){return "red";} 
            else if ($id%3 == 1){return "blue";} 
            else if ($id%3 == 2){return "brown";} 
        }
    ?>
    <?php query_posts('cat=3'); ?>
    <?php $counter = 0; ?>
        <?php while(have_posts()) : the_post(); ?>
            <li class="<?php get_color($counter++); ?>">
                <strong><?php the_title(); ?></strong>
                <br />
                <?php the_content(); ?>
            </li>
        <?php endwhile; ?>
    <?php wp_reset_query(); ?>