我需要一些幫助來訂購我的歸檔頁面。我爲我的內容類型「activiteiten」製作了一個自定義的WordPress歸檔頁面,並將其命名爲:archive-activiteiten.php。並使用動作「pre_get_posts」來進行查詢。在兩個Querys中拆分一個WordPress帖子歸檔頁面
我的檔案模板有兩個查詢在它:
- 第一個在未來
- 第二個在過去
顯示事件我想顯示事件更改第一個查詢的順序。以便它顯示第一個即將到來的活動。
存檔 - activiteiten.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');
+1使用'pre_get_posts' – janw
我假設你想通過'get_field('activiteit_datum')'命令? 「get_field」函數來自WordPress的原生地址 – janw
@janw get_field函數是插件「高級自定義字段」中的一個函數,您可以在http://www.advancedcustomfields.com/上閱讀更多信息。你是對的,我喜歡通過meta_key命令「activiteit_datum」 –