我用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();
但这不会显示帖子模板。实际上,不会开始执行循环。你知道发生了什么事,或者有什么更简单的方法来实现这一点吗?