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

时间:2013-03-14 13:43:48

标签: php wordpress wordpress-theming

我想创建一个类似/news的页面,此页面需要具有类似于简单WordPress主题中的顶部/底部布局的内容:

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

简单地添加新新闻,添加新帖子或页面,然后只需将新帖子设为普通帖子,然后在新闻页面中选择一个选项即可。

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

2 个答案:

答案 0 :(得分:1)

听起来你想要Custom Post Type。这应该像普通的帖子或页面一样,但有自己的索引页面和后端管理屏幕。您需要使用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 Magazine has a post that should help

答案 1 :(得分:1)

只需基础知识

即可让您入门
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();