显示每个可用定制帖子类型的第一个帖子的循环?

时间:2012-07-08 作者:Squrler

是否可以创建一个循环,遍历所有可用的自定义帖子类型,而不静态定义这些自定义帖子类型,然后为每个自定义帖子类型显示第一篇帖子的特征图像?

2 个回复
最合适的回答,由SO网友:Pippin 整理而成

这应该适合您:

// grab all public post types
$post_types = get_post_types( array(\'public\' => true), \'names\' );

// loops through each post type
foreach( $post_types as $type ) {

    // setup the query
    $query_args = array(
        \'post_type\' => $type,
        \'posts_per_page\' => 1
    );

    // perform the query
    $items = get_posts( $query_args );

    // check if we have found anything
    if( $items ) {

        // loop through the items
        foreach( $items as $item ) {

            // show the post thumbnail
            echo get_the_post_thumbnail( $item->ID, \'thumbnail\' );

        }

    }

}

SO网友:kaiser

作为对@Pippin答案的补充:

甚至可以使用一个语法略有不同的查询。

// Get all post types
$post_types = get_post_types( 
     array( 
         \'public\'       => true
         // Avoid attachments - those aren\'t shown in the admin menu
        ,\'show_in_menu\' => true 
     )
    ,\'names\'
);

// Get rid of unwanted post types
// Use the array to add your unwanted post types
foreach ( array( \'some_unwanted_post_type\', \'another_one\' ) as $not_me )
    in_array( $not_me, $post_types ) AND unset( $post_types[ $not_me ] );

// Group by filter
add_filter( \'posts_groupby\', \'wpse57806_posts_groupby\', 20 );

// Query posts - Two options for post_types
$items = get_posts( array(
     \'post_type\'      => $post_types
    // ... or ...
    # \'post_type\'       => \'any\' // every post type, but not attachments
    ,\'posts_per_page\' => count( $post_types )
) );

// Call the thumbnail
foreach ( $items as $item )
    echo get_the_post_thumbnail( $item->ID, \'thumbnail\' );
编辑您需要一个小的回调函数来进行编辑:

function wpse57806_posts_groupby()
{
    // only needed once - better kept in here
    remove_filter( current_filter(), __FUNCTION__ );

    return \'post_type\';
}

结束

相关推荐

显示每个可用定制帖子类型的第一个帖子的循环? - 小码农CODE - 行之有效找到问题解决它

显示每个可用定制帖子类型的第一个帖子的循环?

时间:2012-07-08 作者:Squrler

是否可以创建一个循环,遍历所有可用的自定义帖子类型,而不静态定义这些自定义帖子类型,然后为每个自定义帖子类型显示第一篇帖子的特征图像?

2 个回复
最合适的回答,由SO网友:Pippin 整理而成

这应该适合您:

// grab all public post types
$post_types = get_post_types( array(\'public\' => true), \'names\' );

// loops through each post type
foreach( $post_types as $type ) {

    // setup the query
    $query_args = array(
        \'post_type\' => $type,
        \'posts_per_page\' => 1
    );

    // perform the query
    $items = get_posts( $query_args );

    // check if we have found anything
    if( $items ) {

        // loop through the items
        foreach( $items as $item ) {

            // show the post thumbnail
            echo get_the_post_thumbnail( $item->ID, \'thumbnail\' );

        }

    }

}

SO网友:kaiser

作为对@Pippin答案的补充:

甚至可以使用一个语法略有不同的查询。

// Get all post types
$post_types = get_post_types( 
     array( 
         \'public\'       => true
         // Avoid attachments - those aren\'t shown in the admin menu
        ,\'show_in_menu\' => true 
     )
    ,\'names\'
);

// Get rid of unwanted post types
// Use the array to add your unwanted post types
foreach ( array( \'some_unwanted_post_type\', \'another_one\' ) as $not_me )
    in_array( $not_me, $post_types ) AND unset( $post_types[ $not_me ] );

// Group by filter
add_filter( \'posts_groupby\', \'wpse57806_posts_groupby\', 20 );

// Query posts - Two options for post_types
$items = get_posts( array(
     \'post_type\'      => $post_types
    // ... or ...
    # \'post_type\'       => \'any\' // every post type, but not attachments
    ,\'posts_per_page\' => count( $post_types )
) );

// Call the thumbnail
foreach ( $items as $item )
    echo get_the_post_thumbnail( $item->ID, \'thumbnail\' );
编辑您需要一个小的回调函数来进行编辑:

function wpse57806_posts_groupby()
{
    // only needed once - better kept in here
    remove_filter( current_filter(), __FUNCTION__ );

    return \'post_type\';
}

相关推荐