从列表中排除自定义帖子类型

时间:2012-11-23 作者:mandy

 <?php 
    $custom_post_types = get_post_types( 
        array(
            \'_builtin\' => false,
            \'public\' => true
        ),
        \'objects\'
     );

    foreach ( $custom_post_types as $custom_post_type ) {
        $custom_post_type->permalink = get_post_type_archive_link( $custom_post_type->label );
    }

    echo \'<ul>\';
    foreach ( $custom_post_types as $custom_post_type ) {
        echo \'<li>\';
        echo \'<a href="\' . $custom_post_type->permalink . \'">\' . $custom_post_type->label . \'</a>\';
        echo \'</li>\';
    }
    echo \'</ul>\';
    ?>
我想从列表中排除产品和销售CPT。有人能帮忙吗?我还想按ACS顺序显示列表。

1 个回复
最合适的回答,由SO网友:Ralf912 整理而成
<?php
    $custom_post_types = get_post_types( 
        array(
                \'_builtin\' => false,
                \'public\' => true
            ),
            \'objects\'
        );

        ksort( $custom_post_types, SORT_ASC );

        echo \'<ul>\';

        foreach ( $custom_post_types as $custom_post_type ) {

            $exclude = array( \'products\', \'sales\' );

            if( TRUE === in_array( $custom_post_type->name, $exclude ) )
                continue;

            $custom_post_type->permalink = get_post_type_archive_link( $custom_post_type->name );

            echo \'<li>\';
            echo \'<a href="\' . $custom_post_type->permalink . \'">\' . $custom_post_type->label . \'</a>\';
            echo \'</li>\';

        }

        echo \'</ul>\';
结束

相关推荐