dongyuxiao6295
2014-04-14 12:21
浏览 46
已采纳

如何在wordpress中每5个帖子后添加任何文本框

I have updated 20 posts in wordpress theme. And i need to add my own text box after every 5 posts. So that i can add 4 text box after every 5 post. I think it can be done by $post_counter please any one give me a query for my question.

My code simply seems to,

<?php
query_posts( array('posts_per_page'=>20,orderby=>post_date, order=>desc) );
while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php endwhile; ?>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dtlc84438 2014-04-14 13:02
    已采纳

    I read your comment and i updated @zameerkhan code.

    <?php
    $query = new WP_Query( array('posts_per_page' => 20, orderby => post_date, order => desc) );
    $p = 1;
    while ( $query->have_posts() ) : $query->the_post();
    ?>
    <?php the_title(); ?>
    <?php the_post_thumbnail(); ?>
    //this will create text box after 5 post with name mytext1,mytext2 etc.
    <?php echo ($p%5 == 0) ? '<input type="text" name="mytext'.($p/5).'" />': "";
    $p++;
    ?>
    <?php endwhile; ?>
    
    打赏 评论
  • donljt2606 2014-04-14 12:33

    I believe this is what you need

    <?php
    $query = new WP_Query( array('posts_per_page' => 20, orderby => post_date, order => desc) );
    $p = 1;
    while ( $query->have_posts() ) : $query->the_post();
    ?>
    <?php the_title(); ?>
    <?php the_post_thumbnail(); ?>
    <?php echo ($p%5 == 0) ? '<input type="text" name="mytext[]" />' : "";
    $p++;
    ?>
    <?php endwhile; ?>
    
    打赏 评论
  • doulan8846 2014-04-14 13:06

    From your code

    <?php
    query_posts( array('posts_per_page'=>20,orderby=>post_date, order=>desc) );
    $p = 1;
    while ( have_posts() ) : the_post();
    ?>
    <?php the_title(); ?>
    <?php the_post_thumbnail(); ?>
    <?php echo ($p%5 == 0) ? '<input type="text" name="mytext[]" />' : "";
    $p++;
    ?>
    <?php endwhile; ?>
    

    Instead of <input type="text" name="mytext[]"/> i replace <h3>This is First text box</h3>

    So, now my output is, Title 1
    Title 2
    Title3
    Title4
    Title5

    This is First text box


    Title 6
    Title 7
    Title 8
    Title 9
    Title 10

    This is First text box


    Title 11
    Title 12
    Title 13
    Title 14
    Title 15

    This is First text box

    ... Here see that "This is First text box" returns 4 times. My question is, i need after Title 10 the text should display as "This is Second text box" and so on.. I think now its clear
    打赏 评论
  • duanbei2914 2014-04-14 19:37

    You should never use query_posts, forget that it exists or ever existed. Use WP_Query or pre_get_posts

    FROM THE CODEX ON query_posts

    Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query

    Your code should look like this

    $args = array(
            'order' => 'DESC',
            'posts_per_page'=>20,
            'orderby' => 'post_date',
        );
    
        $the_query = new WP_Query( $args );
    
        $p = 1;
        while ( $the_query->have_posts() ) : $the_query->the_post();
        ?>
        <?php the_title(); ?>
        <?php the_post_thumbnail(); ?>
        //this will create text box after 5 post with name mytext1,mytext2 etc.
        <?php echo ($p%5 == 0) ? '<input type="text" name="mytext'.($p/5).'" />': "";
        $p++;
        ?>
        <?php endwhile; ?>
    
    打赏 评论

相关推荐 更多相似问题