自定义帖子类型单页模板不起作用

时间:2018-11-27 作者:M Faizan Faizi

我创建了一个页面公文包并选择了模板Portfolio Template我的公文包页面无法访问template-portfolio.php 文件其中还包括content-portfolio.php 在模板零件文件夹中。它访问存档。php布局如果将col-lg-8更改为col-lg-7,并删除任何代码行,则会影响公文包页面。无法访问/显示template-portfolio.php 布局/视图这是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 );
这个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();
这是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(); ?> -->`

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

请参阅Template Hierarchy. 没有template-portfolio.php 样板这是必须的archive-portfolio.php.

此外,您不应该使用自定义查询。使用帖子类型存档模板中的主查询。WordPress已经为您查询了正确的帖子。

因此删除:

$portfolio = new WP_Query(\'post_type\'=> \'portfolio\');
并替换:

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

while ( have_posts() ) :
    the_post(); ?>
而且,您实际上没有使用content-portfolio.php 在任何地方您需要将其实际包含到归档模板中。介于while ( have_posts() ) :endwhile;:

get_template_part( \'path/to/content-portfolio\' ); // Leave off the .php
这些天在这里发布的问题中,至少有50%是由于人们在应该使用主循环时错误地创建了一个新查询造成的。如果能了解人们在哪里学着做这件事,我将不胜感激。

结束

相关推荐

是否可以取消对特定帖子类型的POSTS_PER_PAGE限制?

我想知道我是否可以取消特定帖子类型的posts\\u per\\u页面限制。在存档中。php页面我显示不同的帖子类型,对于特定的“出版物”帖子类型,我想显示所有帖子。我如何在不影响传统“post”类型的情况下实现这一点?