在自定义显示中显示新类型的帖子/页面


WordPress: New type of posts/pages in a custom display

我想做一个像/news这样的页面,这个页面需要有类似于一个简单的WordPress主题的顶部/底部布局的内容:

Title "is a link also to access the post/page"
Space
Content of 160 Chars not more

简单地添加新新闻,添加新帖子或页面,然后简单地使新帖子像普通帖子一样,并在那里选择一个选项使其成为新闻页面。

这也应该在RSS提要中,但我认为这将是,因为它们只是定制的帖子/页面,所以没有问题吗?

这是一些让你开始的东西,只有基本的

function custom_news() {
register_post_type(
            'news', 
            array(
                    'label' => __('News'),
                    'public' => true,
                    'show_ui' => true,
                    'capability_type' => 'post',
                    'menu_position' => 100,
                    'menu_icon' => 'path/to/icon',
                    'supports' => array(
                                 'editor',
                                 'post-thumbnails',
                                 'excerpts',
                                 'custom-fields',
                                 'comments',
                                 'revisions')
            )
    );
    register_taxonomy( 'articles', 'news', array( 'hierarchical' => true, 'label' => __('Articles') ) ); 
   } 
   add_action('init', 'custom_news');

,然后使用WP_Query显示您想要的自定义帖子:

$args = array(
  'post_type' => 'news',
);

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) :
$the_query->the_post();
 echo '<a href="'.get_permalink($the_query->ID).'">' . get_the_title() . '</a>';
     echo '<p>' . get_the_content() . '</p>';
endwhile;

wp_reset_postdata();

这听起来像是你想要一个自定义的帖子类型。这应该表现得像一个普通的帖子或页面,但有自己的索引页和后端管理屏幕。您需要使用register_post_type来创建帖子类型。在那之后,事情基本上是自动的。摘自法典,作为参考:

function codex_custom_init() {
  $labels = array(
    'name' => 'Books',
    'singular_name' => 'Book',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Book',
    'edit_item' => 'Edit Book',
    'new_item' => 'New Book',
    'all_items' => 'All Books',
    'view_item' => 'View Book',
    'search_items' => 'Search Books',
    'not_found' =>  'No books found',
    'not_found_in_trash' => 'No books found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'Books'
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => array( 'slug' => 'book' ),
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  ); 
  register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );

参数可能有点令人困惑。Smashing杂志上有一篇文章应该会有所帮助。