自定义帖子类型Single.php模板仅显示最新帖子

时间:2018-06-28 作者:ardev

我可以用另一双眼睛看这个。。。我上次创建Wordpress主题已经有一段时间了。我搞不清楚我到底做错了什么。

我创建了一个自定义帖子类型(日志),并在自定义模板上设置了一个循环,我在主页上设置了该模板<然后我有一个日记账。单个帖子类型的php模板<在我的主页模板中,期刊CPT帖子显示得很好,url/permalink在检查时看起来是正确的。然而,当我点击其中任何一个(目前我有三个)时,它们都会显示最新帖子的内容。url栏中的url/permalink也显示正确,但内容错误。下面是一些代码:这是主页模板上的循环:

<div class="post-slider">
                        <?php $args = array(\'post_type\' => \'journal\', 
                        \'posts_per_page\' => -1,
                        \'post_status\' => \'publish\',
                        \'orderby\' => \'date\',
                        \'order\' => \'DESC\'); 
                                $loop = new WP_Query($args); 
                             if ( $loop->have_posts() ) : 
                                while ( $loop->have_posts() ) : 
                                    $loop->the_post(); ?>

                <div class="post-card">
                    <div class="post-card-inner">
                        <?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
                                    <a href="<?php the_permalink(); ?>" 
                                    rel="bookmark" 
                                    title="Permanent Link to <?php the_title_attribute(); ?>">

                                        <p><?php the_title(); ?></p>
                                    </a>
                    </div>

                </div>

                        <?php

                            // End the loop.
                            endwhile;
                             else: 
                        ?>
                                    <h1>No posts here!</h1>
                        <?php endif; 
                         wp_reset_postdata(); ?>
            </div>
这是来自单个日志的循环。php:

 <?php $args = array(\'post_type\' => \'journal\', 
            \'showposts\' => 1,
            \'post_status\' => \'publish\',
            \'orderby\' => \'date\',
            \'order\' => \'DESC\'); 
             $loop = new WP_Query($args); 
             if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

                <?php get_template_part( \'content\', get_post_format() ); ?>

                <!--- Add authorname taxonomy -->
                <?php the_terms( $post->ID, \'authorname\', \'Author: \', \', \', \' \' ); ?>


                <hr/>
                <nav class="nav-single">
                    <h3 class="assistive-text"><?php _e( \'Post navigation\', \'twentytwelve\' ); ?></h3>
                    <span class="nav-previous"><?php previous_post_link( \'%link\', \'<span class="meta-nav">\' . _x( \'&larr;\', \'Previous post link\', \'twentytwelve\' ) . \'</span> %title\' ); ?></span>
                    <span class="nav-next"><?php next_post_link( \'%link\', \'%title <span class="meta-nav">\' . _x( \'&rarr;\', \'Next post link\', \'twentytwelve\' ) . \'</span>\' ); ?></span>
                </nav><!-- .nav-single -->

                <?php comments_template( \'\', true ); ?>

            <?php endwhile; endif;  wp_reset_postdata();// end of the loop. ?>
这是自定义帖子类型:

/*
** Journal Entry
**
*/

function journal_entry() { 
    // creating (registering) the custom type 
    register_post_type( \'journal\',
            array(
                \'labels\' => array(
                    \'name\' => __( \'Journal Entry\' ),
                    \'singular_name\' => __( \'Journal Entry\' ),
                    \'add_new\' => __( \'Add New Journal Entry\' ),
                    \'add_new_item\' => __( \'Add New Journal Entry\' ),
                    \'edit_item\' => __( \'Edit Journal Entry\' ),
                    \'new_item\' => __( \'Add New Journal Entry\' ),
                    \'view_item\' => __( \'View Journal Entry\' ),
                    \'search_items\' => __( \'Search Journal Entry\' ),
                    \'not_found\' => __( \'No Journal Entry found\' ),
                    \'not_found_in_trash\' => __( \'No Journal Entry found in trash\' ),
                    \'menu_name\' => _x( \'Journal Entry\', \'journal\' ),
                ),
                //\'taxonomies\' => array(\'category\'),
                \'public\' => true,
                \'exclude_from_search\' => false,
                \'publicly_queryable\'  => true,
                \'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'custom-fields\' ),
                \'capability_type\' => \'post\',
                \'rewrite\' => array("slug" => "journal"), // Permalinks format
                \'has_archive\' => true,
                \'query_var\' => true,
                \'menu_position\' => 5,
                \'menu_icon\' => \'dashicons-book-alt\', /* the icon for the custom post type menu */
                //\'register_meta_box_cb\' => \'add_team_members_metaboxes\'
                //\'yarpp_support\' => true
            )
        ); /* end of register post type */

    /* this adds your post categories to your custom post type */
    //register_taxonomy_for_object_type( \'category\', \'stories\' );
    /* this adds your post tags to your custom post type */
    //register_taxonomy_for_object_type( \'post_tag\', \'custom_post_faq\' );

}

add_action( \'init\', \'journal_entry\');



function journal_taxonomy() {  
    register_taxonomy(  
        \'journal_categories\',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
        \'journal\',        //post type name
        array(  
            \'hierarchical\' => true,  
            \'label\' => \'Custom Categories\',  //Display name
            \'query_var\' => true,
            \'rewrite\' => array(
                \'slug\' => \'journal-categories\', // This controls the base slug that will display before each term
                \'with_front\' => false // Don\'t display the category base before 
            )
        )  
    );  
} 

add_action( \'init\', \'journal_taxonomy\');

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

您不应该在这些模板中使用自定义查询。主查询(使用have_posts();, the_post(); 等,无$loop) 已经为这些模板设置了正确的帖子。

查看您在单个模板上使用的自定义查询:

$args = array(\'post_type\' => \'journal\', 
        \'showposts\' => 1,
        \'post_status\' => \'publish\',
        \'orderby\' => \'date\',
        \'order\' => \'DESC\'); 
         $loop = new WP_Query($args);
这是对最新期刊帖子的查询。所以当你使用$loop->the_post(). 而且showposts 已弃用。使用posts_per_page 相反

因此:

if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
需要:

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

SO网友:Milo

主查询已包含正确的帖子,但您正在创建一个新查询,并要求按日期排序的单个帖子,这将始终为您提供最新的帖子。

删除自定义查询代码,只需运行默认循环:

if( have_posts() ) : while( have_posts() ) : the_post(); 
    // your template tags here
endwhile; endif;
这将从全局$wp_query.

结束

相关推荐

Double loop output

大家好,我是一个中等权重的wp设计师/开发人员,在我工作的网站上遇到了问题。演示页面如下:http://flock.simonpointer.com/blog/我在模板文件中继承了以下循环代码,我一辈子都不明白为什么我在特色内容框中的作者信息会有两个输出。我似乎得到了第一篇和第二篇文章。在第二个页面上,不会加载作者照片和链接详细信息,但会加载到第一个页面上,并且会显示第二个页面的帖子缩略图,但不会显示第一个页面的帖子缩略图。感谢您的帮助: <div class=\"col-2-