每隔5个Wordpress帖子后插入一个Div?

时间:2016-11-23 01:29:13

标签: wordpress

如何在主页上每五个帖子后插入一个div?

这是我的循环:

DELIMITER $
CREATE PROCEDURE depth(IN n INT, OUT depth INT)
BEGIN
    DECLARE parent INT DEFAULT 0;
    SET max_sp_recursion_depth=255;
    SELECT parent_id INTO parent FROM t WHERE id=n;
    IF parent = 0 THEN SET depth = 1;
    ELSE CALL depth(parent,depth); SET depth = depth + 1;
    END IF;
END $
DELIMITER ;

CALL(6,@depth);
SELECT @depth;

如何在循环中插入此代码以使div在每5个帖子后出现?

WITH RECURSIVE tree(id,parent_id,depth) AS 
  ( 
    SELECT id, parent_id, 1 from t WHERE parent_id=0 
    UNION ALL 
    SELECT t.id, t.parent_id, depth+1 FROM t JOIN tree ON tree.id = t.parent_id
  ) SELECT * FROM tree WHERE id = 6;

1 个答案:

答案 0 :(得分:0)

在while之前,您可以声明值为0的变量,并在while内增加此变量。例如:

    $counter = 0;

    while( have_posts() ):
    // codes
    $counter ++;
    if ($counter >= 5) {
    echo '<div>My div!</div>';
    $counter = 0; // clean to count again
    }
    endwhile;

代码将是:

<?php
            if ( have_posts() ) :

                  if ( is_home() && ! is_front_page() ) : ?>
                    <header>
                        <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
                    </header>


                  <?php
                endif;

$counter = 0;

                /* Start the Loop */
                while ( have_posts() ) : the_post();

                    /*
                     * Include the Post-Format-specific template for the content.
                     * If you want to override this in a child theme, then include a file
                     * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                     */
                    get_template_part( 'template-parts/content', get_post_format() );

    $counter ++;
    if ($counter >= 5) {
    echo '<div>My div!</div>';
    $counter = 0; // clean to count again
    }

                endwhile;

                the_posts_navigation();

            else :

                get_template_part( 'template-parts/content', 'none' );

                  endif; 


        ?>