在模板中显示自定义帖子类型

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

我有一个自定义的帖子类型,叫做营地、事件和活动

我想在列表中显示它。

因此,当用户单击链接时,它将显示该自定义帖子类型下的所有帖子。

任何帮助都将不胜感激。

编辑

add_action(\'init\', \'activity_resources_register_post_type\');

function activity_resources_register_post_type() {
    register_post_type(\'activity_resources\', array(
        \'labels\' => array(
            \'name\' => \'Activity Resources\',
            \'singular_name\' => \'Activity Resources\',
            \'add_new\' => \'Add new Activity Resources\',
            \'edit_item\' => \'Edit Activity Resources\',
            \'new_item\' => \'New Activity Resources\',
            \'view_item\' => \'View Activity Resources\',
            \'search_items\' => \'Search Activity Resources\',
            \'not_found\' => \'No Activity Resources found\',
            \'not_found_in_trash\' => \'No Activity Resources found in Trash\'
        ),
        \'public\' => true,
        \'has_archive\' => \'activity_resources\',
        \'rewrite\' => array("slug" => "activity_resources"), // the slug for permalinks
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\')
    ));
}

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

创建自定义页面模板第一步是create a custom page template 保存代码。例如,命名文件template-cpt-list.php:

<?php
/**
 * Template Name: Custom Post Types List
 */

get_header();

// Custom code will go here

get_footer();

?>
创建生成的自定义帖子类型(CPT)列表下一步是生成您的CPT列表。这有一个核心功能: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
    ),
    // Set to "objects", so we have the full CPT object
    \'objects\'
 );
现在,您的CPT包含在一个具有CPT对象数组的变量中。

创建CPT存档索引永久链接列表,因此下一步是获取CPT对象,并使用它们创建每个CPT存档索引的永久链接列表。还有一个核心功能:get_post_type_archive_link():

<?php get_post_type_archive_link( $posttype ); ?>
因此,我们只需遍历post类型名称的数组,然后检索每个类型的permalink:

foreach ( $custom_post_types as $custom_post_type ) {
    $custom_post_type->permalink = get_post_type_archive_link( $custom_post_type->name );
}
然后,可以使用永久链接数组(URL)创建列表标记:

<ul>
<?php
foreach ( $custom_post_types as $custom_post_type ) {
    echo \'<li>\';
    echo \'<a href="\' . $custom_post_type->permalink . \'">\' . $custom_post_type->name . \'</a>\';
    echo \'</li>\';
}
?>
</ul>
把所有这些放在一起template-cpt-list.php 现在应该是这样:

<?php
/**
 * Template Name: Custom Post Types List
 */

get_header();

// Get list of CPTs
$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
    ),
    // Set to "objects", so we have the full CPT object
    \'objects\'
 );

// Add CPT permalinks to CPT objects
foreach ( $custom_post_types as $custom_post_type ) {
    $custom_post_type->permalink = get_post_type_archive_link( $custom_post_type->name );
}

// Output CPT archive index permalinks list
echo \'<ul>\';
foreach ( $custom_post_types as $custom_post_type ) {
    echo \'<li>\';
    echo \'<a href="\' . $custom_post_type->permalink . \'">\' . $custom_post_type->name . \'</a>\';
    echo \'</li>\';
}
echo \'</ul>\';

get_footer();

?>

SO网友:Rachel Baker

曼迪,

您可以创建一个新的页面模板(根据需要命名,我将其命名为“page activity resources.php”),该模板包含一个WP\\u查询,用于获取活动资源帖子并分页显示。

<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$cpt_query_args = array(
\'post_type\' => \'activity_resources\',
\'posts_per_page\' => 5,
\'paged\' => $paged
);
$cpt_query = new WP_Query( $cpt_query_args );
if ($cpt_query->have_posts()) : while ($cpt_query->have_posts()) : $cpt->the_post(); ?>
要列出所有自定义帖子类型,可以使用get\\u post\\u type生成列表:

    function your_custom_post_types() {
$args=array(
  \'_builtin\' => false
    );
    $cpt_types=get_post_types($args,\'names\');
    return $cpt_types;
}





   <?php $post_types = your_custom_post_types();
            foreach ($post_types as $post_type) {
    echo \'<li><a href="#">\'.$post_type.\'</a></li>\';
endforeach;

结束

相关推荐