只有第一篇文章有​​循环内容

时间:2014-10-01 12:25:30

标签: php wordpress loops

我的Wordpress网站有一个问题: 在我的博客循环中,只有第一篇文章显示内容;其余the_content();似乎从未被调用(使用&#34检查;检查元素")。 我的代码中的任何probs?

的PHP / HTML:

<section id="blogPosts" class="clearfix"> 
<?php $myposts = get_posts('');
    foreach($myposts as $post) :
        setup_postdata($post);
    ?>
    <div id="post-&lt;?php the_ID(); ?&gt;" class="post clearfix">
        <div class="postHeader clearfix">
            <a href="&lt;?php the_permalink();?&gt;"> 
            <h2><?php the_title(); ?></h2>
            </a> 
            <h4><?php the_date(); ?></h4>
        </div>
        <div class="postTags clearfix">
            <ul class="tagContainer clearfix">
        <?php the_tags( '<li><div class="tagInline">',
                        '</div></li><li><div class="tagInline">', 
                        '</div></li>'); ?>
            </ul>
        </div>
        <div class="blogContent">
    <?php the_content();?>
        </div>
        <div class="editPost">
    <?php edit_post_link('<h4>Redigera detta inlägg', '', '</h4>'); ?>
        </div>
    <?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>
    <?php //comments_template(); ?>
    </div>
<?php endforeach; wp_reset_postdata(); ?>
</section>

2 个答案:

答案 0 :(得分:0)

检查所有其他帖子中是否包含内容.. 帖子可以通过

获得
<?php $posts_array = get_posts( $args ); ?> 

args的默认用法:

<?php $args = array(
    'posts_per_page'   => 5,
    'offset'           => 0,
    'category'         => '',
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true ); ?>

访问所有帖子数据

<?php
$args = array( 'posts_per_page' => -1 );
$allposts = get_posts( $args );
foreach ( $allposts as $post ) :
  setup_postdata( $post ); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
<?php endforeach; 
wp_reset_postdata(); ?>

这是获取所有帖子的简单代码。您可以通过打印数据来检查代码(print_r($ post);)以检查您在循环中获得的内容。

答案 1 :(得分:0)

如果你正在谈论在Wordpress阅读设置下分配给帖子的页面,你可以摆脱大部分代码并简化它:

<?php if (have_posts()): while (have_posts()) : the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_content(); ?> <?php endwhile; endif; ?>

只需在 while 语句中输入您想要的任何信息,您就会变得更好。如果您想更具体,请参阅Wordpress codex

相关问题