php - 第一个帖子风格不同,其他帖子风格不同

标签 php css wordpress

我遇到了 WP_Query 和 HTML 输出的问题:

查询:

<!-- Category posts -->
<!-- Entretencion -->
<article class="six column">
<?php
$caps = new WP_Query();
$caps->query('posts_per_page=4&category_name=entretencion');
while($caps->have_posts()) {
    $caps->the_post();
    $count++;
    if($count == 1) {
?>
<!--First post -->
<h4 class="cat-title"><a href="#"><?php $category = get_the_category();  echo $category[0]->cat_name; ?></a></h4>
<div class="post-image">
    <a href="#"><img src="<?php bloginfo( 'template_url' ); ?>/upload/imagepost1.jpg" alt=""></a>
</div>

<div class="post-container">
    <h2 class="post-title">Create a Flexible Folded Paper Effect Using CSS3 Features</h2>
    <div class="post-content">
        <p>Venenatis volutpat orci, ut sodales augue tempor nec. Integer tempus ullamcorper felis eget dipiscing. Maecenas orci justo, mollis at tempus ac, gravida non</p>
    </div>
</div>

<div class="post-meta">
    <span class="comments"><a href="#">24</a></span>
    <span class="author"><a href="#">nextwpthemes</a></span>
    <span class="date"><a href="#">13 Jan 2013</a></span>
</div>
<!-- End of first post -->              
<?php } if($count >= 2) { //Contador ?>
<!-- Second and others posts -->
<div class="other-posts">
    <ul class="no-bullet">
        <!-- Repeat this list with post 2, 3, and 4 -->
        <li id="<?php echo $post->ID; ?>">
            <a href="#"><img src="<?php bloginfo( 'template_url' ); ?>/upload/thumb1.jpg" alt=""></a>
            <h3 class="post-title"><a href="#">Check Out the New Recommended Resources on Webdesigntuts+</a></h3>
            <span class="date"><a href="#">13 Jan 2013</a></span>
        </li>
    </ul>
</div>
<?php } // Fin contador ?>
<?php } ?>
</article>

但是 HTML 输出重复 3 个 div 类 other-post

问题(左)原题(右)

enter image description here

如何只重复li标签?

最佳答案

<!-- Category posts -->
<!-- Entretencion -->
<article class="six column">
<?php
$count = '';
$perpage = 4;
$caps = new WP_Query();
$caps->query('posts_per_page='.$perpage.'&category_name=entretencion');

while($caps->have_posts()) {
    $caps->the_post();
    $count++;
    if($count == 1) {
?>
<!--First post -->
<h4 class="cat-title"><a href="#"><?php $category = get_the_category();  echo $category[0]->cat_name; ?></a></h4>
<div class="post-image">
    <a href="#"><img src="<?php bloginfo( 'template_url' ); ?>/upload/imagepost1.jpg" alt=""></a>
</div>

<div class="post-container">
    <h2 class="post-title">Create a Flexible Folded Paper Effect Using CSS3 Features</h2>
    <div class="post-content">
        <p>Venenatis volutpat orci, ut sodales augue tempor nec. Integer tempus ullamcorper felis eget dipiscing. Maecenas orci justo, mollis at tempus ac, gravida non</p>
    </div>
</div>

<div class="post-meta">
    <span class="comments"><a href="#">24</a></span>
    <span class="author"><a href="#">nextwpthemes</a></span>
    <span class="date"><a href="#">13 Jan 2013</a></span>
</div>
<!-- End of first post -->              
<?php
    }

    //only on second post/loop
    if($count == 2) {
?>
    <!-- Second and others posts -->
    <div class="other-posts">
        <ul class="no-bullet">
<?php   
    }

    if($count >= 2) { //Contador
?>
        <!-- Repeat this list with post 2, 3, and 4 -->
        <li id="<?php echo $post->ID; ?>">
            <a href="#"><img src="<?php bloginfo( 'template_url' ); ?>/upload/thumb1.jpg" alt=""></a>
            <h3 class="post-title"><a href="#">Check Out the New Recommended Resources on Webdesigntuts+</a></h3>
            <span class="date"><a href="#">13 Jan 2013</a></span>
        </li>
<?php
    }

    //again, only on final loop
    if($count == $perpage) {
?>
        </ul>
    </div>
<?php
    }
}
?>
</article>

这将在第二个循环中只输出一个 li,并且在 $perpage 结束时(我将你要求的每页 4 个保存到原始 WP_Query)。 这很丑陋,但应该有效。

关于php - 第一个帖子风格不同,其他帖子风格不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18775446/

相关文章:

html - 如何让我的按钮响应?

css - 使用 css 从页脚隐藏 URL

javascript - Angular - ngAnimate - Animate.css - 元素首先显示然后动画开始

php - 从 Level3 CDN 保护 RT​​MP 流的问题

PHP 数据库将哈希登录与 db 进行比较

php - 将行数存储在数据库中或动态获取

javascript - Onclick 播放多个视频的 Jquery 触发器

WordPress简码参数

php - 如何通过PHP将正则表达式数据的完整内容插入wordpress数据库?

php - 如何获取mysql表列的小数位数