繁体   English   中英

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

[英]How to add any text box after every 5 latest posts in wordpress

我已经更新了 20 个 wordpress 主题的帖子。 我需要在每 5 个帖子后添加我自己的文本框。 这样我就可以在每 5 个帖子后添加 4 个文本框。 我认为它可以通过$post_counter完成,请任何人向我询问我的问题。

我的代码似乎只是,

<?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; ?>

我相信这就是你所需要的

<?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; ?>

我阅读了您的评论并更新了@zameerkhan 代码。

<?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; ?>

你永远不应该使用query_posts ,忘记它存在或曾经存在过。 使用WP_Querypre_get_posts

来自 CODEX ON query_posts

注意:此功能不适用于插件或主题。 正如稍后所解释的,有更好、更高性能的选项来更改主查询。 query_posts() 是通过用新的查询实例替换页面的主查询来修改页面的主查询的过于简单和有问题的方法

你的代码应该是这样的

$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; ?>

从你的代码

<?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; ?>

而不是<input type="text" name="mytext[]"/>我替换了<h3>This is First text box</h3>

所以,现在我的输出是,标题 1
标题 2
标题3
标题4
标题5

这是第一个文本框


标题 6
标题 7
标题 8
标题 9
标题 10

这是第一个文本框


标题 11
标题 12
标题 13
标题 14
标题 15

这是第一个文本框

...这里看到“这是第一个文本框”返回 4 次。 我的问题是,我需要在标题 10 之后文本应该显示为“这是第二个文本框”等等..我想现在很清楚

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM