2

ここで、アーカイブ ページを注文する際に少し助けが必要です。コンテンツ タイプ「activiteiten」のカスタム WordPress アーカイブ ページを作成し、「archive-activiteiten.php」という名前を付けました。アクション「pre_get_posts」を使用して、クエリを作成しました。

私のアーカイブ テンプレートには、次の 2 つのクエリがあります。

  1. 将来のイベントを表示する最初のもの
  2. 過去のイベントを表示する 2 つ目

最初のクエリの順序を変更したいと思います。最初の今後のイベントが一番上に表示されるようにします。

archive-activiteen.php コード:

    <?php
/**
 * The template for displaying Archive pages.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package Simon van der Aa
 * @since Simon van der Aa 1.0
 */

get_header(); ?>

<div class="container main">
        <div class="row">
            <div class="span8">
                <section class="komende-activiteiten">
                    <h1 class="page-title">Activiteiten</h1>
                    <?php 
                    while ( have_posts() ) : the_post(); $eventdate = DateTime::createFromFormat('Ymd', get_field('activiteit_datum')); if($eventdate->format('Ymd') >= date('Ymd')) : ?>
                    <article class="agenda-item">
                        <h2><a href="<?php echo get_permalink() ?>" title="<?php echo get_the_title() ?>"><?php echo get_the_title() ?></a></h2>
                        <?php if ( has_post_thumbnail() ) :?>
                            <a href="<?php echo get_permalink(); ?>" title="<?php echo get_the_title(); ?>">
                            <figure class="article-image">
                            <?php the_post_thumbnail('besturen-thumb', array('class' => 'img-rounded')); ?>
                            <?php the_post_thumbnail_caption(); ?>
                            </figure>
                            </a>
                        <?php endif; if ( get_field('activiteit_datum', '') ) : $date = DateTime::createFromFormat('Ymd', get_field('activiteit_datum')); ?>
                            <div class="activiteit-datum">
                                <span class="dag"><?php echo $date->format('d');?></span>
                                <span class="maand"><?php echo $date->format('F');?></span>
                            </div>
                        <?php endif; echo the_excerpt(); if ( get_field('activiteit_inschrijfformulier', '') ) :?><a href="<?php echo get_permalink(); ?>#inschrijven" class="btn">Inschrijven voor de activiteit</a><?php endif; ?>
                    </article>
                    <?php endif; endwhile; ?>
                </section><!-- komende-activiteiten -->
                <section class="geweest-activiteiten">
                    <h2>Geweest</h2>
                    <table class="table table-striped agenda-archief">
                        <thead>
                            <tr>
                                <th>Datum</th>
                                <th>Activiteit</th>
                             </tr>
                        </thead>
                        <tbody>           
                    <?php while ( have_posts() ) : the_post(); $eventdate = DateTime::createFromFormat('Ymd', get_field('activiteit_datum')); if($eventdate->format('Ymd') < date('Ymd')) : ?>
                        <tr>
                          <td><?php echo $eventdate->format('d-m-Y') ?></td>
                          <td><a href="<?php echo get_permalink(); ?>" title="<?php echo get_the_title(); ?>"><?php echo get_the_title(); ?></a></td>
                        </tr>
                        <?php endif; endwhile;?>
                        </tbody>
                    </table>
                </section><!-- geweest-activiteiten -->
                <?php simonvanderaa_content_nav( 'nav-below' ); ?>
            </div><!--/ span8 -->
            <div class="span4">
                <?php get_sidebar(); ?>
            </div><!--/ span4 -->
        </div><!--/ row -->
    </div><!--/ container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

そして、私の functions.php では、次の pre_get_posts アクションを使用します

function my_post_queries( $query ) {
  // do not alter the query on wp-admin pages and only alter it if it's the main query
  if (!is_admin() && $query->is_main_query()){

    // alter the query for the home and category pages 

    if(is_home()){
      $query->set('posts_per_page', 3);
    }

   if ( is_post_type_archive('commissie') ){
           $query->query_vars['posts_per_page'] = 5;
           $query->query_vars['order'] = 'desc';
           return;
       }
   if ( is_post_type_archive('nieuws') ){
           $query->query_vars['posts_per_page'] = 10;
           $query->query_vars['order'] = 'desc';
           return;
       }
   if ( is_post_type_archive('besturen') ){
           $query->query_vars['posts_per_page'] = 1;
           $query->query_vars['order'] = 'desc';
           return;
       }
   if ( is_post_type_archive('faqs') ){
           $query->query_vars['posts_per_page'] = 99;
           $query->query_vars['order'] = 'asc';
           return;
       }

   if ( is_post_type_archive('activiteiten') ){
           $query->query_vars['posts_per_page'] = 10;
           $query->query_vars['orderby'] = 'meta_value';
           $query->query_vars['meta_key'] = 'activiteit_datum';
           return;
       }
  }
}
add_action( 'pre_get_posts', 'my_post_queries' );
4

1 に答える 1