最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Creating a Page to View the List of Posts for a Custom Post Type?

programmeradmin1浏览0评论

(Moderator's Note: The original title was "viewing custom post types")

I've setup a custom post type called 'recordings' with a custom taxonomy called 'themes':

add_action('init', 'recordings');
function recordings() {
  $args = array(
    'label' => __('Recordings'),
    'singular_label' => __('Recordings'),
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => true,
    'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')
  );
  register_post_type( 'recordings' , $args );
}
register_taxonomy("Themes", array("recordings"), array(
  "hierarchical" => true, 
  "label" => "Themes", 
  "singular_label" => "Theme", 
  "rewrite" => true
));

I've read that I should now make a copy of page.php, rename it recordings-page.php and season to taste (code is as follows):

<?php
/*
Template Name: recordingsPage
*/
?>
<?php get_header(); ?>
<div id="container">
  <div id="content" role="main">
  <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>
    <?php $recent = new WP_Query('post_type=recording&posts_per_page=10&meta_key=Date&orderby=meta_value&order=ASC'); ?>
    <?php while($recent->have_posts()) : $recent->the_post();?>
      <?php the_title( '<h2 class="entry-title"><a href="' . 
        get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . 
        '" rel="bookmark">', '</a></h2>' 
      ); ?>
      <div class="entry-content">
        <?php the_content(); ?>
        <?php wp_link_pages( array( 
          'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 
          'after' => '</div>' 
        ) ); ?>
        <?php edit_post_link( 
          __( 'Edit', 'twentyten' ), 
          '<span class="edit-link">', '</span>' 
        ); ?>
      </div><!-- .entry-content -->
      <?php comments_template( '', true ); ?>
    <?php endwhile; ?>
  </div><!-- #content -->
</div><!-- #container -->

But this is where I'm stuck. I'm not sure how I should call my recordings-page? I've added a new page called recordings, but all that shows up is the header/nav and the sidebar. I'm working with a fresh install using the TwenyTen theme. I'm sure I'm missing something here but I don't know what.

(Moderator's Note: The original title was "viewing custom post types")

I've setup a custom post type called 'recordings' with a custom taxonomy called 'themes':

add_action('init', 'recordings');
function recordings() {
  $args = array(
    'label' => __('Recordings'),
    'singular_label' => __('Recordings'),
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => true,
    'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')
  );
  register_post_type( 'recordings' , $args );
}
register_taxonomy("Themes", array("recordings"), array(
  "hierarchical" => true, 
  "label" => "Themes", 
  "singular_label" => "Theme", 
  "rewrite" => true
));

I've read that I should now make a copy of page.php, rename it recordings-page.php and season to taste (code is as follows):

<?php
/*
Template Name: recordingsPage
*/
?>
<?php get_header(); ?>
<div id="container">
  <div id="content" role="main">
  <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>
    <?php $recent = new WP_Query('post_type=recording&posts_per_page=10&meta_key=Date&orderby=meta_value&order=ASC'); ?>
    <?php while($recent->have_posts()) : $recent->the_post();?>
      <?php the_title( '<h2 class="entry-title"><a href="' . 
        get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . 
        '" rel="bookmark">', '</a></h2>' 
      ); ?>
      <div class="entry-content">
        <?php the_content(); ?>
        <?php wp_link_pages( array( 
          'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 
          'after' => '</div>' 
        ) ); ?>
        <?php edit_post_link( 
          __( 'Edit', 'twentyten' ), 
          '<span class="edit-link">', '</span>' 
        ); ?>
      </div><!-- .entry-content -->
      <?php comments_template( '', true ); ?>
    <?php endwhile; ?>
  </div><!-- #content -->
</div><!-- #container -->

But this is where I'm stuck. I'm not sure how I should call my recordings-page? I've added a new page called recordings, but all that shows up is the header/nav and the sidebar. I'm working with a fresh install using the TwenyTen theme. I'm sure I'm missing something here but I don't know what.

Share Improve this question edited Feb 22, 2011 at 7:17 MikeSchinkel 37.6k14 gold badges117 silver badges132 bronze badges asked Feb 22, 2011 at 7:03 don de liondon de lion 531 silver badge6 bronze badges 7
  • it doesn't look like my code for recordings-page.php showed up entirely. am I not formatting it correctly? – don de lion Commented Feb 22, 2011 at 7:08
  • here's my recordings-page.php code:; – don de lion Commented Feb 22, 2011 at 7:16
  • @don de lion - I reformatted for you. All likes of code must be indented 4 spaces to format correctly. – MikeSchinkel Commented Feb 22, 2011 at 7:18
  • @don de lion: What do you want to do? Create an "archive page" for multiple recordings, using a "fake" page? Or a template for a single recording? – Jan Fabry Commented Feb 22, 2011 at 8:26
  • Is your code missing the s at the end of recording in the post_type query? – Christopher Commented Feb 22, 2011 at 14:01
 |  Show 2 more comments

1 Answer 1

Reset to default 2

I think it might be as simple as you needing to set the Page Template to "recordingsPage" as you can see in the following screenshots:


(source: mikeschinkel)


(source: mikeschinkel)

UPDATE

Looking again at your code it seems you define your post type as plural ('recordings') and yet you refer to it as singular ('recording') in your WP_Query. You need to be consistent; WordPress can't figure out the difference (My experience says to go with singular all the way but if you have existing recordings in your database you'll now need to update those records to use 'recording'instead of 'recordings'.)

发布评论

评论列表(0)

  1. 暂无评论