WordPress self-developed AMP - 如果 URL 是主页,请编辑查询。com/amp 以获得 front-page post

WordPress self-developed AMP - edit Query if URL is homepage.com/amp to get front-page post

我不确定我做错了什么,过去几天一直在研究但没有希望。

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; ?>

目标 URL:主页。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' ); 

当前结果:

当然会 return 404 not found 因为 amp 页面或 amp post 不存在。

我的想法是,有一种替代方法可以通过为您的网站添加自定义端点 return 到有效 url

参考:https://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint

将以下代码添加到您的主题函数中

add_rewrite_endpoint( 'amp', EP_ALL );

然后保存永久链接,然后尝试访问您网站的任何 url,然后是 /amp 示例:主页。com/amp,主页。com/single-post/amp

希望对您有所帮助

花了几天时间研究 WP_Query 和 pre_get_posts 的工作原理,终于得到答案

/*
 * if user is accessing the not existing page homepage.com/amp/
 * set the query to get the frontpage post
 */
function front_page_post_AMP( $query ) {

    // Only noop the main query
    if ( ! $query->is_main_query() ) {
        return;
    }

    /*
    * check if endpoint is AMP
    * check if the array count is 1 (means no parameters, and most likely will result in 404)
    */
    if ( is_amp_endpoint() && 
         count( $query->query ) == 1 && 
         ! isset( $query->query['page'] ) ) 
    {
        // 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";

        /* 
         * If current URL is homepage.com/amp
         * set the empty query to get the front page post
         */
        if ( strcasecmp( $current_url, $front_page_amp_url ) == 0 ) {

            $front_page_id = get_option( 'page_on_front' );
            $query->set( 'page_id', $front_page_id );
            $query->set( 'post_type', "page" );
        }

    }

    return;
}
add_action( 'pre_get_posts', 'front_page_post_AMP' );

使用此解决方案,您可以使用自然的方式调用页面模板上的标题和内容,如下所示

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php echo the_content(); ?>
<?php endwhile; endif; ?>