【问题标题】:wordpress wrap each post in its own divwordpress 将每个帖子包装在自己的 div 中
【发布时间】:2014-03-04 19:42:02
【问题描述】:

我希望能够单独设置每个 WP 帖子的样式,并让 while post 循环自动将每个新帖子分配给它自己的 div。例如:

<div id="blog-content-wp">
  <div class="post">
    Post 1
  </div>
  <div class="post">
    Post 2
  </div>
</div>

这是我目前的代码:

<div id="blog-content-wp">
<?php while(have_posts()): the_post()?>
  <?php $myposts = get_posts('');
    foreach($myposts as $post) :
    setup_postdata($post);
  ?>
<?php the_content(); ?>          
<?php endforeach; wp_reset_postdata(); ?>
<?php endwhile;?>
<?php comments_template('', true);?>
</div>

所有这一切都是将我所有的帖子放在 blog-content-wp 中,而我想要实现的只是每个帖子都有一个单独的 div。

有什么建议吗?我知道这可能是重复的,但到目前为止我所看到的只是“如何每个 div 获得三个帖子”等。

谢谢

【问题讨论】:

    标签: php wordpress html


    【解决方案1】:

    在你的循环中打印出新的 div,包裹你对 the_content() 的调用

    <div id="blog-content-wp">
    <?php while(have_posts()): the_post()?>
      <?php $myposts = get_posts('');
        foreach($myposts as $post) :
        setup_postdata($post);
      ?>
    
    <div >
    <?php the_content(); ?>          
    </div >
    
    <?php endforeach; wp_reset_postdata(); ?>
    <?php endwhile;?>
    <?php comments_template('', true);?>
    </div>
    

    【讨论】:

      【解决方案2】:

      使用 post 类,提供带有标签、类等的类

        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <?php the_content(); ?>
        </div>  
      

      完整参考:
      http://codex.wordpress.org/Function_Reference/post_class

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-30
        • 2011-12-19
        • 2016-11-30
        • 2014-07-20
        相关资源
        最近更新 更多