custom post single page

时间:2018-09-24 作者:Johny D Good

WP和AP主题开发非常新。

我正在创建一个基于自定义帖子类型的主题。

这是我的职责。php

            /**
             * Defines theme version.
             */
        define( \'TUTORIALEE\', \'1.0.0\' );

        if ( ! isset( $content_width ) )
            $content_width = 800; /* pixels */

        if ( ! function_exists( \'tutorialee_setup\' ) ) :
        /**
         * Sets up theme defaults and registers support for various WordPress features.
         *
         * Note that this function is hooked into the after_setup_theme hook, which runs
         * before the init hook. The init hook is too late for some features, such as indicating
         * support post thumbnails.
         */
        function tutorialee_setup() {

            /**
             * Make theme available for translation.
             * Translations can be placed in the /languages/ directory.
             */
            load_theme_textdomain( \'tutorialee\', get_template_directory() . \'/languages\' );

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

            /**
             * Enable support for post thumbnails and featured images.
             */
            add_theme_support( \'post-thumbnails\' );

            /**
             * Add support for two custom navigation menus.
             */
            register_nav_menus( array(
                \'primary\'   => __( \'Primary Menu\', \'tutorialee\' ),
                \'secondary\' => __(\'Secondary Menu\', \'tutorialee\' )
            ) );

            /**
             * Enable support for the following post formats:
             * aside, gallery, quote, image, and video
             */
            add_theme_support( \'post-formats\', array ( \'aside\', \'gallery\', \'quote\', \'image\', \'video\' ) );
        }
        endif; // tutorialee_setup
        add_action( \'after_setup_theme\', \'tutorialee_setup\' );





        class MySettingsPage
        {
            /**
             * Holds the values to be used in the fields callbacks
             */
            private $options;

            /**
             * Start up
             */
            public function __construct()
            {
                add_action( \'admin_menu\', array( $this, \'add_plugin_page\' ) );
                add_action( \'admin_init\', array( $this, \'page_init\' ) );
            }

            /**
             * Add options page
             */
            public function add_plugin_page()
            {
                // This page will be under "Settings"
                add_options_page(
                    \'Settings Admin\', 
                    \'My Settingsz\', 
                    \'manage_options\', 
                    \'my-setting-admin\', 
                    array( $this, \'create_admin_page\' )
                );
            }

            /**
             * Options page callback
             */
            public function create_admin_page()
            {
                // Set class property
                $this->options = get_option( \'my_option_name\' );
                ?>
                <div class="wrap">
                    <h1>My Settings</h1>
                    <form method="post" action="options.php">
                    <?php
                        // This prints out all hidden setting fields
                        settings_fields( \'my_option_group\' );
                        do_settings_sections( \'my-setting-admin\' );
                        submit_button();
                    ?>
                    </form>
                </div>
                <?php
            }

            /**
             * Register and add settings
             */
            public function page_init()
            {        
                register_setting(
                    \'my_option_group\', // Option group
                    \'my_option_name\', // Option name
                    array( $this, \'sanitize\' ) // Sanitize
                );

                add_settings_section(
                    \'setting_section_id\', // ID
                    \'My Custom Settings\', // Title
                    array( $this, \'print_section_info\' ), // Callback
                    \'my-setting-admin\' // Page
                );  

                add_settings_field(
                    \'id_number\', // ID
                    \'ID Number\', // Title 
                    array( $this, \'id_number_callback\' ), // Callback
                    \'my-setting-admin\', // Page
                    \'setting_section_id\' // Section           
                );      

                add_settings_field(
                    \'title\', 
                    \'Title\', 
                    array( $this, \'title_callback\' ), 
                    \'my-setting-admin\', 
                    \'setting_section_id\'
                );      
            }

            /**
             * Sanitize each setting field as needed
             *
             * @param array $input Contains all settings fields as array keys
             */
            public function sanitize( $input )
            {
                $new_input = array();
                if( isset( $input[\'id_number\'] ) )
                    $new_input[\'id_number\'] = absint( $input[\'id_number\'] );

                if( isset( $input[\'title\'] ) )
                    $new_input[\'title\'] = sanitize_text_field( $input[\'title\'] );

                return $new_input;
            }

            /** 
             * Print the Section text
             */
            public function print_section_info()
            {
                print \'Enter your settings below:\';
            }

            /** 
             * Get the settings option array and print one of its values
             */
            public function id_number_callback()
            {
                printf(
                    \'<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />\',
                    isset( $this->options[\'id_number\'] ) ? esc_attr( $this->options[\'id_number\']) : \'\'
                );
            }

            /** 
             * Get the settings option array and print one of its values
             */
            public function title_callback()
            {
                printf(
                    \'<input type="text" id="title" name="my_option_name[title]" value="%s" />\',
                    isset( $this->options[\'title\'] ) ? esc_attr( $this->options[\'title\']) : \'\'
                );
            }
        }

        if( is_admin() )
            $my_settings_page = new MySettingsPage();


        // Adding Custom Post Type for Tutorials Listing

        function my_custom_post_tutorial() {

           //labels array added inside the function and precedes args array

           $labels = array(
            \'name\'               => _x( \'Tutorials\', \'post type general name\' ),
            \'singular_name\'      => _x( \'Tutorial\', \'post type singular name\' ),
            \'add_new\'            => _x( \'Add New\', \'Tutorial\' ),
            \'add_new_item\'       => __( \'Add New Tutorial\' ),
            \'edit_item\'          => __( \'Edit Tutorial\' ),
            \'new_item\'           => __( \'New Tutorial\' ),
            \'all_items\'          => __( \'All Tutorials\' ),
            \'view_item\'          => __( \'View Tutorial\' ),
            \'search_items\'       => __( \'Search tutorials\' ),
            \'not_found\'          => __( \'No tutorials found\' ),
            \'not_found_in_trash\' => __( \'No tutorials found in the Trash\' ),
            \'parent_item_colon\'  => \'\',
            \'menu_name\'          => \'Tutorials\'
          );

                 // args array

           $args = array(
            \'labels\'        => $labels,
            \'description\'   => \'Displays tutorials\',
            \'public\'        => true,
            \'menu_position\' => 4,
            \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
            \'has_archive\'   => false,
          );

          register_post_type( \'tutorial\', $args );
        }
        add_action( \'init\', \'my_custom_post_tutorial\' );



        //creating custom taxonomies for tutorials custom post

           //registration of taxonomies

        function my_taxonomies_tutorial() {

            //labels array

        $labels = array(
            \'name\'              => _x( \'Tutorial Categories\', \'taxonomy general name\' ),
            \'singular_name\'     => _x( \'Tutorial  Category\', \'taxonomy singular name\' ),
            \'search_items\'      => __( \'Search Tutorial Categories\' ),
            \'all_items\'         => __( \'All Tutorial Categories\' ),
            \'parent_item\'       => __( \'Parent Tutorial Category\' ),
            \'parent_item_colon\' => __( \'Parent Tutorial Category:\' ),
            \'edit_item\'         => __( \'Edit Tutorial Category\' ),
            \'update_item\'       => __( \'Update Tutorial Category\' ),
            \'add_new_item\'      => __( \'Add New Tutorial Category\' ),
            \'new_item_name\'     => __( \'New Tutorial Category\' ),
            \'menu_name\'         => __( \' Tutorial Categories\' ),
          );

           //args array

        $args = array(
            \'labels\' => $labels,
            \'hierarchical\' => true,
          );

          register_taxonomy( \'tutorial_category\', \'tutorial\', $args );
        }

        add_action( \'init\', \'my_taxonomies_tutorial\', 0 );
我只有一个教程。php文件,如果我理解正确的话,应该调用该文件作为任何自定义tutorialee帖子的模板。但当我转到永久链接时,我只得到索引。php代码。

请帮忙。

1 个回复
SO网友:Adnan

您可以使用此代码刷新重写规则

flush_rewrite_rules();

结束