自定义 WP_Sitemaps_Provider 站点地图页面加载最后一篇博客文章

我已经更新到 Wordpress 5.5,并希望从安装中删除 Yoast,因为它几乎只用于站点地图,但是需要根据不同的帖子类型创建几个自定义站点地图,我目前正在使用 Yoast 进行此操作。

我添加了一个自定义提供程序,如下所示,它覆盖了两个所需的抽象函数。这两个都正常工作,并且新条目正在添加到位于 wp-sitemap.xml 的站点地图索引中

但是,当单击 /wp-sitemap-range-1.xml 时,我得到了网站上最新博客文章的解决方案,而不是包含三种帖子类型的预期网站地图。

我在 Wordpress API 规范或法典中找不到任何文档,所以目前有点不知所措 - 感谢任何帮助。与示例工作提供商的链接也将不胜感激,因为我已经进行了很长时间的搜索,试图找到一些没有运气的东西。

我的下一步是检查所有 404 处理程序并重写主题中的处理程序,看看是否有任何内容将其发送到错误的位置。我要生成更复杂的站点地图,但希望这三种帖子类型的简单聚合首先起作用。

<?php

//Add provider for post types 'cast', 'keg', 'cider' to create a sitemap called 'range'

add_action('init', function() {

  $rangeProvider = new key_sitemapProvider('range', array('cask', 'keg', 'cider'));

  wp_register_sitemap_provider('pmrs-range', $rangeProvider);

});


/*---------------------*/

class key_sitemapProvider extends WP_Sitemaps_Provider {  

  public $postTypes = array();


  /*---------------------*/

  public function __construct($name, $postTypes) {

    $this->name        = $name;

    $this->postTypes   = $postTypes;    

    $this->object_type = 'post';

  }


  /*---------------------*/

  private function queryArgs(){

    return array(

      'post_type'      => $this->postTypes, 

      'post_status'    => 'publish',

      'posts_per_page' => -1,

      'orderby'        => 'post_date',

      'order'          => 'DESC'

    );

  }


  /*--OVERRIDE-----------*/

  public function get_url_list($page_num, $post_type = '') {

    $query = new WP_Query($this->queryArgs());

    $urlList = array();     


    foreach($query->posts as $post) {

      $sitemapEntry = array(

        'chf' => 'weekly',

        'pri' => 1.0,

        'loc' => get_permalink($post),

        'mod' => get_the_modified_time('Y-m-d H:i:s', $post)

      );

      

      $sitemapEntry = apply_filters('wp_sitemaps_posts_entry', $sitemapEntry, $post, $post_type);

      $urlList[] = $sitemapEntry;

    }


    return $urlList;

  }



解决方案

感谢马特·贾沃斯基(Matt Jaworski)在下面的回答,找到了一种让它发挥作用的方法,但还有其他一些问题。

  • WP_Sitemaps_Provider 扩展的名称属性需要与您在 Wordpress 中注册的名称相匹配。另外,这不应该包含任何特殊字符

  • 站点地图条目中支持的值有:changefreq、priority、loc 和 lastmod。这与我之前的缩短版本相反。


一只萌萌小番薯
浏览 54回答 1
1回答

繁花如伊

我遇到了同样的问题,并且文档根本不存在。通过几次试验和错误,我发现 WordPress 很可能不喜欢名称中的任何特殊字符。就我而言,替换community-posts为communitypostshelp。这是我们现在正在研究的非常粗略(但有效)的概念证明:class PeepSo3_Sitemap_Provider extends WP_Sitemaps_Provider {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private $limit = 10; // @TODO CONFIGURABLE&nbsp; &nbsp; public function __construct() {&nbsp; &nbsp; &nbsp; &nbsp; $this->name&nbsp; &nbsp; &nbsp; &nbsp; = 'communityposts';&nbsp; &nbsp; &nbsp; &nbsp; $this->object_type = 'communityposts';&nbsp; &nbsp; }&nbsp; &nbsp; private function sql($page_num) {&nbsp; &nbsp; &nbsp; &nbsp; $sql =""; // your queries here;&nbsp; &nbsp; &nbsp; &nbsp; return $wpdb->get_results($sql);&nbsp; &nbsp; }&nbsp; &nbsp; // retrieve a page of results&nbsp; &nbsp; public function get_url_list( $page_num, $object_subtype = '' ) {&nbsp; &nbsp; &nbsp; &nbsp; $url_list = [];&nbsp; &nbsp; &nbsp; &nbsp; $posts = $this->sql($page_num);&nbsp; &nbsp; &nbsp; &nbsp; foreach($posts as $post)&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url_list[] = ['loc' => $post->url; // depends on your item structure&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $url_list;&nbsp; &nbsp; }&nbsp; &nbsp; // estimate how many pages are available&nbsp; &nbsp; public function get_max_num_pages( $object_subtype = '' ) {&nbsp; &nbsp; &nbsp; &nbsp; $posts = $this->sql(-1);&nbsp; &nbsp; &nbsp; &nbsp; return ceil($posts[0]->count_posts/$this->limit);&nbsp; &nbsp; }}// Register XML Sitemap Provideradd_filter('init', function() {&nbsp; &nbsp; $provider = new PeepSo3_Sitemap_Provider();&nbsp; &nbsp; wp_register_sitemap_provider( 'communityposts', $provider );});
打开App,查看更多内容
随时随地看视频慕课网APP