how do i make this loop work

时间:2015-03-27 作者:luka

抱歉,我是一个PHp初学者,我不知道为什么这个循环对我的标签不起作用。php页面欢迎任何帮助。

<?php get_header(); ?>
<div class="posts"><!-- BLOG -->

    <!-- Shapes on sides -->
    <div class="shapes_left"> </div>
    <div class="shapes_right"> </div>

        <?php if (is_tag()) { ?>
            <div id="archive_title">
                <h1><?php single_tag_title(); ?></h1>
                <?php }?>
        </div>


    <div id="featured_home">        

        <?php $counter = 0;
            while ( have_posts() ) {
                $counter += 1;
                if ( $counter > 5 ) {
                    break; 
                    }
                    the_post();

         ?>
        <article class="sticky">
            <div class="desc">
                    <div class="desc_over"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div>
                 <?php the_post_thumbnail(large); ?> 


            </div>
        </article>
        <?php }?>
        </div>





        <?php while( have_posts() ) {
            the_post();
            // it\'s a post! Display the post!
                    } ?>


            <div class="post_main">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <ul class="postinfo">
                        <li>Posted by <?php the_author_link(); ?></li>
                        <li><?php the_time( \'jS F Y\'); ?></li>
                    </ul>   

                 <?php the_content( \'\'); ?>

                <div class="read_more_blog"><a href="<?php the_permalink(); ?>">Read More</a></div>
            </div>

        <?php endwhile; ?>
        <nav id="pagination"> <!-- PAGINATION FOR BLOG -->
            <ul>
                <li class="older"><?php next_posts_link( \'Older posts\'); ?></li>
                <li class="newer"><?php previous_posts_link( \'Newer posts\'); ?></li>
            </ul>
        </nav>

        </div> 
        <!-- END OF BLOG PAGINATION -->
        <?php else : ?>
         <div id="post">
            <!-- 404 Messege -->
                <h3>404 ERROR!!</h3>

            <p>Sorry we can\'t seem able to find what you are looking for</p>
            <p><a href="<?php echo home_url(); ?>">Click here to get back to the homepage</a>
            </p>
        </div>
        <?php endif; ?>
        <?php get_sidebar(); ?>
    </div>
    <?php get_footer(); ?>

2 个回复
最合适的回答,由SO网友:erichmond 整理而成

您应该进一步清理它,但这将修复您的代码:

<?php get_header(); ?>
<div class="posts"><!-- BLOG -->

    <!-- Shapes on sides -->
<div class="shapes_left"> </div>
<div class="shapes_right"> </div>

    <?php if (is_tag()) { ?>
        <div id="archive_title">
            <h1><?php single_tag_title(); ?></h1>
            <?php }?>
    </div>


<div id="featured_home">        

    <?php $counter = 0;
        while ( have_posts() ) {
            $counter += 1;
            if ( $counter > 5 ) {
                break; 
                }
                the_post();

     ?>
    <article class="sticky">
        <div class="desc">
                <div class="desc_over"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div>
             <?php the_post_thumbnail(large); ?> 


        </div>
    </article>
    <?php }?>
    </div>





    <?php
    // I\'ve added the missing if statement and corrected some php grammar
    if ( have_posts() ) : while( have_posts() ) :
        the_post();
        // it\'s a post! Display the post!
                 ?>


        <div class="post_main">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <ul class="postinfo">
                    <li>Posted by <?php the_author_link(); ?></li>
                    <li><?php the_time( \'jS F Y\'); ?></li>
                </ul>   

             <?php the_content( \'\'); ?>

            <div class="read_more_blog"><a href="<?php the_permalink(); ?>">Read More</a></div>
        </div>

    <?php endwhile; ?>
    <nav id="pagination"> <!-- PAGINATION FOR BLOG -->
        <ul>
            <li class="older"><?php next_posts_link( \'Older posts\'); ?></li>
            <li class="newer"><?php previous_posts_link( \'Newer posts\'); ?></li>
        </ul>
    </nav>

    </div> 
    <!-- END OF BLOG PAGINATION  -->
    <!-- because the if statement has been started the else will now work  -->
    <?php else : ?>
     <div id="post">
        <!-- 404 Messege -->
            <h3>404 ERROR!!</h3>

        <p>Sorry we can\'t seem able to find what you are looking for</p>
        <p><a href="<?php echo home_url(); ?>">Click here to get back to the homepage</a>
        </p>
    </div>
    <!-- now the if statement is ended  -->
    <?php endif; ?>
    <?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
希望这有帮助

SO网友:erichmond

循环的正确迭代是:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
结束于此:

<?php endwhile; else : ?>
    <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
<?php endif; ?>
有关循环的更多详细信息,请参阅https://codex.wordpress.org/The_Loop

另外,我不确定您的$计数器变量在PHP中是否有任何意义。。。

$计数器+=1;

如果试图将1添加到$计数器变量,则应执行以下操作:

$计数器++;

这将使$计数器变量增加1

结束

相关推荐

Secondary loop doesn't work

嗨,我的模板页面中的二次循环有问题。php。我试图显示一些证明,检索这些证明的代码在我的索引中工作得非常好。php,但如果我从模板页面调用,我不会检索任何推荐信息。这里是我的代码:幻灯片。php<div class=\"jumbotron bg-black\"> <div class=\"row\"> <div class=\"container experience\"> <hr> <