简体   繁体   English

自定义循环未显示所有可用帖子

[英]Custom loop not displaying all available posts

I am having trouble with a custom Wordpress / ACF loop I am working on. 我正在使用自定义的Wordpress / ACF循环遇到麻烦。

The idea is that it displays the latest posts within the 'events' post type, hiding any posts where the event date has passed. 想法是,它在“事件”帖子类型中显示最新的帖子,隐藏事件日期已过的所有帖子。

The posts do hide if the date has passed. 如果日期已过,则帖子会隐藏。 However the loop is not displaying the full amount of posts available. 但是,循环并没有显示可用的帖子总数。 Currently with the loop below, it is only showing 6 out of the available 10. 当前在下面的循环中,它仅显示可用10中的6。

I have checked the reading settings in Wordpress and that's fine. 我已经检查了Wordpress中的阅读设置,这很好。

The code I am using for my loop is: 我用于循环的代码是:

<ul class="events-list">

<?php 
    $loop = new WP_Query( array( 
        'post_type' => 'events',
        'posts_per_page' => -1,
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_type' => 'DATE',
        'meta_key' => 'event-date'
    ));

    while ( $loop->have_posts() ) : $loop->the_post(); 

    $today = date('dmY');
    $expire = get_field('event-date');

    if( $expire > $today )

{ ?>

    <li>
        <h3><?php the_field('event-date'); ?> - <?php the_title(); ?></h3>
        <span class="time"><?php the_field('event-time'); ?></span>
        <?php the_field('event-details'); ?>
    </li>

<?php; } endwhile; wp_reset_query(); ?>

</ul>

If you're going to compare dates then you need to convert them to the appropriate types. 如果要比较日期,则需要将它们转换为适当的类型。 Convert them to Unix timestamp then you can easily compare when the date has surpassed. 将它们转换为Unix时间戳,然后您可以轻松地比较何时超过日期。 At the moment you are comparing which string is greater than the other which works sometimes but it's much more reliable to use Unix timestamp as your date formats always need to match. 目前,您正在比较哪个字符串大于另一个有时可以使用的字符串,但是使用Unix时间戳更为可靠,因为您需要始终匹配日期格式。

if(strtotime(get_field('event-date')) > date('U')) {
    //Your code here
}

Simply print compared dates before the "if" statement, and you will see where you made a mistake. 只需在“ if”语句之前打印比较的日期,您就会看到错误的地方。

echo $expire.'__'.$today.'<br>';
if( $expire > $today )

It can be because of invalid date format, empty $expire field etc.. Anyway, you will see what the reason is after implementing that print. 这可能是由于无效的日期格式,空的$ expire字段等引起的。无论如何,实现该打印后,您将看到原因是什么。

The solution to this problem was to change the loop to: 解决此问题的方法是将循环更改为:

<?php 

    $today = date('Ymd');

    $loop = new WP_Query( array( 
        'post_type' => 'events',
        'showposts' => 2,
        'meta_key' => 'event-date',  
        'meta_compare' => '>',  
        'meta_value' => date("Ymd"),
        'orderby' => 'meta_value_num',
        'order' => 'ASC'
    ));

    while ( $loop->have_posts() ) : $loop->the_post(); 

{ ?>

Post stuff here

<?php; } endwhile; wp_reset_query(); ?>

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

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