有没有办法不在下面的代码中使用my_init_方法(它创建了一个定制的POST类型)?

时间:2011-05-11 作者:janoChen

Mike Schindel很久以前就为我提供了以下代码:

<?php
/**
 * Create the Blocks custom post type and the Sections custom taxonomy
 */
add_action(\'init\', \'my_init_method\');

if (!class_exists(\'YourSite_Blocks\')) {
    class YourSite_Blocks {
        static function on_load() {
            add_action(\'init\',array(__CLASS__,\'init\'));
            add_filter(\'manage_blocks_posts_columns\',
                array(__CLASS__,\'manage_blocks_posts_columns\'));
            add_filter(\'manage_posts_custom_column\',
                array(__CLASS__,\'manage_posts_custom_column\'),10,2);
            add_action(\'restrict_manage_posts\',
                array(__CLASS__,\'restrict_manage_posts\'));
        }
        // Register custom post types and custom taxonomies
        static function init() {
            register_post_type(\'blocks\',array(
                \'labels\' => array(
                    \'name\' => __( \'Blocks\' ),
                    \'singular_name\' => __( \'Block\' ),
                    \'add_new_item\' => \'Add New Block\',
                    \'edit_item\' => \'Edit Block\',
                    \'new_item\' => \'New Block\',
                    \'search_items\' => \'Search Block\',
                    \'not_found\' => \'No Blocks found\',
                    \'not_found_in_trash\' => \'No Blocks found in trash\',
                ),
                \'public\' => true,
                \'hierarchical\' => false,
                \'taxonomies\' => array( \'section\'),
                \'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'custom-fields\'),
                \'rewrite\' => array(\'slug\'=>\'blocks\',\'with_front\'=>false),
                \'menu_position\' => 50,
            ));
            register_taxonomy(\'location\',\'blocks\',array(
                \'hierarchical\' => true,
                \'labels\' => array(
                    \'name\' => __( \'Locations\' ),
                    \'singular_name\' => __( \'Location\' ),
                    \'add_new_item\' => \'Add New Location\',
                    \'edit_item\' => \'Edit Location\',
                    \'new_item\' => \'New Location\',
                    \'search_items\' => \'Search Location\',
                    \'not_found\' => \'No Location found\',
                    \'not_found_in_trash\' => \'No Locations found in trash\',
                    \'all_items\' => __( \'All Locations\' ),
                ),
                \'query_var\' => true,
                \'rewrite\' => array( \'slug\' => \'location\' ),
            ));
            register_taxonomy(\'section\',\'blocks\',array(
                \'hierarchical\' => true,
                \'labels\' => array(
                    \'name\' => __( \'Sections\' ),
                    \'singular_name\' => __( \'Section\' ),
                    \'add_new_item\' => \'Add New Section\',
                    \'edit_item\' => \'Edit Section\',
                    \'new_item\' => \'New Section\',
                    \'search_items\' => \'Search Section\',
                    \'not_found\' => \'No Sections found\',
                    \'not_found_in_trash\' => \'No Sectionss found in trash\',
                    \'all_items\' => __( \'All Sections\' ),
                ),
                \'query_var\' => true,
                \'rewrite\' => array( \'slug\' => \'section\' ),
            ));
            // Add initial terms
            if (!get_option(\'yoursite-blocks-initialized\')) {
                $terms = array(
                    \'Intro\',
                    \'Tagline\',
                    \'Mainbar\',
                    \'Sidebar\',
                    \'Featured\',
                    \'Content\',
                );
                foreach($terms as $term) {
                    if (!get_term_by(\'name\',$term,\'section\')) {
                        wp_insert_term($term, \'section\');
                    }
                }
                update_option(\'yoursite-blocks-initialized\',true);
            }
        }
        // Arrange the position of elements in the table
        function manage_blocks_posts_columns($columns) {
            $columns = array(
                \'cb\' => \'<input type="checkbox" />\',
                \'title\' => \'Name\',
                \'location_column\' => \'Location\',
                \'section_column\' => \'Section\',
                \'author\' => \'Author\',
                \'date\' => \'Date\',
            );
            return $columns;
        }
        // Verify that we are indeed working with only the custom post type and use a switch statement to test against the column
        function manage_posts_custom_column( $column,$post_id ) {
            global $typenow;
            if ($typenow==\'blocks\') {
                $location_taxonomy = \'location\';
                $section_taxonomy = \'section\';
                switch ($column) {
                case \'location_column\':
                    $location_column = get_the_terms($post_id,$location_taxonomy);
                    if (is_array($location_column)) {
                        foreach($location_column as $key => $location) {
                            $edit_link = get_term_link($location,$location_taxonomy);
                            $location_column[$key] = \'<a href="\'.$edit_link.\'">\' . $location->name . \'</a>\';
                        }
                        echo implode(\' | \',$location_column);
                    }
                    break;
                case \'section_column\':
                    $section_column = get_the_terms($post_id,$section_taxonomy);
                    if (is_array($section_column)) {
                        foreach($section_column as $key => $section) {
                            $edit_link = get_term_link($section,$section_taxonomy);
                            $section_column[$key] = \'<a href="\'.$edit_link.\'">\' . $section->name . \'</a>\';
                        }
                        echo implode(\' | \',$section_column);
                    }
                    break;
                }
            }
        }
        // Output multiple taxonomy filters
        function restrict_manage_posts() {

            // Only display these taxonomy filters on desired custom post_type listings
            global $typenow;
            if ($typenow == \'blocks\') {

                // Create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
                $filters = array(\'location\', \'section\');

                foreach ($filters as $tax_slug) {
                    // Retrieve the taxonomy object
                    $tax_obj = get_taxonomy($tax_slug);
                    $tax_name = $tax_obj->labels->name;
                    // Retrieve array of term objects per taxonomy
                    $terms = get_terms($tax_slug);

                    // Output html for taxonomy dropdown filter
                    echo "<select name=\'$tax_slug\' id=\'$tax_slug\' class=\'postform\'>";
                    echo "<option value=\'\'>Show All $tax_name</option>";
                    foreach ($terms as $term) {
                        // Output each select option line, check against the last $_GET to show the current option selected
                        echo \'<option value=\'. $term->slug, $_GET[$tax_slug] == $term->slug ? \' selected="selected"\' : \'\',\'>\' . $term->name .\' (\' . $term->count .\')</option>\';
                    }
                    echo "</select>";
                }
            }
        }
    }
    YourSite_Blocks::on_load();
}
到目前为止,它工作得很好。

我使用的主题似乎与第一行有问题:

add_action(\'init\', \'my_init_method\');
是否有任何方法可以修改此代码,使其在没有冲突代码行的情况下仍能工作?

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

生成的错误消息是什么?

此外,为了避免冲突,所有自定义主题函数、类、选项、常量等都应该有唯一的前缀,例如使用theme-slug.

一般函数名很可能是my_init_method 被其他插件或主题使用。尝试将其重命名为mytheme-slug_init_method.

编辑:

要实现,请将函数调用重命名为:

function my_init_method() {}
收件人:

function mytheme-slug_init_method() {}
然后,将钩子调用更改为:

add_action( \'init\', \'my_init_method\' );
收件人:

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

SO网友:chrisguitarguy

根据您对Chip的评论,我认为这可能是您想要的:

add_action( \'init\', array( \'YourSite_Blocks\', \'on_load\' ) ).

并删除add_action( \'init\', \'my_init_method\' );

您要做的是在每次wordpress加载时调用YourSite\\u Blocks类的on\\u load方法。这是负责注册所有帖子类型并添加操作和过滤器的方法。

结束

相关推荐

为什么分页在Category.php上不起作用

有两个不同的类别“生命科学”和“普通实验室”,还有关于“生命科学”的子类别产品。现在“普通实验室”类别想要获取“生命科学”拥有的所有子类别和产品。代码在类别中。php。以下是全部代码: <div id=\"container\"> <div id=\"content\" role=\"main\"> <?php if ( is_category(\'general-lab\') ) : ?> &#x