0

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

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

要简单地添加新新闻,请添加新帖子或页面,然后只需将新帖子设置为普通帖子,然后在此处选择一个选项以使其出现在新闻页面中。

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

4

2 回答 2

1

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

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 有一篇文章应该有所帮助

于 2013-03-14T14:09:00.370 回答
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();
于 2013-03-14T15:38:02.987 回答