Genesis Grid Classes用于自定义帖子类型模板页面

时间:2015-07-28 17:42:50

标签: custom-post-type genesis

我创建了一个名为events的自定义帖子类型。然后我创建了模板页面以列出所有事件。我试图在第一个'的网格循环中列出它们。和#三分之一'这样我就可以使用gensis内置的网格类。

然而,我所尝试过的所有内容都没有把这个课程放在第一个'在第一个元素上。我只会放置三分之一的'所有帖子上的课程

我尝试使用更简单的网格循环形式bill erickson http://www.billerickson.net/a-better-and-easier-grid-loop/,但这只适用于除我的自定义帖子类型之外的所有内容。 Alos尝试了我找到的其他方法。

我尝试过的模板页面和方法如下。感谢您提供的任何方向,因为我在这一点上难以接受。感谢

尝试和失败

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

在事件模板的循环中对此进行了尝试

$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();

1 个答案:

答案 0 :(得分:0)

经过多次试验和错误以及研究后,我在wordpress forum

上找到了答案,稍作修改

确保在while循环内部发布

$postcount++;
$new_class = ( ($postcount % 3) == 1 ) ? "first one-third" : "one-third";

//Then add the actual post structure
<article <?php post_class($new_class) ?> id="post-<?php the_ID();?>"  itemscope="" itemtype="http://schema.org/Event">
    //Add whatever else you need in here
</article>

希望可以帮助别人。