Cherry框架添加自定义帖子类型

时间:2014-08-21 作者:Dizzy Bryan High

在cherry框架的开发人员文档中寻找一些信息似乎是不存在的。

我正在尝试在我的theme init文件中添加一个带有以下代码的自定义帖子类型:(此代码在与cherry框架的主题一起使用的标准安装中运行良好,它不显示,我只收到一个404错误)

    <?php

add_action( \'after_setup_theme\', \'my_setup\' );

if ( ! function_exists( \'my_setup\' ) ):

function my_setup() {

    // This theme styles the visual editor with editor-style.css to match the theme style.
    add_editor_style();

    // This theme uses post thumbnails
    if ( function_exists( \'add_theme_support\' ) ) { // Added in 2.9
        add_theme_support( \'post-thumbnails\' );
        set_post_thumbnail_size( 299, 190, true ); // Normal post thumbnails
        add_image_size( \'slider-post-thumbnail\', 1386, 563, true ); // Slider Thumbnail
        add_image_size( \'slider-thumb\', 96, 41, true ); // Slider Small Thumbnail
    }

    // Add default posts and comments RSS feed links to head
    add_theme_support( \'automatic-feed-links\' );

    // custom menu support
    add_theme_support( \'menus\' );
    if ( function_exists( \'register_nav_menus\' ) ) {
        register_nav_menus(
            array(
              \'top_header_menu\' => \'Top Header Menu\',
              \'header_menu\' => \'Header Menu\',
              \'footer_menu\' => \'Footer Menu\'
            )
        );
    }
}
endif;


/* Slider */
function my_post_type_slider() {
    register_post_type( \'slider\',
                array( 
                \'label\' => __(\'Slides\', CURRENT_THEME), 
                \'singular_label\' => __(\'Slide\', CURRENT_THEME),
                \'_builtin\' => false,
                \'exclude_from_search\' => true, // Exclude from Search Results
                \'capability_type\' => \'page\',
                \'public\' => true, 
                \'show_ui\' => true,
                \'show_in_nav_menus\' => false,
                \'rewrite\' => array(
                    \'slug\' => \'slide-view\',
                    \'with_front\' => FALSE,
                ),
                \'query_var\' => "slide", // This goes to the WP_Query schema
                \'menu_icon\' => get_template_directory_uri() . \'/includes/images/icon_slides.png\',
                \'supports\' => array(
                        \'title\',
                        \'custom-fields\',
            \'thumbnail\')
                    ) 
                );
}

add_action(\'init\', \'my_post_type_slider\');



/* Portfolio */
function my_post_type_portfolio() {
    register_post_type( \'portfolio\',
                array( 
                \'label\' => __(\'Projects\', CURRENT_THEME),
                \'singular_label\' => __(\'Project\', CURRENT_THEME),
                \'_builtin\' => false,
                \'public\' => true, 
                \'show_ui\' => true,
                \'show_in_nav_menus\' => true,
                \'hierarchical\' => true,
                \'capability_type\' => \'page\',
                \'menu_icon\' => get_template_directory_uri() . \'/includes/images/icon_portfolio.png\',
                \'rewrite\' => array(
                    \'slug\' => \'portfolio-view\',
                    \'with_front\' => FALSE,
                ),
                \'supports\' => array(
                        \'title\',
                        \'editor\',
                        \'thumbnail\',
                        \'excerpt\',
                        \'custom-fields\',
                        \'comments\')
                    ) 
                );
    register_taxonomy(\'portfolio_category\', \'portfolio\', array(\'hierarchical\' => true, \'label\' => \'Categories\', \'singular_name\' => \'Category\', "rewrite" => true, "query_var" => true));
    register_taxonomy(\'portfolio_tag\', \'portfolio\', array(\'hierarchical\' => false, \'label\' => \'Tags\', \'singular_name\' => \'Tag\', \'rewrite\' => true, \'query_var\' => true));
}

add_action(\'init\', \'my_post_type_portfolio\');


/* Testimonial */
function my_post_type_testi() {
    register_post_type( \'testi\',
                array( 
                \'label\' => __(\'Testimonial\', CURRENT_THEME), 
                \'public\' => true, 
                \'show_ui\' => true,
                \'show_in_nav_menus\' => false,
                \'menu_position\' => 5,
                \'rewrite\' => array(
                    \'slug\' => \'testimonial-view\',
                    \'with_front\' => FALSE,
                ),
                \'supports\' => array(
                        \'title\',
                        \'custom-fields\',
                        \'thumbnail\',
                        \'editor\')
                    ) 
                );
}

add_action(\'init\', \'my_post_type_testi\');


/* Services */
function my_post_type_services() {
    register_post_type( \'services\',
                array( 
                \'label\' => __(\'Services\', CURRENT_THEME), 
                \'public\' => true, 
                \'show_ui\' => true,
                \'show_in_nav_menus\' => false,
                \'menu_position\' => 5,
                \'rewrite\' => array(
                    \'slug\' => \'services-view\',
                    \'with_front\' => FALSE,
                ),
                \'supports\' => array(
                        \'title\',
                        \'thumbnail\',
                        \'editor\')
                    ) 
                );
}

add_action(\'init\', \'my_post_type_services\');



/* FAQs */
function phi_post_type_faq() {
    register_post_type(\'faq\', 
                array(
                \'label\' => __(\'FAQs\', CURRENT_THEME),
                \'singular_label\' => __(\'FAQ\', CURRENT_THEME),
                \'public\' => false,
                \'show_ui\' => true,
                \'_builtin\' => false, // It\'s a custom post type, not built in
                \'_edit_link\' => \'post.php?post=%d\',
                \'capability_type\' => \'post\',
                \'hierarchical\' => false,
                \'rewrite\' => array("slug" => "faq"), // Permalinks
                \'query_var\' => "faq", // This goes to the WP_Query schema
                \'supports\' => array(\'title\',\'author\',\'editor\'),
                \'menu_position\' => 5,
                \'publicly_queryable\' => true,
                \'exclude_from_search\' => false,
                ));
}
add_action(\'init\', \'phi_post_type_faq\');


/* Our Team */
function my_post_type_team() {
    register_post_type( \'team\',
                array( 
                \'label\' => __(\'Our Team\', CURRENT_THEME), 
                \'singular_label\' => __(\'Person\', CURRENT_THEME),
                \'_builtin\' => false,
                \'exclude_from_search\' => true, // Exclude from Search Results
                \'capability_type\' => \'page\',
                \'public\' => true, 
                \'show_ui\' => true,
                \'show_in_nav_menus\' => false,
                \'menu_position\' => 5,
                \'rewrite\' => array(
                    \'slug\' => \'team-view\',
                    \'with_front\' => FALSE,
                ),
                \'supports\' => array(
                        \'title\',
                        \'custom-fields\',
                        \'editor\',
            \'thumbnail\')
                    ) 
                );
}

add_action(\'init\', \'my_post_type_team\');

function products_post_type() {

    $labels = array(
        \'name\'                => _x( \'Products\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'       => _x( \'Product\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'           => __( \'Products\', \'text_domain\' ),
        \'parent_item_colon\'   => __( \'Parent Product:\', \'text_domain\' ),
        \'all_items\'           => __( \'All Products\', \'text_domain\' ),
        \'view_item\'           => __( \'View Product\', \'text_domain\' ),
        \'add_new_item\'        => __( \'Add New Product\', \'text_domain\' ),
        \'add_new\'             => __( \'New Product\', \'text_domain\' ),
        \'edit_item\'           => __( \'Edit Product\', \'text_domain\' ),
        \'update_item\'         => __( \'Update Product\', \'text_domain\' ),
        \'search_items\'        => __( \'Search products\', \'text_domain\' ),
        \'not_found\'           => __( \'No products found\', \'text_domain\' ),
        \'not_found_in_trash\'  => __( \'No products found in Trash\', \'text_domain\' ),
    );
    $args = array(
        \'label\'               => __( \'product\', \'text_domain\' ),
        \'description\'         => __( \'Product information pages\', \'text_domain\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'thumbnail\', \'custom-fields\', ),
        \'taxonomies\'          => array( \'category\', \'post_tag\' ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
    );
    register_post_type( \'product\', $args );

}



// Hook into the \'init\' action
add_action( \'init\', \'products_post_type\');



function my_connection_types() {
    p2p_register_connection_type( 
        array(
            \'name\' => \'testi_to_product\',
            \'from\' => \'testi\',
            \'to\' => \'product\',
            \'admin_box\' => 
            array(
                \'show\' => \'any\',
                \'context\' => \'advanced\'
            )
        )
    );
}
add_action( \'p2p_init\', \'my_connection_types\' );
?>
在创建新产品后,我无法显示帖子,我只得到一个404错误,针对类别和单个页面。

有没有人成功地向cherry框架中添加了一个新的帖子类型,并为我指出了正确的方向,以便正确地集成它?

2 个回复
最合适的回答,由SO网友:Howard E 整理而成

在最后一次}添加之后的代码末尾:

add_action(\'init,\'products_post_type\');
确保您正在将此添加到/wp-content/your\\u-custom\\u-theme/includes/theme-init中的文件中。php文件,然后当然更新permalink。

使用CherryFramework,请确保不修改CherryFramework文件夹中的任何文件,否则在更新时会丢失。

我刚刚在Cherry开发网站上测试了你的代码,效果很好。你的主题一定还有很多?

SO网友:helgatheviking

尝试添加:

add_action( \'init\', \'products_post_type\' );
正如您在Codex示例中看到的register_post_type() 您必须将函数添加到init hook中,以便它实际上是run。否则,您拥有的是一个已定义但从未执行的函数。

如果使用默认主题执行此操作,则将函数添加到动作挂钩。我不知道theme-init.php 文件在Cherry框架中,但帖子类型实际上并不属于主题。当您需要更改主题时会发生什么情况?

我建议将您的帖子类型代码移动到插件或site-specific snippets plugin

结束

相关推荐

Paypal Framework

我正在尝试建立一个基于paypal订阅的网站,如果人们通过paypal支付订阅费,就可以在那里注册。我对Paypal IPN系统很陌生,但我发现这个插件似乎可以满足我的需要:http://wordpress.org/extend/plugins/paypal-framework/有人知道我需要通过hashCall() 函数为尝试注册的用户设置paypal订阅?我想这不是doDirectPayment 不需要所有CC参数,对吗?