我创建了一个名为事件的自定义帖子类型。然后我创建了模板页面来列出所有事件。我试图用“第一”和“三分之一”类在网格循环中列出它们,以便我可以使用网格类中内置的 gensis。
但是,我尝试过的所有事情都没有将类“第一”放在第一个元素上。我只会在所有帖子上放置“三分之一”类
我曾尝试使用更简单的网格循环表单比尔埃里克森http://www.billerickson.net/a-better-and-easier-grid-loop/但这仅适用于除我的自定义帖子类型之外的所有内容。阿洛斯尝试了我发现的其他方法。
我尝试过的模板页面和方法如下。感谢您提供的任何指导,因为我在这一点上很难过,如下所示。谢谢
尝试和失败
function be_archive_post_class( $classes ) {
global $wp_query;
if( ! $wp_query->is_main_query() )
return $classes;
if (is_page('events')){
$classes[] = 'one-third';
if( 0 == $wp_query->current_post || 0 == $wp_query->current_post % 3 )
$classes[] = 'first';
return $classes;
}
}
add_filter( 'post_class', 'be_archive_post_class' );
//I also Added the if(is_page('events')){} to the above
还在事件模板上的我的While循环中尝试了这种变化
$c = 0;
while (have_posts()) : the_post();
if( $c % 3 == 0 ){
$style = 'one-third';
}
else{
$style = 'first';
}
$c++;
}
<?php post_class($style) ?>
活动模板页面
<?php
/**
* Template Name: Events Template
* Description: Used as a CPT Archive for our Events Custom Post Type
*
*/
/** Replace the standard loop with our custom loop */
// Do the Custom Loop
remove_action('genesis_loop', 'genesis_do_loop');
add_action( 'genesis_loop', 'ame_custom_loop' );
//Comparing the end date with the timestamp so that old events don't show
function ame_custom_loop(){
$meta_quer_args = array(
'relation' => 'AND',
array(
'key' => 'event-end-date',
'value' => time(),
'compare' => '>='
)
);
//Define Our Arguments
$query_args = array(
'post_type' => 'event',
'posts_per_page' => $instance['number_events'],
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'meta_key' => 'event-start-date',
'orderby' => 'meta_key',
'order' => 'ASC',
'meta_query' => $meta_quer_args
);
$upcoming_events = new WP_Query( $query_args );
while($upcoming_events->have_posts() ): $upcoming_events->the_post();
$event_start_date = get_post_meta( get_the_ID(), 'event-start-date', true );
$event_end_date = get_post_meta( get_the_ID(), 'event-end-date', true );
$event_street_address = get_post_meta( get_the_ID(), 'event-street-address', true );
$event_city = get_post_meta( get_the_ID(), 'event-city', true );
$event_postal_code = get_post_meta( get_the_ID(), 'event-postal-code', true );
$event_venue = get_post_meta( get_the_ID(), 'event-venue', true );
$event_start_time = get_post_meta( get_the_ID(), 'event-start-time', true );
$event_end_time = get_post_meta( get_the_ID(), 'event-end-time', true ); ?>
<article id="post-<?php the_ID();?>" <?php post_class() ?> itemscope="" itemtype="http://schema.org/Event">
<header class="entry-header">
<p class="entry-meta">
<time class="entry-time start-time" datetime="<?php echo date( 'F d, Y', $event_start_date );?>" itemprop="startDate"><?php echo date( 'F d, Y', $event_start_date ); ?></time>
<?php
if($event_end_date != NULL){ ?>
<time class="entry-time end-time" datetime="<?php echo date( 'F d, Y', $event_end_date );?>" itemprop="endDate"><?php echo date( 'F d, Y', $event_end_date ); ?></time>
<?php }
?>
</p>
<h1 class="entry-title" itemprop="headline"><a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title() ?></a></h1>
</header>
</article>
<?php endwhile;
}
genesis();