php - 数组 - Orderby - 类型

标签 php html arrays wordpress types

设置为期四 (4) 天的计划概览:周三、周四、周五、周六。所有事件(电影、事件、研讨会、工作坊++)都列在post type event 中。

我将把查询分成每一天,这意味着在 args 中应该设置 date

最终结果应该是这样的:

Wednesday

11:00

<---- Events which starts at 11:00 ---->

12:00

<---- Events which starts at 12:00 ---->

下一页:

Thursday

11:00

<---- Events which starts at 11:00 ---->

12:00

<---- Events which starts at 12:00 ---->

等等。

我已经开始使用 PHP,但在我意识到我需要将 args 与使用 Types 制作的信息相结合时停了下来。

每个事件都有post type event 并定义了一个date。这不是撰写文章时的常规 WordPress 发布日期。这意味着 orderby date 将毫无用处。

摘录:

$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
);

$the_query = new WP_Query($args); 

注意:如前所述,现在它在 post date 之后排序,但这是不正确的,也不会起作用。

<?php if ( $the_query->have_posts() ) : ?> <!-- Listing all the events -->

<div id="isotope-list">

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 

<div class="col-md-4"> <!-- Each event should be in this div -->

<?php
$value = types_render_field('dato');
if ( !empty($value) ) { 
echo do_shortcode( '[types field="dato" format="g:i"][/types]' ); 
echo " |";
}
else { echo ""; }
?>

</div> <!-- End col-md-4  -->
<?php endwhile;  ?>
</div> <!-- End isotope-list -->
<?php endif; ?>

我认为这应该可以解释问题、我的想法、我需要帮助的地方以及我尝试过的方法。如果您有任何问题,请提出。

可在此处找到该页面:http://vekvemedia.no/oppdrag/dokfilm/ (滚动到程序)

抱歉,该网站是挪威语。翻译:

Onsdag - 周三,Torsdag - 周四,Fredag​​ - 周五,Lørdag - 周六。

已更新

$args = array(
    'post_type' => 'event',
    'posts_per_page' => -1,
    'meta_query'        => array(
           'key' => 'dato',
           'value' => date("Y-m-d H:i:s"),
           'compare' => '>=',
           'type' => 'date'
    ),
    'orderby' => 'meta_value', 
    'meta_key' => 'dato',
    'order' => 'DESC',
    );

更新 2

在大卫的帮助下

<?php
    $args = array(
        'post_type' => 'event',
        'posts_per_page' => -1,
    );

    $the_query = new WP_Query($args); 
?>

<?php $x= 0;

if ( $the_query->have_posts() ) : ?> <!-- Listing all the events -->

<div id="isotope-list">

    <!-- open the first day -->
    <div id="isotope-day">
        <div class="col-md-12">

        <?php while ( $the_query->have_posts() ) : $the_query->the_post();

            $newtime = false;
            $date = types_render_field('dato', array("format"=>"U"));//get a unix date, i'm not sure of the dateformat you are using but it should be ok..if not ref strtotime for your date format
            $now = strtotime('now');

            if($date < $now){
                continue;
            }

            //do we need a new day?? yes if not the same date!
            if( date('d/m/Y', $date) !== $curdate  || !isset($curdate) ){
                $curday = date('l', $date );
                $curdate= date('d/m/Y', $date);
                $newdate= true; 
                $x++;
            }

            //do we need a new time??
            if( date('H', $date ) != $curtime || !isset($curtime) ){
                $curtime = date('H', $date );
                $newtime= true; 
            }

        ?>

        <?php if($newday): ?>

        <?php if ($x>0):?>
        </div> <!-- End previous day when adding a new one except in first instance -->
    </div> <?php endif; ?>

    <div id="isotope-day">
        <div class="col-md-12">
            <h1><?php echo $curday.' '.$curdate; ?></h1>

            <?php endif; ?>
            <?php if($newtime) : ?>
            <h2><?php echo $curtime; ?>:00</h2>
            <?php endif; ?>

            <div class="col-md-4">
                <a href="#">
                    <div class="content grid lefttext maximg littluft">
                        <div class="red-overlay">
                            <figure>
                                <?php if ( has_post_thumbnail() ) { the_post_thumbnail('event_thumb'); } ?>
                                <figcaption class="absolute-bottom">
                                    <div class="littpaddingleft">
                                        <h2 class="whitetext nomarginbottom jubilat"><?php the_title(); ?></h2>
                                        <p><span class="whitetext thin">

                                        <?php
                                            $value = types_render_field('dato');
                                            if ( !empty($value) ) { 
                                            echo do_shortcode( '[types field="dato" format="G:i"][/types]' ); 
                                            echo " |";
                                            }
                                            else { echo ""; }
                                        ?>
                                        <?php
                                            $value = types_render_field('type');
                                            if ( !empty($value) ) { 
                                            echo $value;
                                            echo " |";
                                            }
                                            else { echo ""; }
                                        ?>
                                        <?php
                                            $value = types_render_field('stad');
                                            if ( !empty($value) ) { echo $value; }
                                            else { echo ""; }
                                        ?>

                                        </span></p>
                                    </div>
                                </figcaption>           
                            </figure>  
                        </div> 
                    </div>   
                </a> 
            </div> <!-- End col-md-4  -->                         


    <?php
        // increase our counter... 
        $x++;
        endwhile;  
    ?>

    </div> <!-- Close col-md-12 -->
    </div> <!-- close final day -->
