当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Wordpress loop "posts_per_page=4" shows only 1 post

问题描述:

I have a loop where I want to check

  1. if there is a custom field checked (checkbox) and

  2. if there is content in the loop that follows.

Now I don't understand why the loop always shows only one post:

<?php

//check if field has content

if(get_field("news_kategorie")): ?>

<ul class="page-articles-related">

<?php

$temp = $wp_query;

$wp_query= null;

$wp_query = new WP_Query();

$wp_query->query('posts_per_page=6&orderby=date&category_name='.implode (',',get_field('news_kategorie'))); ?>

<?php

//check if loop has content

if ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<li class="relativ">

<?php the_title(); ?>

</li>

<?php else: { ?>

<?php echo "There is nothing ..."; ?>

<?php } ?>

<?php endif; ?>

</ul>

<?php endif; ?>

Everything works except posts_per_page=6, I see always one post but I want to show six posts.

网友答案:

Your current code only prints one post, this should print all six posts:

if ($wp_query->have_posts()) : $wp_query->the_post();
    while ( have_posts() ) 
    {
        ?>
        <li class="relativ">
        <?php the_title(); ?>
        </li>  

    <?php
    } // end while

In your title you said you want 4 posts per page, but in the actual question you want 6, which is it?

分享给朋友:
您可能感兴趣的文章:
随机阅读: