【问题标题】:WordPress self-developed AMP - edit Query if URL is homepage.com/amp to get front-page postWordPress 自研 AMP - 编辑 Query if URL is homepage.com/amp 获取首页帖子
【发布时间】: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

【问题讨论】:

    标签: php wordpress amp-html


    【解决方案1】:

    当然它会返回 404 not found 因为 amp 页面或 amp 帖子不存在。

    我的想法是有另一种方法可以通过为您的网站添加自定义端点来返回有效的 url

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

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

    add_rewrite_endpoint( 'amp', EP_ALL );
    

    然后保存永久链接,然后尝试访问您网站的任何网址,然后是 /amp 示例:homepage.com/amp、homepage.com/single-post/amp

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      花了几天时间研究 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; ?>
      

      【讨论】:

        猜你喜欢
        • 2018-08-17
        • 1970-01-01
        • 1970-01-01
        • 2022-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多