WordPress.org

Make WordPress Core

Ticket #17808: add-to-functions.txt

File add-to-functions.txt, 1.3 KB (added by heilhard, 9 years ago)

Based on the TwentyTen theme, add this to functions.php

Line 
1// Register custom post type
2function create_post_type() {
3        register_post_type( 'custom_news',
4                array(
5                        'labels' => array(
6                                'name' => __( 'News' ),
7                                'singular_name' => __( 'News' )
8                        ),
9                'public' => true,
10                'has_archive' => true,
11                'exclude_from_search' => false,
12                'supports' => array('title', 'editor', 'revisions', 'author', 'excerpt', 'thumbnail'),
13                'rewrite' => array('slug' => 'news', 'with_front' => 'false')
14                )
15        );
16
17}
18add_action( 'init', 'create_post_type' );
19
20
21
22
23
24// Create custom feed from template
25function create_newsfeed() {
26    load_template( TEMPLATEPATH . '/news-feed.php');
27}
28
29// Replace default feed rewrite rules
30function customise_feed_rules($rules) {
31    // Remove all feed related rules
32    $filtered_rules = array_filter($rules, function($rule) {
33        return !preg_match("/feed/i", $rule);
34    });
35    // Add the rule(s) for your custom feed(s)
36    $new_rules = array(
37        'feed/newsfeed' => 'index.php?feed=newsfeed'
38    );
39    return $new_rules + $filtered_rules;
40}
41
42// Add the custom feed and update rewrite rules
43function add_newsfeed() {
44    global $wp_rewrite;
45    add_action('do_feed_newsfeed', 'create_newsfeed', 10, 1);
46    add_filter('rewrite_rules_array','customise_feed_rules');
47    $wp_rewrite->flush_rules();
48}
49
50add_action('init', 'add_newsfeed');
51
52