【发布时间】:2017-05-18 00:24:14
【问题描述】:
我不确定我做错了什么,过去几天一直在研究但没有希望。
WordPress 设置:静态页面:主页
目标: 替换查询,因此需要所有首页数据,包括以下内容:
- 首页发布内容
- 首页标题
- 首页元数据
并通过调用主题页面模板以自然的方式显示内容、标题、元数据
// Takes front page title
<?php wp_title(); ?>
// Takes front page config
<?php wp_head(); ?>
// takes front page post and display content
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo the_content(); ?>
<?php endwhile; endif; ?>
目标网址:homepage.com/amp(非现有页面) 但定义如下图
// defines AMP variable
define( 'AMP_QUERY_VAR', apply_filters( 'amp_query_var', 'amp' ) );
// enable URL endpoint rewrite for /amp
add_rewrite_endpoint( AMP_QUERY_VAR, EP_ALL );
以下当前代码中的主要问题
function front_page_post_AMP( $query ) {
// Get's the Current Browser URL
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
// Homepage AMP URL
$front_page_amp_url = get_site_url() . "/amp";
// check if the current browser URL is homepage.com/amp
if ( strcasecmp( $current_url, $front_page_amp_url ) == 0 && $query->is_main_query() )
{
// gets front page id
$front_page_id = get_option( 'page_on_front' );
// replace query id
$query->set( 'page_id', $front_page_id );
return $query;
}
}
add_action( 'pre_get_posts', 'front_page_post_AMP' );
当前结果:
- 页面标题 = 未找到
- 页面帖子内容 = 空/null
【问题讨论】: