正在检索自定义发布类型的列表

时间:2012-11-16 作者:Jessica

我使用以下两条代码来检索类别列表(第一条代码),然后在我的主题选项页面的选择菜单(第二条代码)中显示它们。我想知道如何更改此代码以执行相同的操作,但是,取而代之的是检索自定义帖子类型??

用于检索类别的代码

$tp_categories[0] = array(
\'value\' => 0,
\'label\' => \'\'
);
$tp_cats = get_categories(); $i = 1;
foreach( $tp_cats as $tp_cat ) :
$tp_categories[$tp_cat->cat_ID] = array(
    \'value\' => $tp_cat->cat_ID,
    \'label\' => $tp_cat->cat_name
);
$i++;
endforeach;
用于在下拉选择菜单中显示类别的代码

<tr valign="top"><th scope="row"><label for="choose_cat">Choose Category</label></th>
<td>
<select id="choose_cat" name="tp_options[choose_cat]">
<?php
foreach ( $tp_categories as $category ) :
    $label = $category[\'label\'];
    $selected = \'\';
    if ( $category[\'value\'] == $settings[\'choose_cat\'] )
        $selected = \'selected="selected"\';
    echo \'<option style="padding-right: 10px;" value="\' . esc_attr( $category[\'value\'] ) . \'" \' . $selected . \'>\' . $label . \'</option>\';
endforeach;
?>
</select>
</td>
</tr>

2 个回复
SO网友:Chip Bennett

这有一个核心功能:get_post_types():

<?php get_post_types( $args, $output, $operator ); ?>
因此,例如,要按名称返回公共、自定义帖子类型,请执行以下操作:

$custom_post_types = get_post_types( array(
    // Set to FALSE to return only custom post types
    \'_builtin\' => false,
    // Set to TRUE to return only public post types
    \'public\' => true
) );

SO网友:fuxia

使用get_post_types() 或全局变量$wp_post_types.

从…起wp-includes/post.php:

/**
 * Get a list of all registered post type objects.
 *
 * @package WordPress
 * @subpackage Post
 * @since 2.9.0
 * @uses $wp_post_types
 * @see register_post_type
 *
 * @param array|string $args An array of key => value arguments to match against the post type objects.
 * @param string $output The type of output to return, either post type \'names\' or \'objects\'. \'names\' is the default.
 * @param string $operator The logical operation to perform. \'or\' means only one element
 *  from the array needs to match; \'and\' means all elements must match. The default is \'and\'.
 * @return array A list of post type names or objects
 */
function get_post_types( $args = array(), $output = \'names\', $operator = \'and\' ) {
    global $wp_post_types;

    $field = (\'names\' == $output) ? \'name\' : false;

    return wp_filter_object_list($wp_post_types, $args, $operator, $field);
}

结束

相关推荐