$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'); ?>custom post type single page template not working|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)

custom post type single page template not working

matteradmin8PV0评论

I created a page portfolio and select template Portfolio Template My portfolio page does not access the template-portfolio.php file. In which also includes content-portfolio.php in template parts folder. It accesses archive.php layout if change the col-lg-8 to col-lg-7 and remove any line of code its effect on the portfolio page. It does not access/display template-portfolio.php layout/view This is portfolio-post-type.php

 <?php
// Register Portfolio Post Type
function portfolio_post_type() {

    $labels = array(
        'name'                  => _x( 'portfolios', 'Post Type General Name', 'portfolio' ),
        'singular_name'         => _x( 'portfolio', 'Post Type Singular Name', 'portfolio' ),
        'menu_name'             => __( 'Portfolio', 'portfolio' ),
        'name_admin_bar'        => __( 'Portfolio', 'portfolio' ),
        'archives'              => __( 'Portfolio Archives', 'portfolio' ),
        'attributes'            => __( 'Portfolio Attributes', 'portfolio' ),
        'parent_item_colon'     => __( 'Parent Item:', 'portfolio' ),
        'all_items'             => __( 'All Portfolio', 'portfolio' ),
        'add_new_item'          => __( 'Add New Portfolio', 'portfolio' ),
        'add_new'               => __( 'Add New portfolio', 'portfolio' ),
        'new_item'              => __( 'New portfolio', 'portfolio' ),
        'edit_item'             => __( 'Edit portfolio', 'portfolio' ),
        'update_item'           => __( 'Update portfolio', 'portfolio' ),
        'view_item'             => __( 'View portfolio', 'portfolio' ),
        'view_items'            => __( 'View portfolios', 'portfolio' ),
        'search_items'          => __( 'Search portfolio', 'portfolio' ),
        'not_found'             => __( 'Not found', 'portfolio' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'portfolio' ),
        'featured_image'        => __( 'Featured Image', 'portfolio' ),
        'set_featured_image'    => __( 'Set featured image', 'portfolio' ),
        'remove_featured_image' => __( 'Remove featured image', 'portfolio' ),
        'use_featured_image'    => __( 'Use as featured image', 'portfolio' ),
        'insert_into_item'      => __( 'Insert into item', 'portfolio' ),
        'uploaded_to_this_item' => __( 'Uploaded to this portfolio', 'portfolio' ),
        'items_list'            => __( 'Portfolios list', 'portfolio' ),
        'items_list_navigation' => __( 'Portfolios list navigation', 'portfolio' ),
    'filter_items_list'     => __( 'Filter portfolios list', 'portfolio' ),
);
$args = array(
    'label'                 => __( 'portfolio', 'portfolio' ),
    'description'           => __( 'display company projects', 'portfolio' ),


    'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail' ),
        'taxonomies'            => array( 'category' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-portfolio',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'portfolio', $args );

}
add_action( 'init', 'portfolio_post_type', 0 );

This template-portfolio.php

get_header();
?>

<div id="primary" class="content-area">
    <div id="main" class="site-main col-lg-8 col-md-8 col-sm-8 col-xs-12">
        <div class="home-page-data">
    <?php
     $portfolio = new WP_Query('post_type'=> 'portfolio');

    while ($portfolio-> have_posts() ) :
        $portfolio->the_post(); ?>

        <div class="col-md-4">
            <?php the_post_thumbnail(); ?>
        </div> 

<?php   endwhile; // End of the loop.
    ?>  
        </div>
    </div><!-- #main -->
    <div class="main-sidebar col-lg-4 col-md-4 col-sm-2 col-xs-12">
            <?php get_sidebar(); ?>

        </div>
</div><!-- #primary -->

<?php

    get_footer();

This is content-portfolio.php

     <?php
    /**
     * Template part for displaying page content in template-portfolio.php
     *
     *
     * @package E_blog
     */

    ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


        <?php eblog_post_thumbnail(); ?>

        <div class="entry-content">
            <?php
            the_content();

            wp_link_pages( array(
                'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'eblog' ),
                'after'  => '</div>',
            ) );
            ?>
        </div><!-- .e

ntry-content -->

    <?php if ( get_edit_post_link() ) : ?>
        <footer class="entry-footer">
            <?php
            edit_post_link(
                sprintf(
                    wp_kses(
                        /* translators: %s: Name of current post. Only visible to screen readers */
                        __( 'Edit <span class="screen-reader-text">%s</span>', 'eblog' ),
                        array(
                            'span' => array(
                                'class' => array(),
                            ),
                        )
                    ),
                    get_the_title()
                ),
                '<span class="edit-link">',
                '</span>'
            );
            ?>
        </footer><!-- .entry-footer -->
    <?php endif; ?>
</article><!-- #post-<?php the`_ID(); ?> -->`

I created a page portfolio and select template Portfolio Template My portfolio page does not access the template-portfolio.php file. In which also includes content-portfolio.php in template parts folder. It accesses archive.php layout if change the col-lg-8 to col-lg-7 and remove any line of code its effect on the portfolio page. It does not access/display template-portfolio.php layout/view This is portfolio-post-type.php

 <?php
// Register Portfolio Post Type
function portfolio_post_type() {

    $labels = array(
        'name'                  => _x( 'portfolios', 'Post Type General Name', 'portfolio' ),
        'singular_name'         => _x( 'portfolio', 'Post Type Singular Name', 'portfolio' ),
        'menu_name'             => __( 'Portfolio', 'portfolio' ),
        'name_admin_bar'        => __( 'Portfolio', 'portfolio' ),
        'archives'              => __( 'Portfolio Archives', 'portfolio' ),
        'attributes'            => __( 'Portfolio Attributes', 'portfolio' ),
        'parent_item_colon'     => __( 'Parent Item:', 'portfolio' ),
        'all_items'             => __( 'All Portfolio', 'portfolio' ),
        'add_new_item'          => __( 'Add New Portfolio', 'portfolio' ),
        'add_new'               => __( 'Add New portfolio', 'portfolio' ),
        'new_item'              => __( 'New portfolio', 'portfolio' ),
        'edit_item'             => __( 'Edit portfolio', 'portfolio' ),
        'update_item'           => __( 'Update portfolio', 'portfolio' ),
        'view_item'             => __( 'View portfolio', 'portfolio' ),
        'view_items'            => __( 'View portfolios', 'portfolio' ),
        'search_items'          => __( 'Search portfolio', 'portfolio' ),
        'not_found'             => __( 'Not found', 'portfolio' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'portfolio' ),
        'featured_image'        => __( 'Featured Image', 'portfolio' ),
        'set_featured_image'    => __( 'Set featured image', 'portfolio' ),
        'remove_featured_image' => __( 'Remove featured image', 'portfolio' ),
        'use_featured_image'    => __( 'Use as featured image', 'portfolio' ),
        'insert_into_item'      => __( 'Insert into item', 'portfolio' ),
        'uploaded_to_this_item' => __( 'Uploaded to this portfolio', 'portfolio' ),
        'items_list'            => __( 'Portfolios list', 'portfolio' ),
        'items_list_navigation' => __( 'Portfolios list navigation', 'portfolio' ),
    'filter_items_list'     => __( 'Filter portfolios list', 'portfolio' ),
);
$args = array(
    'label'                 => __( 'portfolio', 'portfolio' ),
    'description'           => __( 'display company projects', 'portfolio' ),


    'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail' ),
        'taxonomies'            => array( 'category' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-portfolio',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'portfolio', $args );

}
add_action( 'init', 'portfolio_post_type', 0 );

This template-portfolio.php

get_header();
?>

<div id="primary" class="content-area">
    <div id="main" class="site-main col-lg-8 col-md-8 col-sm-8 col-xs-12">
        <div class="home-page-data">
    <?php
     $portfolio = new WP_Query('post_type'=> 'portfolio');

    while ($portfolio-> have_posts() ) :
        $portfolio->the_post(); ?>

        <div class="col-md-4">
            <?php the_post_thumbnail(); ?>
        </div> 

<?php   endwhile; // End of the loop.
    ?>  
        </div>
    </div><!-- #main -->
    <div class="main-sidebar col-lg-4 col-md-4 col-sm-2 col-xs-12">
            <?php get_sidebar(); ?>

        </div>
</div><!-- #primary -->

<?php

    get_footer();

This is content-portfolio.php

     <?php
    /**
     * Template part for displaying page content in template-portfolio.php
     *
     *
     * @package E_blog
     */

    ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


        <?php eblog_post_thumbnail(); ?>

        <div class="entry-content">
            <?php
            the_content();

            wp_link_pages( array(
                'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'eblog' ),
                'after'  => '</div>',
            ) );
            ?>
        </div><!-- .e

ntry-content -->

    <?php if ( get_edit_post_link() ) : ?>
        <footer class="entry-footer">
            <?php
            edit_post_link(
                sprintf(
                    wp_kses(
                        /* translators: %s: Name of current post. Only visible to screen readers */
                        __( 'Edit <span class="screen-reader-text">%s</span>', 'eblog' ),
                        array(
                            'span' => array(
                                'class' => array(),
                            ),
                        )
                    ),
                    get_the_title()
                ),
                '<span class="edit-link">',
                '</span>'
            );
            ?>
        </footer><!-- .entry-footer -->
    <?php endif; ?>
</article><!-- #post-<?php the`_ID(); ?> -->`
Share Improve this question asked Nov 27, 2018 at 14:44 M Faizan FaiziM Faizan Faizi 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Please see the Template Hierarchy. There is no template-portfolio.php template. It needs to be archive-portfolio.php.

Also, you should not be using a custom query*. Use the main query in your post type archive templates. WordPress already queries the correct posts for you.

So remove:

$portfolio = new WP_Query('post_type'=> 'portfolio');

And replace:

while ($portfolio-> have_posts() ) :
    $portfolio->the_post(); ?>

With:

while ( have_posts() ) :
    the_post(); ?>

Also, you're not actually using content-portfolio.php anywhere. You need to actually include it into the archive template. Somewhere between while ( have_posts() ) : and endwhile;:

get_template_part( 'path/to/content-portfolio' ); // Leave off the .php

*It seems like at least 50% of the questions posted here these days are caused by people incorrectly creating a new query when they should be using the main loop. I'd appreciate any insight into where people are learning to do this.

Post a comment

comment list (0)

  1. No comments so far