使用数组在归档页面上列出自定义帖子类型

时间:2012-11-08 作者:kristina childs

我刚刚开始关注自定义帖子类型,由于某些原因,无法在循环中显示任何自定义帖子类型。我在网上发现了一些查询,但它们都不在封闭的php语句中,我不知道如何集成它们。以下是我所拥有的:

// add custom post type
add_action( \'init\', \'create_post_type\' );
function create_post_type() {
    register_post_type( \'band_artists\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Artists\' ),
                \'singular_name\' => __( \'Artist\' )
            ),
        \'public\' => true,
        \'has_archive\' => true,
        \'supports\' => array( \'title\', \'editor\', \'comments\', \'excerpt\', \'custom-fields\', \'thumbnail\' ),
        \'rewrite\' => array(\'slug\' => \'artists\'),
        \'taxonomies\' => array(\'category\', \'post_tag\')
        )
    );
}
并使用随机化器进行查询。。。当这些只是普通的帖子(减去\'post_type\' 显然)。

<?php query_posts(array(
    \'post_type\' => \'band_artists\',
    \'showposts\' => 1,
    \'orderby\' => \'rand\',
    \'category\' => \'5\'
    ));
    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
?>

<div id="post-<?php the_ID(); ?>" class="artistSingle">

    <?php if (has_post_thumbnail( $post->ID ) ): ?>
    <div id="artistImgSingle"><?php the_post_thumbnail(\'medium\'); ?></div>
    <?php endif; ?>

    <p class="artistName"><?php the_title(); ?></p>
    <p>
        <?php if(get_post_meta($post->ID, \'band\', true)): ?>
            <strong>Band:</strong> <?php echo get_post_meta($post->ID, \'band\', true); ?><br />  
        <?php endif; ?>
        <?php if(get_post_meta($post->ID, \'gear\', true)): ?>
            <strong>Gear:</strong> <?php echo get_post_meta($post->ID, \'gear\', true); ?><br>
        <?php endif; ?> 
        <?php if(get_post_meta($post->ID, \'website\', true)): ?>
            <strong>Website:</strong> <a href="http://<?php echo get_post_meta($post->ID, \'website\', true); ?>" target="_blank"><?php echo get_post_meta($post->ID, \'website\', true); ?></a>
        <?php endif; ?>
    </p>
    <?php the_content(); ?>

    <div>
        <?php wp_link_pages( array( \'before\' => \'<div class="page-link"><span>\' . __( \'Pages:\', \'2012\' ) . \'</span>\', \'after\' => \'</div>\' ) ); ?>
    </div>

</div>

<?php endwhile; // end of featured large loop ?>
<?php else : ?>
<?php endif; ?>

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

在对自定义帖子类型进行了大量搜索和教育之后,我找到了答案。我还添加了一些东西,使规范更加细化。

// Add custom post type Artists 
function register_artists_post_type() {
    register_post_type(\'artists\',array(
        \'labels\' => array(
                \'name\' => __( \'Artists\' ),
                \'singular_name\' => __( \'Artist\' ),
                \'add_new\' => __( \'Add Artist\',\'Artist\' ),
                \'add_new_item\' => __( \'Add New Artist\' ),
                \'edit_item\' => __( \'Edit Artist\' ),
                \'new_item\' => __( \'New Artist\' ),
                \'view_item\' => __( \'View Artist\' ),
                \'search_items\' => __( \'Search Artists\' ),
                \'not_found\' => __( \'No Artists Found\' ),
                \'not_found_in_trash\' => __( \'No Artists In Trash\' ),
                \'parent_item_colon\' => \'\'
            ),
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'has_archive\' => true,
        \'supports\' => array( \'title\',\'editor\',\'excerpt\',\'custom-fields\',\'thumbnail\' ),
        \'rewrite\' => array(\'slug\' => \'artists\'),
        \'taxonomies\' => array(\'large_feature\',\'small_feature\'),
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
    ));
}
add_action(\'init\',\'register_artists_post_type\')
注册自定义分类法(对于新手来说,这是一种说类别的奇特方式……“类别”在普通帖子中只是默认的分类法)

// Add custom taxonomies for Artists post type
add_action(\'init\',\'register_large_feature_taxonomy\');
function register_large_feature_taxonomy() {
    register_taxonomy(\'large_feature\',array(\'artists\'),array(
        \'hierarchical\' => true, 
        \'label\' => \'Large Feature\', 
        \'public\'=> true,
        \'show_ui\'=> true,
        \'query_var\'=> true,
        \'rewrite\' => false,
    ));
}   
add_action(\'init\',\'register_small_feature_taxonomy\');
function register_small_feature_taxonomy() {
    register_taxonomy(\'small_feature\',array(\'artists\'),array(
        \'hierarchical\' => true, 
        \'label\' => \'Small Feature\', 
        \'public\'=> true,
        \'show_ui\'=> true,
        \'query_var\'=> true,
        \'rewrite\' => false,
    ));
}       
最后,查询,如archive-artists.php

<?php query_posts(array(
    \'large_feature\' => \'artists_page_large\', // this is the same as calling for a category within standard posts, where \'large_feature\' is category and \'artists_page_large\' is the ID/slug (either should work, I chose to use the slug to avoid going back to check ID numbers)
    \'showposts\' => 1,
    \'orderby\' => \'rand\', // randomizes the posts each page load
    ));
    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
?>

<div id="post-<?php the_ID(); ?>" class="artistSingle white">
    <!-- do stuff -->
</div>

<?php endwhile; endif; // end of featured large loop ?>



<div id="featArtistSm" class="white">

    <?php rewind_posts(); // start featured small loop ?> <!-- rewind allows for multiple loops on a single page -->
    <?php query_posts(array(
        \'small_feature\' => \'artists_page_small\',
        \'showposts\' => 4,
        \'orderby\' => \'rand\',
        ));
    ?>
    <?php if(have_posts()) : while (have_posts() ) : the_post(); ?>

    <div class="featArtistSmSng white">
        <-- do stuff -->
    </div>

    <?php endwhile; endif; // end of featured small loop ?>
</div>



<div id="featuredArtists">
  <div id="artistRoster" class="artistRoster">

    // query to pull all posts of \'artist\' custom post type
    <?php query_posts(\'post_type=artists&orderby=title&order=asc&posts_per_page=-1\'); ?>
        <?php if(have_posts()) : while (have_posts() ) : the_post(); ?>

        <p class="roster"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>

    <?php endwhile; endif; ?>

  </div>
</div>
希望这对其他人有所帮助!因为我最初有一个类别叫做“带鼻涕虫的艺术家”artists, 我最终删除了所有类别,并将slug用于自定义帖子类型。它更干净,消除了混淆系统的可能性。原来我还有一个名为“艺术家”的页面,其中有一个模板页面被我删除了,但该页面仍然是垃圾。确保从垃圾箱中删除任何冲突的项目!即使它在垃圾桶中,它仍然在你的数据库中。

SO网友:totels
结束

相关推荐

如何使用jQuery AJAX导入/上传文件?

我一直遇到在AJAX中添加文件的问题,以便服务器AJAX功能可以处理数据。如何使$\\u文件的传递与默认操作(内置到表单元素中)相同?HTML Form<form id=\"frmImport\" name=\"frmImport\" method=\"post\" enctype=\"multipart/form-data\" > <input id=\"file_import\" name=\"importData\" type=\"file\" />