首页 > 解决方案 > 在页面的日期排序列表中显示自定义帖子类型的自定义字段

问题描述

我正在为此苦苦挣扎。我添加了多个自定义字段值:在这种情况下,事件日期及其标题和位置到我的所有项目(自定义帖子类型),并希望将它们一起显示在名为日历的页面上的表格中。我尝试查询帖子,但自定义字段将一起显示为组,而不是按日期排序的列表。

在这种情况下,事件标题与帖子标题匹配。

我的问题是:最好的解决方案是什么?将所有事件日期创建为帖子中的自定义字段,然后将它们全部显示在一个称为日历的页面上,或者在日历页面中创建它们,然后过滤标题上的那些以仅在​​单个帖子中显示匹配的相关日期页面标题?

事件名称取自一个帖子对象,这似乎是所有事件都在同一页面上创建的场景的最佳解决方案,但否则我可能只使用帖子标题。

希望有人帮忙!

我的日历代码(即将到来的日期部分):



<div class="Rtable upcoming  Rtable--collapse">


    <?php $outs = array(); if( have_rows('events') ):  ?>
    
    
    <?php while ( have_rows('events') ) : the_row();  ob_start();

                                            // Get sub field values.
                                            $date = get_sub_field('date');    
                                            $location = get_sub_field('location');
                                            $website = get_sub_field('website');
                                            $title = get_sub_field('title');
                                            $premiere = get_sub_field('premiere');
                                            $event = get_sub_field('date', false, false);
                                            $today = strtotime(date('Ymd'));
                                            $upcoming = strtotime($event);
                                            $datefin = new DateTime($event); ?>
 
<?php if ($upcoming >=$today): ?>
    
    
    
    
    <div class="Rtable-cell Rtable-cell--head <?php if( get_sub_field('premiere') == 'yes' ): ?>premiere <?php endif; ?>">

        <?php if( get_sub_field('date') ): ?><?php the_sub_field('date'); ?><?php endif; ?>

    </div>



    <div class="Rtable-cell table-project-title"><?php if( get_sub_field('premiere') == 'yes' ): ?><span style="color: var(--magenta);">premiere:</span> <?php endif; ?>
    
        <?php if( $title ): ?><?php foreach( $title as $post ): 

                                                              // Setup this post for WP functions (variable must be named $post).
                                                              setup_postdata($post); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php endforeach; ?> <?php 
                                                          // Reset the global post object so that the rest of the page works correctly.
                                                          wp_reset_postdata(); ?> <?php endif; ?>
    
    
    </div>
    
    
    <div class="Rtable-cell"><?php if( get_sub_field('location') ): ?><?php the_sub_field('location'); ?><?php endif; ?></div>
    <div class="Rtable-cell Rtable-cell--foot"><?php if( get_sub_field('website') ): ?><a class="button" href="<?php echo esc_url( $website ); ?>" target="_blank">Tickets</a> <?php endif; ?></div>


  
   <?php endif; ?>

    <?php $outs[] = ob_get_clean(); endwhile; ?>


    <?php
        else :
        endif;
        $outs = array_reverse($outs);
        echo implode($outs);
        ?>

</div><!-- UPCOMING END -->


标签: datecalendaradvanced-custom-fields

解决方案


我找到了解决方案,按自定义字段日期查询。结果在这里:

<?php
/**
* Template Name: Calendar

*/

get_header();
?>

<?php get_header();
?>

<div class="content-header yellow">

    <h1><?php the_title();
?></h1>

</div>

<!-- Kalender -->

<div class="yellow content">

    <div class="Rtable upcoming  Rtable--collapse">

        <?php

// Create an empty array for storage
$post_data = array();

// Set arguments for your query
$args = array(
    'post_type'         => 'projects',
    'posts_per_page'    => -1
);

// Do our query with any arguments from above
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // Setup our repeater
        if ( have_rows( 'events' ) ):
        while ( have_rows( 'events' ) ) : the_row();
        // Optional - Only get posts that match a true false within our repeater

        $event = get_sub_field( 'date', false, false );
        $today = strtotime( date( 'Ymd' ) );
        $upcoming = strtotime( $event );
        $datefin = new DateTime( $event );
     
        

        if ( $upcoming >= $today ) {

            // Build our array with the data we need using key => value
            $post_data[] = array(
                'url' => get_the_permalink(),
                'title' => get_the_title(),
                'date'  => get_sub_field( 'date' ),
                'location'  => get_sub_field( 'location' ),
                'website'  => get_sub_field( 'website' ),

            );

        }
        endwhile;
        endif;
    }
} ?>



        <?php // Custom function to use in our sort to list the items by date

function subval_sort( $a, $b ) {
    if ( $a['date'] == $b['date'] )
    return 0;
    return $a['date'] < $b['date'] ? -1 : 1;
}

// Sort our multidimensional array by sub array value
usort( $post_data, 'subval_sort' );

// We can now work our data normally through easy to access methods
foreach ( $post_data as $post ) {
    ?>

        <div class="Rtable-cell Rtable-cell--head <?php if( get_sub_field('premiere') == 'yes' ): ?>premiere <?php endif; ?>">

            <?php   $format_in = 'm/d/Y';
    // the format your value is saved in ( set in the field options )
    $format_out = 'd/m/Y';
    // the format you want to end up with

    $date = DateTime::createFromFormat( $format_in, $post['date'] );

    echo $date->format( $format_out ) . '&nbsp; ' ;
    ?>

        </div>

        <div class="Rtable-cell table-project-title"><?php if ( get_sub_field( 'premiere' ) == 'yes' ): ?><span style="color: var(--magenta);">premiere:</span> <?php endif;
    ?>

            <a href="<?php echo $post['url']; ?> ">

                <?php
    echo $post['title'] . '&nbsp; ' ;
    ?>

            </a>
        </div>

        <div class="Rtable-cell">

            <?php echo $post['location'] . '&nbsp; ' ;
    
    ?>

        </div>

        <div class="Rtable-cell Rtable-cell--foot">

            <a class="button" href="<?php
    echo $post['website']; ?> ">

                tickets
            </a>
        </div>

        <?php }
    ?>


    </div>


</div>

<?php get_footer();
    ?>

推荐阅读