找不到自定义帖子类型模板

时间:2015-10-17 17:46:34

标签: php wordpress custom-post-type

我为汽车创建了一个自定义的帖子类型,我已经引导了tutorial。我遇到的问题是我返回404而不是 archive-car.php single-car.php 。我还尝试将复数版本添加到模板名称中,但是没有成功。可能有什么不对?我错过了什么吗?

<?php
if( ! function_exists( 'quote_create_post_type' ) ) :
    function quote_create_post_type() {
        $labels = array(
            'name' => 'Cars',
            'singular_name' => 'Car',
            'add_new' => 'Add Car',
            'all_items' => 'All Cars',
            'add_new_item' => 'Add Car',
            'edit_item' => 'Edit car',
            'new_item' => 'New car',
            'view_item' => 'View car',
            'search_items' => 'Search cars',
            'not_found' => 'No cars found',
            'not_found_in_trash' => 'No cars found in trash',
            'parent_item_colon' => 'Parent car'
            //'menu_name' => default to 'name'
        );
        $args = array(
            'labels' => $labels,
            'public' => true,
            'has_archive' => true,
            'publicly_queryable' => true,
            'query_var' => true,
            'rewrite' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'supports' => array(
                'title',
                'editor',
                'excerpt',
                'thumbnail',
                //'author',
                //'trackbacks',
                //'custom-fields',
                //'comments',
                'revisions',
                //'page-attributes', // (menu order, hierarchical must be true to show Parent option)
                //'post-formats',
            ),
            'menu_position' => 5,
            'exclude_from_search' => true,
            'register_meta_box_cb' => 'quote_add_post_type_metabox'
        );
        register_post_type( 'car', $args );
        //flush_rewrite_rules();

        register_taxonomy( 'quote_category', // register custom taxonomy - category
            'car',
            array(
                'hierarchical' => true,
                'labels' => array(
                    'name' => 'Brands',
                    'singular_name' => 'Brand',
                )
            )
        );

    }
    add_action( 'init', 'quote_create_post_type' );


    function quote_add_post_type_metabox() { // add the meta box
        add_meta_box( 'quote_metabox', 'Car Details', 'quote_metabox', 'car', 'normal' );
    }


    function quote_metabox() {
        global $post;
        // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="quote_post_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

        // Get the data if its already been entered
        $quote_post_name = get_post_meta($post->ID, '_quote_post_name', true);
        $quote_post_desc = get_post_meta($post->ID, '_quote_post_desc', true);

        // Echo out the field
        ?>

        <table class="form-table">
            <tr>
                <th>
                    <label>Brand</label>
                </th>
                <td>
                    <input type="text" name="quote_post_name" value="<?php echo $quote_post_name; ?>"> 
                    <!-- classes: .small-text .regular-text .large-text -->
                </td>
            </tr>
            <tr>
                <th>
                    <label>Description</label>
                </th>
                <td>
                    <textarea name="quote_post_desc" class="large-text"><?php echo $quote_post_desc; ?></textarea>
                </td>
            </tr>
        </table>
    <?php
    }


    function quote_post_save_meta( $post_id, $post ) { // save the data

        /*
         * We need to verify this came from our screen and with proper authorization,
         * because the save_post action can be triggered at other times.
         */

        if ( ! isset( $_POST['quote_post_noncename'] ) ) { // Check if our nonce is set.
            return;
        }

        if( !wp_verify_nonce( $_POST['quote_post_noncename'], plugin_basename(__FILE__) ) ) { // Verify that the nonce is valid.
            return $post->ID;
        }

        // verify this came from the our screen and with proper authorization,
        // because save_post can be triggered at other times
        if( !wp_verify_nonce( $_POST['quote_post_noncename'], plugin_basename(__FILE__) ) ) {
            return $post->ID;
        }

        // is the user allowed to edit the post or page?
        if( ! current_user_can( 'edit_post', $post->ID )){
            return $post->ID;
        }
        // ok, we're authenticated: we need to find and save the data
        // we'll put it into an array to make it easier to loop though

        $quote_post_meta['_quote_post_name'] = $_POST['quote_post_name'];
        $quote_post_meta['_quote_post_desc'] = $_POST['quote_post_desc'];

        // add values as custom fields
        foreach( $quote_post_meta as $key => $value ) { // cycle through the $quote_post_meta array
            // if( $post->post_type == 'revision' ) return; // don't store custom data twice
            $value = implode(',', (array)$value); // if $value is an array, make it a CSV (unlikely)
            if( get_post_meta( $post->ID, $key, FALSE ) ) { // if the custom field already has a value
                update_post_meta($post->ID, $key, $value);
            } else { // if the custom field doesn't have a value
                add_post_meta( $post->ID, $key, $value );
            }
            if( !$value ) { // delete if blank
                delete_post_meta( $post->ID, $key );
            }
        }
    }
    add_action( 'save_post', 'quote_post_save_meta', 1, 2 ); // save the custom fields
endif; // end of function_exists()


if( ! function_exists( 'view_quotes_posts' ) ) : // output
    function view_quotes_posts($do_shortcode = 1, $strip_shortcodes = 0 ) {

        $args = array(
            'posts_per_page'     => 10,
            'offset'          => 0,
            //'category'        => ,
            'orderby'         => 'menu_order, post_title', // post_date, rand
            'order'           => 'DESC',
            //'include'         => ,
            //'exclude'         => ,
            //'meta_key'        => ,
            //'meta_value'      => ,
            'post_type'       => 'car',
            //'post_mime_type'  => ,
            //'post_parent'     => ,
            'post_status'     => 'publish',
            'suppress_filters' => true
        );

        $posts = get_posts( $args );

        $html = '';
        foreach ( $posts as $post ) {
            $meta_name = get_post_meta( $post->ID, '_quote_post_name', true );
            $meta_desc = get_post_meta( $post->ID, '_quote_post_desc', true );
            $img = get_the_post_thumbnail( $post->ID, 'medium' );
            if( empty( $img ) ) {
                $img = '<img src="'.plugins_url( '/img/default.png', __FILE__ ).'">';
            }


            if( has_post_thumbnail( $post->ID ) ) {
                $img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' );
                $img_url = $img[0];

                //the_post_thumbnail( 'thumbnail' ); /* thumbnail, medium, large, full, thumb-100, thumb-200, thumb-400, array(100,100) */
            }

            $content = $post->post_content;
            if( $do_shortcode == 1 ) {
                $content = do_shortcode( $content );
            }
            if( $strip_shortcodes == 1 ) {
                $content = strip_shortcodes( $content );
            }
            $content = wp_trim_words( $content, 30, '...');
            $content = wpautop( $content );

            $html .= '
            <div>
                <h3>'.$post->post_title.'</h3>
                <div>
                    <p>Name: '.$meta_name.'</p>
                    <p>Description: '.$meta_desc.'</p>
                </div>
                <div>'.$img.'</div>
                <div>'.$content.'</div>
            </div>
            ';
        }
        $html = '<div class="wrapper">'.$html.'</div>';
        return $html;
    }
endif; // end of function_exists()
?>

更多信息

我所做的只是扩展TwentyFifteen主题。

1 个答案:

答案 0 :(得分:2)

如果我不得不为此付钱,我会说你没有更新你的固定链接!只需转到设置&gt;&gt;永久链接&gt;&gt;帖子名称

codex提供了更多信息:

  

注意:在某些情况下,必须更新永久链接结构,以便在查看自定义帖子类型的帖子时访问新模板文件。为此,请转到管理面板&gt;设置&gt;永久链接,将永久链接结构更改为不同的结构,保存更改,并将其更改回所需的结构。

相关问题