如何呈现带有帖子名称的单个帖子模板?

时间:2014-01-10 作者:Manolo

我用Ajax调用页面,我在functions.php 要确定调用哪个页面,然后呈现相应的模板:

function ajax_callback() {
    if( isset(  $_POST[\'href\'] ) ) {
        $pageId = preg_match( \'/^http:\\/\\/.+\\/\\?page_id=\\d+$/\', $_POST[\'href\'] ) ? substr( strrchr( $_POST[\'href\'], \'=\' ), 1 ) : \'8\' ;

        if( $pageId === \'8\' ) get_template_part(\'home-ajax\');
        elseif( $pageId === \'22\' ) get_template_part(\'reviews-rvw-ajax\');
        elseif( $pageId === \'6\' ) get_template_part(\'noticias-news-ajax\');
        elseif( $pageId === \'109\' ) get_template_part(\'entrevistas-ajax\');
    }
}
这很好,但现在我也想打单发。我的帖子类型如下:

$pageId = preg_match( \'/^http:\\/\\/.+\\/\\?page_id=\\d+$/\', $_POST[\'href\'] ) ? substr( strrchr( $_POST[\'href\'], \'=\' ), 1 ) : \'no-page\' ;
if( $pageId === \'no-page\' && preg_match( \'/^http:\\/\\/.+\\/\\?news=.+$/\', $_POST[\'href\'] ) ) {
        $newSlug = substr( strrchr( $_POST[\'href\'], \'=\' ), 1 );
} elseif ( $pageId === \'no-page\' && preg_match( \'/^http:\\/\\/.+\\/\\?reviews=.+$/\', $_POST[\'href\'] ) ) {
    $reviewSlug = substr( strrchr( $_POST[\'href\'], \'=\' ), 1 );
}

if( $pageId !== 0 ) {
    if( $pageId === \'8\' ) get_template_part(\'home-ajax\');
    elseif( $pageId === \'22\' ) get_template_part(\'reviews-rvw-ajax\');
    elseif( $pageId === \'6\' ) get_template_part(\'noticias-news-ajax\');
    elseif( $pageId === \'109\' ) get_template_part(\'entrevistas-ajax\');
} elseif( $newSlug !== \'\' ) {
            //here I have to get the single post template
            // e.g. $newSlug = slug-post-name, and post_type = \'news\'
}
但我不知道如何用slug的名字来命名这个单一的帖子模板。我得打电话给single.php 没有页眉和页脚的模板,因此我可以执行以下操作:

get_template_part(\'single-ajax\');

//single-ajax.php
<?php
            // Start the Loop.
            while ( have_posts() ) : the_post();

                /*
                 * Include the post format-specific template for the content. If you want to
                 * use this in a child theme, then include a file called called content-___.php
                 * (where ___ is the post format) and that will be used instead.
                 */
                get_template_part( \'content\', get_post_format() );

                // Previous/next post navigation.
                twentyfourteen_post_nav();

                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) {
                    comments_template();
                }
            endwhile;
但不起作用,因为我应该定义查询对象:

        <?php
 $temp = $wp_query; // assign ordinal query to temp variable for later use  
 $wp_query = null;
 $newSlug = substr( strrchr( $_POST[\'href\'], \'=\' ), 1 );
 $wp_query = new WP_Query( array( \'name\' => $newSlug  ) ); 
 if($wp_query->have_posts()) {
while ( $wp_query->have_posts() ) : $wp_query->the_post();
    // Start the Loop.
        /*
         * Include the post format-specific template for the content. If you want to
         * use this in a child theme, then include a file called called content-___.php
         * (where ___ is the post format) and that will be used instead.
         */
        get_template_part( \'content\', get_post_format() );

        // Previous/next post navigation.
        twentyfourteen_post_nav();

        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || get_comments_number() ) {
            comments_template();
        }
endwhile;
}
die();
但这不会显示帖子模板。实际上,不会开始执行循环。你知道发生了什么事,或者有什么更简单的方法来实现这一点吗?

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

好吧,我只需将post类型添加到WP查询的参数中即可实现:

$args = array (
 \'post_type\' => $post_type,
 \'post_status\' => \'publish\',
 \'name\'=> $newSlug
);
 $wp_query = new WP_Query( $args ); 
完成了!

结束

相关推荐

wp_query inside the_loop

我的循环有这个问题。我正在制作一个将从主查询调用的快捷码。它将显示在\\u循环的首页上。因为某种我无法理解的原因。第二个查询只显示一篇文章,而它应该显示3篇。因此,短代码将出现在页面内容中。在“设置”部分,我将首页设置为“主页”,将博客页面设置为“博客”,但主页不是模板。它是由索引生成的。使用WordPress 2014主题的php页面。因此,在“主页”内容区域,我有一个快捷码,它生成第二个循环,从一个名为“特色”的类别中获取3篇文章。 $featured = new WP_Query( array( \