WordPress:在Yoast中添加自定义用户/作者站点地图

问题描述

我想为特定用户角色创建自定义的Yoast XML网站地图。就我而言,角色是“供应商”。

在遇到以下示例函数之前,我尝试了很多代码片段和代码https://gist.github.com/leepowers/2b32f734571cbeadc811b93501cfcced

这是完整的代码

/**
 * USAGE:
 * - Search and replace the `CUSTOM_SITEMAP` string with a lowercase identifier name: e.g.,"vehicles","customer_profiles"
 * - The `your_alternate_data_source()` function does not exist; it's simply a placeholder for your own function that returns some sort of data array. 
 * - Uses heredocs for inline XML: https://www.PHP.net/manual/en/language.types.string.PHP#language.types.string.Syntax.heredoc
 */

/**
 * Uncomment the next line of code to disable sitemap caching.
 * - For development environments and debugging only. 
 * - All other scenarios,follow the "Manual Sitemap Update" instructions in the Yoast documentation:
 *   - https://yoast.com/help/sitemap-does-not-update/#manual
 */
#add_filter("wpSEO_enable_xml_sitemap_transient_caching","__return_false");

/**
 * Add CUSTOM_SITEMAP-sitemap.xml to Yoast sitemap index
 */
function CUSTOM_SITEMAP_sitemap_index($sitemap_index) {
    global $wpSEO_sitemaps;
    $sitemap_url = home_url("CUSTOM_SITEMAP-sitemap.xml");
    $sitemap_date = date(DATE_W3C);  # Current date and time in sitemap format.
    $custom_sitemap = <<<SITEMAP_INDEX_ENTRY
<sitemap>
    <loc>%s</loc>
    <lastmod>%s</lastmod>
</sitemap>
SITEMAP_INDEX_ENTRY;
    $sitemap_index .= sprintf($custom_sitemap,$sitemap_url,$sitemap_date);
    return $sitemap_index;
}
add_filter("wpSEO_sitemap_index","CUSTOM_SITEMAP_sitemap_index");

/**
 * Register CUSTOM_SITEMAP sitemap with Yoast
 */
function CUSTOM_SITEMAP_sitemap_register() {
    global $wpSEO_sitemaps;
    if (isset($wpSEO_sitemaps) && !empty($wpSEO_sitemaps)) {
        $wpSEO_sitemaps->register_sitemap("CUSTOM_SITEMAP","CUSTOM_SITEMAP_sitemap_generate");
    }
}
add_action("init","CUSTOM_SITEMAP_sitemap_register");

/**
 * Generate CUSTOM_SITEMAP sitemap XML body
 */
function CUSTOM_SITEMAP_sitemap_generate() {
    global $wpSEO_sitemaps;
    $data = your_alternate_data_source();  # Replace this with your own data source function
    $urls = array();
    foreach ($data as $item) {
        $urls[]= $wpSEO_sitemaps->renderer->sitemap_url(array(
            "mod" => $item->date,# <lastmod></lastmod>
            "loc" => $item->url,# <loc></loc>
            "images" => array(  
                array(  # <image:image></image:image>
                    "src" => $item->image_url,# <image:loc></image:loc>
                    "title" => $item->image_title,# <image:title></image:title>
                    "alt" => $item->image_title,# <image:caption></image:caption>
                ),),));
    }
    $sitemap_body = <<<SITEMAP_BODY
<urlset
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd"
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
%s
</urlset>
SITEMAP_BODY;
    $sitemap = sprintf($sitemap_body,implode("\n",$urls));
    $wpSEO_sitemaps->set_sitemap($sitemap);
}

对我来说,看起来很有希望。

我需要用作者列表替换一行:

$data = your_alternate_data_source();  # Replace this with your own data source function

我已经有一个功能,可以给我所有供应商的清单。但是我不知道如何将它们存储为$data

这是我的用户(供应商)列表代码

$display_admins = false;
$order_by       = 'Meta_value';
$role           = 'vendor';
$hide_empty     = true;

$admins = get_users('role=administrator');
$exclude = array();
foreach($admins as $ad) {
    $exclude[] = $ad->ID;
}
$exclude = implode(',',$exclude);
$vendorusers = get_users('Meta_key=pv_shop_name&exclude='.$exclude.'&orderby='.$order_by.'&role='.$role);

$authors = array();
foreach ($vendorusers as $vendoruser) {
    $user = get_userdata($vendoruser->ID);
    if(!empty($hide_empty)) {
        $numposts = count_user_posts($user->ID,'product');
        if($numposts < 1) continue;
    }
    $authors[] = (array) $user;
}

foreach($authors as $author) {

    $vendor_shop_link           = WCV_vendors::get_vendor_shop_page( $author['ID'] );
    $vendor_shop_url            = get_the_author_Meta( 'url',$author['ID'] );

    // This is the link
    echo $vendor_shop_link;
}

站点地图的链接存储在$vendor_shop_link中,并且与站点地图功能中的$item->url相同。

我不确定如何获得$item->date所需的用户的上次修改日期。但是我想我会弄清楚的。

我的问题是如何结合这两个功能。 如何将我的用户/供应商列表存储为$data

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...