【问题标题】:Display wordpress posts in bootstrap grid with different classes在具有不同类的引导网格中显示 wordpress 帖子
【发布时间】:2015-11-25 10:29:19
【问题描述】:

我正在使用从Blog 获得的脚本,该脚本在非 wordpress 页面上显示 3 个 wordpress 帖子,如下所示:

<div class="wrapper row">
    <?php
    define('WP_USE_THEMES', false);
    require('news/wp-load.php');
    query_posts('showposts=3');
    ?>

    <?php while (have_posts()): the_post(); ?>
        <div class="col-sm-4">
            <h2><?php the_title(); ?></h2>
            <p><em>Posted <?php the_date(); ?> by <?php the_author(); ?></em></p>
            <div class="image"><?php
                if ( has_post_thumbnail() )
                    the_post_thumbnail( 'thumbnail' );
                else
                    echo '<img src="news/wp-content/uploads/2015/06/150x150-2.jpg" alt="Example Image" title="Example" width="100px"/>';
                ?></div>
            <p><?php the_excerpt(); ?></p>
            <p><a href="<?php the_permalink(); ?>">Read more...</a></p>
        </div><!-- /.col-sm-4 -->
    <?php endwhile; ?>
</div>

我目前在引导网格中得到循环(3 个帖子),在 12 列网格中重复“col-sm-4”div。

我想做的是将第一个帖子放在“col-sm-6”div 中,另外两个放在“col-sm-3”div 中。

有没有办法可以调整这段代码来做到这一点?

提前致谢!

【问题讨论】:

    标签: php wordpress twitter-bootstrap


    【解决方案1】:

    您可以有一个简单的$first = true 标志,然后可以使用它来输出col-sm-6。在第一次运行完成之前,您将标志设置为false

    考虑到这一点,这是您重写的代码:

    <div class="wrapper row">
    <?php
    define('WP_USE_THEMES', false);
    require('news/wp-load.php');
    query_posts('showposts=3');
    ?>
    
    <?php $first = true; while (have_posts()): the_post(); ?>
    <div class="<?php if ($first) { echo 'col-sm-6'; } else { echo 'col-sm-3'; } ?>">
    <h2><?php the_title(); ?></h2>
    <p><em>Posted <?php the_date(); ?> by <?php the_author(); ?></em></p>
    <div class="image"><?php
    if ( has_post_thumbnail() )
        the_post_thumbnail( 'thumbnail' );
    else
         echo '<img src="news/wp-content/uploads/2015/06/150x150-2.jpg"   alt="Example Image" title="Example" width="100px"/>';
    ?></div>
    <p><?php the_excerpt(); ?></p>
    <p><a href="<?php the_permalink(); ?>">Read more...</a></p>
    </div><!-- /.col-sm-4 -->
    <?php $first = false; endwhile; ?>
    </div>
    

    【讨论】:

    • 感谢 Luka,这里是否存在代码拼写错误,因为它破坏了页面。 Dreamweaver 挑选出有错误的以下行。 &lt;div class="&lt;?php if ($first) { echo 'col-sm-6' } else { echo 'col-sm-3' }"&gt;
    • 史蒂夫,是的,我有一个错字。现在应该可以了。
    • 卢卡,感谢您的更新。不幸的是,它仍然打破了页面。 Dreamweaver 仍然不喜欢同一行代码&lt;div class="&lt;?php if ($first) { echo 'col-sm-6' } else { echo 'col-sm-3' } ?&gt;"&gt;。有什么想法吗?
    • 史蒂夫,缺少一些分号。我已经添加了它们,测试了这段代码,它现在可以运行了。很抱歉给您带来不便。
    • Luka,知道如何通过显示永久链接列表 (&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;) 来添加此内容,但不包括前三个帖子。理想情况下,这将出现在与 3 个帖子同一行的第四列中。因此,我会将 div 更改为 - 'col-sm-4' 用于帖子 1,'col-sm-3' 用于帖子 2 和 3,'col-sm-2' 用于除前三个帖子之外的永久链接。跨度>
    【解决方案2】:

    为此,您只需检查循环是否第一次运行,即它是否显示第一篇文章。

    通过简单地初始化变量并使其在每次while完成其循环时增加1。

    我已经为它制作了虚拟代码,让你清楚地理解。

    <?php 
    $sn = 1; //initializing variable assigning 1 to it
    while (have_posts()): the_post(); // your while loop
    if($sn == 1){ // here we will check if $sn = 1 which means we are have first post if not then it might be 2nd or third
    ?>
        <div class="col-sm-6">
    <?php } else { ?>
        <div class="col-sm-4">    
    <?php } ?>        
            <h2><?php the_title(); ?></h2>
            <p><em>Posted <?php the_date(); ?> by <?php the_author(); ?></em></p>
            <div class="image">
                <?php
                if ( has_post_thumbnail() )
                    the_post_thumbnail( 'thumbnail' );
                else
                    echo '<img src="news/wp-content/uploads/2015/06/150x150-2.jpg" alt="Example Image" title="Example" width="100px"/>';
                ?>
            </div>
            <p><?php the_excerpt(); ?></p>
            <p><a href="<?php the_permalink(); ?>">Read more...</a></p>
        </div><!-- /.col-sm-4 -->
    <?php 
    $sn++;  // very important to increment variable by 1  so that we can check how many time loop has run
    endwhile; ?>
    

    希望对你有帮助。

    【讨论】:

    • 感谢 Yatin 的努力,但这段代码破坏了页面。你能看到任何代码错别字吗?
    【解决方案3】:
    <div class="wrapper row">
        <?php
        define('WP_USE_THEMES', false);
        require('news/wp-load.php');
        query_posts('showposts=3');
    
        $isFirst = true;
        while (have_posts()): the_post();
        if($isFirst == true) {
            echo '<div class="col-sm-6">';
            $isFirst = false;
        } else {
            echo '<div class="col-sm-4">';
        }
        ?><h2><?php the_title(); ?></h2>
        <p><em>Posted <?php the_date(); ?> by <?php the_author(); ?></em></p>
        <div class="image"><?php
            if ( has_post_thumbnail() )
                the_post_thumbnail( 'thumbnail' );
            else
                echo '<img src="news/wp-content/uploads/2015/06/150x150-2.jpg" alt="Example Image" title="Example" width="100px"/>';
            ?></div>
            <p><?php the_excerpt(); ?></p>
            <p><a href="<?php the_permalink(); ?>">Read more...</a></p>
        </div><!-- /.col-sm-4 -->
    <?php endwhile; ?>
    </div>
    

    试试这个代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多