带有 Genesis 框架的主页上的 Wordpress 自定义 Post 类型 (CPT) 存档网格

Wordpress Custom Post Type (CPT) Archive Grid on Homepage with Genesis Framework

我正在尝试让自定义 post 类型的网格显示在我的主页上。我在 WordPress 中使用 genesis 框架。我的 CPT 存档网格在存档页面上正常工作,但当我在我的主页上使用代码时它不显示任何 post。

<?php
/*** This file handles the output on the homepage.*/
/***************** Start Project Filter  ********************/
// Force full width content (optional)
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
//remove standard loop (optional)
remove_action( 'genesis_loop', 'genesis_do_loop' );
// Add our custom loop
add_action( 'genesis_loop', 'lp_filterable_portfolio' );
// Enqueue javascript
wp_enqueue_script('isotope', get_stylesheet_directory_uri() . '/js/isotope.pkgd.min.js', array('jquery'), '1.5.25', true);
wp_enqueue_script('isotope_init', get_stylesheet_directory_uri() . '/js/isotopes_init.js', array('isotope'), '', true);


/*** Get Excerpt. */
function the_excerpt_max_charlength($charlength) {
 $excerpt = get_the_excerpt();
 $charlength++;
 if ( mb_strlen( $excerpt ) > $charlength ) {
 $subex = mb_substr( $excerpt, 0, $charlength - 5 );
 $exwords = explode( ' ', $subex );
 $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
 if ( $excut < 0 ) {
 echo mb_substr( $subex, 0, $excut );
 } else {
 echo $subex;
 }
 echo '[...]';
 } else {
 echo $excerpt;
 }
}
/**
* Output filterable items. */
function lp_filterable_portfolio( ){

 $args = array(
 'post_per_page' => 9999
 );
 $loop = new WP_Query( $args );
 $terms = get_terms( 'my_taxonomy' );
 $count=0;
 ?>
<div class="archive-description">
 <?php if( $terms ) { ?>
 <ul id="portfolio-cats" class="filter clearfix">
 <li><a href="#" class="active" data-filter="*"><span><?php _e('All', 'lp'); ?></span></a></li>
 <?php
 foreach( $terms as $term ){
 echo "<li><a href='#' data-filter='.$term->slug'><span>$term->name</span></a></li>";
 }
 ?>
 </ul><!-- /portfolio-cats --><br/><br/>
 <?php } ?>
 <?php if( have_posts() ) { ?>
 <div id="portfolio-wrap" class="clearfix filterable-portfolio">
 <div class="portfolio-content">
 <?php while( have_posts() ): the_post(); ?>
 <?php $count++; ?>
 <?php $terms = get_the_terms( get_the_ID(), 'my_taxonomy' ); ?>
 <?php if ( has_post_thumbnail($post->ID) ) { ?>
 <article class="portfolio-item col-<?php echo $count; ?> <?php if( $terms ) foreach ( $terms as $term ) { echo $term->slug .' '; }; ?>">
 <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php echo genesis_get_image( array( size => 'lp-portfolio' ) ); ?>
 <div class="portfolio-overlay">
 <h3><?php the_title(); ?></h3>
 <p><?php the_excerpt_max_charlength(50);?></p>
 </div><!-- overlay --></a>
 </article>
 <?php } ?>
 <?php endwhile; ?>
 </div><!-- /themes-content -->
 </div><!-- /themes-wrap -->
 <?php } ?>
 <?php wp_reset_postdata(); ?>
</div>
<?php
 wp_reset_postdata();
}

genesis();

如有任何帮助,我们将不胜感激。

谢谢

回答我自己的问题... MUHAHAHAHAHA

通过将 'post_type' => 'projects' 添加到我的 $args = arrray 并将 while( have_posts() ): the_post(); 更改为 while ( $loop->have_posts() ): $loop->the_post();,如 WordPress Post Types 页面上的 "Querying by Post Type" 部分所述能够在主页上获得所有自定义 post 类型的拉取和过滤。

希望这对其他想回答同样问题的人有所帮助。

完整的工作代码:

<?php
/**
 * Home
 */
// Force full width content (optional)
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
//remove standard loop (optional)
remove_action( 'genesis_loop', 'genesis_do_loop' );
// Add our custom loop
add_action( 'genesis_loop', 'lp_filterable_portfolio' );
// Enqueue javascript
wp_enqueue_script( 'isotope', get_stylesheet_directory_uri() . '/js/isotope.pkgd.min.js', array( 'jquery' ), '1.5.25', true );
wp_enqueue_script( 'isotope_init', get_stylesheet_directory_uri() . '/js/isotopes_init.js', array( 'isotope' ), '', true );
/**
 * Get Excerpt.
 *
 * @since 1.0
 *
 */
function the_excerpt_max_charlength( $charlength ) {
    $excerpt = get_the_excerpt();
    $charlength++;
    if ( mb_strlen( $excerpt ) > $charlength ) {
        $subex = mb_substr( $excerpt, 0, $charlength - 5 );
        $exwords = explode( ' ', $subex );
        $excut = -( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
        if ( $excut < 0 ) {
            echo mb_substr( $subex, 0, $excut );
        } else {
            echo $subex;
        }
        echo '[...]';
    } else {
        echo $excerpt;
    }
}
/**
 * Output filterable portfolio items.
 *
 * @since 1.0
 *
 */
function lp_filterable_portfolio() {
    $args = array(
        'post_type' => 'your_post_type', //Add your custom post type
        'post_per_page' => 9999
    );
    $loop = new WP_Query( $args );
    $terms = get_terms( 'your_taxonomy' ); // Add your custom taxonomy 
    $count = 0;
    ?>
    <div class="archive-description">
        <?php if( $terms ) { ?>
        <ul id="portfolio-cats" class="filter clearfix">
            <li><a href="#" class="active" data-filter="*"><span><?php _e('All', 'lp'); ?></span></a>
            </li>
            <?php
            foreach ( $terms as $term ) {
                echo "<li><a href='#' data-filter='.$term->slug'><span>$term->name</span></a></li>";
            }
            ?>
        </ul>
        <!-- /portfolio-cats --><br/><br/>
        <?php } ?>
        <?php if( have_posts() ) { ?>
        <div id="portfolio-wrap" class="clearfix filterable-portfolio">
            <div class="portfolio-content">
                <?php while ( $loop->have_posts() ): $loop->the_post(); ?>
                <?php $count++; ?>
                <?php $terms = get_the_terms( get_the_ID(), 'your_taxonomy' ); // Add your custom taxonomy ?> 
                <?php if ( has_post_thumbnail($post->ID) ) { ?>
                <article class="portfolio-item col-<?php echo $count; ?> <?php if( $terms ) foreach ( $terms as $term ) { echo $term->slug .' '; }; ?>">
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                        <?php echo genesis_get_image( array( size => 'lp-portfolio' ) ); ?>
                        <div class="portfolio-overlay">
                            <h3>
                                <?php the_title(); ?>
                            </h3>
                            <p>
                                <?php the_excerpt_max_charlength(50);?>
                            </p>
                        </div>
                        <!-- overlay -->
                    </a>
                </article>
                <?php } ?>
                <?php endwhile; ?>
            </div>
            <!-- /themes-content -->
        </div>
        <!-- /themes-wrap -->
        <?php } ?>
        <?php wp_reset_postdata(); ?>
    </div>
    <?php
    wp_reset_postdata();


}
genesis();