$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>pages - My Custom Post Type Still Using index.php|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

pages - My Custom Post Type Still Using index.php

matteradmin7PV0评论

I Registered a New CPT , and it did works but , after working on something different , when i refreshed the page , i realized that my CPT turn to work with index.php . i think all is fine with my code . here it is .

function mn_portfolio_post_type_init() {

$labels = array(

    // Labels .

);

$portfolioargs = array(

    'labels'                => $labels,
    'public'                => true,
    'has_archive'           => true,
    'query_var'             => true,
    'publicly_queryable'    => true,
    'rewrite'               => true,
    'supports'              => array(
        'title',
        'editor',
        'thumbnail',
        'author',
        'excerpt',
        'custom-fields',
        'revisions'
    ),
    'hierarchical'          => false,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-media-document',
    'capability_type'       => 'post'

);

  register_post_type('portfolio', $portfolioargs);

}

add_action('init', 'mn_portfolio_post_type_init');

After That I Used a new page-name.php to work with , and all was fine , but for now i'm out of ideas for help me out of that .

I Registered a New CPT , and it did works but , after working on something different , when i refreshed the page , i realized that my CPT turn to work with index.php . i think all is fine with my code . here it is .

function mn_portfolio_post_type_init() {

$labels = array(

    // Labels .

);

$portfolioargs = array(

    'labels'                => $labels,
    'public'                => true,
    'has_archive'           => true,
    'query_var'             => true,
    'publicly_queryable'    => true,
    'rewrite'               => true,
    'supports'              => array(
        'title',
        'editor',
        'thumbnail',
        'author',
        'excerpt',
        'custom-fields',
        'revisions'
    ),
    'hierarchical'          => false,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-media-document',
    'capability_type'       => 'post'

);

  register_post_type('portfolio', $portfolioargs);

}

add_action('init', 'mn_portfolio_post_type_init');

After That I Used a new page-name.php to work with , and all was fine , but for now i'm out of ideas for help me out of that .

Share Improve this question edited Feb 15, 2019 at 22:59 Simo Patrek asked Jan 13, 2018 at 23:38 Simo PatrekSimo Patrek 1121 gold badge4 silver badges15 bronze badges 10
  • 1 I would suggest moving it to a plugin file since you're changing how data is structured. What template file do you want it to use? – Cedon Commented Jan 13, 2018 at 23:46
  • thanks man for responding , but i'm doing it in functions.php – Simo Patrek Commented Jan 13, 2018 at 23:47
  • What template file do you want this post type to use to show content? – Cedon Commented Jan 13, 2018 at 23:48
  • i'm not sure what you wanna tell me ? – Simo Patrek Commented Jan 13, 2018 at 23:48
  • 1 have you seen developer.wordpress/themes/basics/template-hierarchy/… and developer.wordpress/themes/basics/template-hierarchy/… ? – Michael Commented Jan 13, 2018 at 23:59
 |  Show 5 more comments

1 Answer 1

Reset to default 1

It looks like your theme is missing certain key files that are needed in order to keep WordPress from defaulting to index.php per the WordPress Template Hierarchy.

Since you have defined your portfolio post type to act like posts by setting 'hierarchical' to false, you should probably be using the post custom template files.

In reading the chart from left to right, you want to create one, possible two files depending if you want the index view to be custom as well as the single post view. For the single view, WordPress uses single-$posttype.php where $posttype is replaced by the slug you gave your custom post type. In your case this file would be single-portfolio.php. The archive page is a similar naming convention with archive-$posttype.php so you would make archive-portfolio.php

Pages do not have support for this. With a page you can pretty much only use page-$id.php or page-$slug.php.

Post a comment

comment list (0)

  1. No comments so far