</div> <!-- End isotope-list -->
<?php endif; ?>

遇到 $curdate 等问题...可以在此处找到该页面:http://vekvemedia.no/oppdrag/dokfilm/ (滚动到程序)

var_dump($posts) 回应了这个:

array(2) { [0]=> object(WP_Post)#130 (24) { ["ID"]=> int(26) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2016-01-23 14:07:20" ["post_date_gmt"]=> string(19) "2016-01-23 13:07:20" ["post_content"]=> string(56) "Verdas trivlegaste dokumentarfilmfestival fyllar 20 år!" ["post_title"]=> string(39) "20 år med Dokfilm – 27. til 30. mars" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(32) "20-ar-med-dokfilm-27-til-30-mars" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2016-01-23 14:07:20" ["post_modified_gmt"]=> string(19) "2016-01-23 13:07:20" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(42) "http://vekvemedia.no/oppdrag/dokfilm/?p=26" ["menu_order"]=> int(0) ["post_type"]=> string(4) "post" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [1]=> object(WP_Post)#113 (24) { ["ID"]=> int(1) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2016-01-23 13:13:22" ["post_date_gmt"]=> string(19) "2016-01-23 12:13:22" ["post_content"]=> string(101) "Velkommen til WordPress. Dette er ditt første innlegg. Rediger eller slett det, og start bloggingen!" ["post_title"]=> string(12) "Hei, verden!" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(10) "hei-verden" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2016-01-23 13:13:22" ["post_modified_gmt"]=> string(19) "2016-01-23 12:13:22" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(41) "http://vekvemedia.no/oppdrag/dokfilm/?p=1" ["menu_order"]=> int(0) ["post_type"]=> string(4) "post" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "1" ["filter"]=> string(3) "raw" } }

元键:wpcf-dato。元值:1458741600

最佳答案

不幸的是,日期元查询仅适用于(据推测)YYYY-mm-dd 存储日期,我之前在正确存储日期时遇到过问题,所以最好的选择是实际存储一个 unix 日期(包含数字),所以越大数字越大的日期。

但是因为您要返回所有帖子并且不使用分页功能,所以您可以不显示已过期的事件

像这样的东西应该可以工作:

<?php 
// your query args...
$args = array(
    'post_type' => 'event',
    'posts_per_page' => -1,
    'meta_query'        => array(
           'key' => 'dato',
           'value' => strtotime('now'),
           'compare' => '>=',
           'type'    => 'numeric',
    ),
    'orderby' => 'meta_value_num', 
    'meta_key' => 'wpcf-dato',
    'order' => 'ASC', // sort ascending, we want the nearest dates to display first?
    );


$the_query = new WP_Query($args);





$x= 0;

if ( $the_query->have_posts() ) : ?> <!-- Listing all the events -->

<div id="isotope-list">

    <!-- open the first day -->
    <div id="isotope-day">

    <?php while ( $the_query->have_posts() ) : $the_query->the_post();

        $newtime = false;
        $date= strtotime( get_post_meta($post->ID, 'dato', true) ); //get a unix date, i'm not sure of the dateformat you are using but it should be ok..if not ref strtotime for your date format
        $now = strtotime('now');

        if($date < $now){
            continue;
        }


        //do we need a new day?? yes if not the same date!
        if( !isset($curdate) || date('d/m/Y', $date) !== $curdate   ){
            $curday = date('l', $date );
            $curdate= date('d/m/Y', $date);
            $newdate= true; 
            $x++;
        }

        //do we need a new time??
        if( !isset($curtime) || date('h', $date ) != $curtime  ){
            $curtime = date('h', $date );
            $newtime= true; 
        }

    ?>

    <?php if($newday): ?>

        <?php if ($x>0):?>
            </div> <!-- End previous day when adding a new one except in first instance -->
        <?php endif; ?>

        <div id="isotope-day">
        <h1><?php echo $curday.' '.$curdate; ?></h1>

    <?php endif; ?>
        <?php if($newtime) : ?>
            <h3><?php echo $curtime; ?>:00</h3>
        <?php endif; ?>

        <!-- your event here -->
    </div> <!-- End col-md-4  -->

    <?php
        // increase our counter... 
        $x++;
        endwhile;  
    ?>

    </div> <!-- close final day -->

</div> <!-- End isotope-list -->
<?php endif; ?>

关于php - 数组 - Orderby - 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35961792/

相关文章:

PHP 5.3.5 PDO FETCH_OBJ 内存泄漏?

php - 将数据从mysql插入到phpexcel的数组中

javascript - javascript的执行优先级是多少?

html - 将精确宽度设置为 td- 文本溢出

php - 将 mysql 结果放入一个 php 数组中,像这样吗?

java - 从键盘获取输入时变量获取未知值

c - 定时数组静态数组分配与 C 中堆栈上的数组

javascript - 对特定表单名称运行 ajax 调用

php - 模拟 phpMyAdmin 导出功能

jquery - 使用 "src"属性更改延迟加载灰色背景图像