关键词:
【中文标题】Wordpress Yoast 从 XML 站点地图中排除帖子【英文标题】:Wordpress Yoast exclude posts from XML sitemap 【发布时间】:2017-11-15 18:07:14 【问题描述】:我正在使用 Yoast SEO 插件,我正在尝试使用“wpseo_sitemap_entry”过滤器从帖子 XML 站点地图中手动排除帖子,但到目前为止还没有运气。
这是我当前的代码:
function sitemap_exclude_post( $url, $type, $post)
if($post->ID == 6298 )
return false;
return $url;
add_filter( 'wpseo_sitemap_entry', 'sitemap_exclude_post', 1, 3 );
任何建议将不胜感激?
注意:我知道这可以通过 Yoast 插件的后端手动输入帖子 ID 来完成,但我需要通过过滤器来完成。上述代码稍后将进一步更改,以自动从 wordpress 中的类别中获取帖子的帖子 ID(single.php)。
【问题讨论】:
您的解决方案应该对我有用,false
是向 Yoast 展示不包含链接的正确方法。你确定身份证是对的吗?您可以尝试更改add_filter
中的优先级,但我认为这不会改变任何事情。
感谢您的检查。是的,身份证是正确的。代码现在可以工作了,我只是按下了 Yoast 仪表板“排除帖子”部分中的“保存更改”按钮,它就可以工作了。不知道怎么做,但它正在做这项工作。
【参考方案1】:
我知道这个问题已经得到解答,但我需要一种更动态的方法来解释@clodclod91 的解释,因为我不止一次收到这个问题,所以我写了一些关于如何以“更流畅的方式”做到最好的代码而不是硬编码任何值。
我希望它可以帮助一些人。
我还写了一篇解释更多的帖子。阅读here。
/**
* Set all excluded items for sitemap in a transient
*
* @return mixed
*/
function beee_get_sitemap_excludes_transient()
// get transient
$output = get_transient( 'beee_exclude_sitemap' );
// if transient returns false, create it
if ( false == $output )
$exclude_args = array(
'post_type' => [ 'page' ], // change this to the post types you want excluded
'posts_per_page' => -1, // all items of course
'meta_query' => array(
array(
'key' => 'beee_exclude_sitemap',
'value' => '1',
'compare' => '=',
),
),
'orderby' => 'ID', // recommend to set to ID so 'the order is easier to compare later on', otherwise it can create issues
'order' => 'ASC', // same, just sort ASC to avoid issues
);
$excluded_items = get_posts( $exclude_args );
if ( count( $excluded_items ) > 0 )
// if there are items and create an empty array
$exclude = [];
foreach( $excluded_items as $item )
// add post id to array
$exclude[] = $item->ID;
// create a string from an array since Yoast stores a string, not an array
$output = implode( ',', $exclude );
// set transient with a 24-hour expiration
set_transient( 'beee_exclude_sitemap', $output, 24 * HOUR_IN_SECONDS );
return $output;
/**
* Set Yoast settings from transient
*/
function beee_exclude_pages_sitemap( $post_id )
// get our excluded items from transient
$our_excluded_posts = beee_get_sitemap_excludes_transient();
// check if 'exclude' field has been set (in the post)
if ( 1 == get_field( 'beee_exclude_sitemap', $post_id ) )
// if yes, check if it exists in transient
if ( in_array( $post_id, explode( ',', $our_excluded_posts ) ) )
// yes, it's already included so we don't have to do anything
else
// no, it's not included so it needs to be added
// so we delete the transient (existing values)
delete_transient( 'beee_exclude_sitemap' );
else
// it has not been set in the post
if ( in_array( $post_id, explode( ',', $our_excluded_posts ) ) )
// if does exists in our stored transient, which it shouldn't be in there
// so we delete the transient (existing values)
delete_transient( 'beee_exclude_sitemap' );
// get our excluded ids from transient
// since we just cleared it, the transient doesn't exist and is retrieved from scratch
$our_excluded_posts = beee_get_sitemap_excludes_transient();
// get post ids from Yoast settings
$wpseo_xml = get_option( 'wpseo_xml' );
// compare Yoast's value with our items
if ( $wpseo_xml[ 'excluded-posts' ] == $our_excluded_posts )
// they are the same so we don't need to do anything
else
// they are not the same so we need to update the database with our new ids
update_option( 'wpseo_xml', $wpseo_xml );
add_action( 'save_post', 'beee_exclude_pages_sitemap' );
【讨论】:
【参考方案2】:可以使用update_option函数。
$array = array(
"excluded-posts" => 6298
);
update_option('wpseo_xml', $array);
应该也可以在 add_filter 钩子中工作。 update_option 是一个 wp 函数,用于在表选项中插入或更新。 Yoast SEO for XML Sitemap 的 wp_options 表中的键是 wpseo_xml。 在值中有一个包含其他信息的数组:
a:18:
s: 22: "disable_author_sitemap";
b: 1;
s: 22: "disable_author_noposts";
b: 1;
s: 16: "enablexmlsitemap";
b: 1;
s: 16: "entries-per-page";
i: 1000;
s: 14: "excluded-posts";
s: 7: "1,2,3,4";
s: 38: "user_role-administrator-not_in_sitemap";
b: 0;
s: 31: "user_role-editor-not_in_sitemap";
b: 0;
s: 31: "user_role-author-not_in_sitemap";
b: 0;
s: 36: "user_role-contributor-not_in_sitemap";
b: 0;
s: 35: "user_role-subscriber-not_in_sitemap";
b: 0;
s: 36: "user_role-authors_tes-not_in_sitemap";
b: 0;
s: 40: "user_role-authors_academy-not_in_sitemap";
b: 0;
s: 30: "post_types-post-not_in_sitemap";
b: 0;
s: 30: "post_types-page-not_in_sitemap";
b: 0;
s: 36: "post_types-attachment-not_in_sitemap";
b: 1;
s: 34: "taxonomies-category-not_in_sitemap";
b: 0;
s: 34: "taxonomies-post_tag-not_in_sitemap";
b: 0;
s: 37: "taxonomies-post_format-not_in_sitemap";
b: 0;
【讨论】:
【参考方案3】:我在问题中发布的上述代码工作正常。
我刚刚按下 Yoast 仪表板“排除帖子”部分中的“保存更改”按钮,它就起作用了。不知道如何,但它正在工作。
【讨论】:
【参考方案4】:您可以从 Yoast 管理界面轻松完成:
https://kb.yoast.com/kb/sitemap-shows-excluded-posts-pages/
干杯, 弗朗切斯科
【讨论】:
感谢您的回复。我知道这一点,但就我而言,这需要通过代码来完成。修改 Wordpress 搜索功能以配合 Yoast SEO
】修改Wordpress搜索功能以配合YoastSEO【英文标题】:ModifyingWordpresssearchfunctiontocoordinatewithYoastSEO【发布时间】:2013-12-0722:08:46【问题描述】:我正在尝试为包含大量帖子的WordPress网站整合搜索功能。每个帖子标题都是一个随机数... 查看详情
将页码添加到分页页面上的元标题 Wordpress、Yoast
】将页码添加到分页页面上的元标题Wordpress、Yoast【英文标题】:AddpagenumbertometatitleonpaginatedpagesWordpress,Yoast【发布时间】:2017-06-0603:14:01【问题描述】:我知道这种问题以前被问过很多次,我已经阅读了网上所有的帖子,没有一... 查看详情
Yoast Plugin for WordPress 使用 Google Analytics 跟踪自定义事件
】YoastPluginforWordPress使用GoogleAnalytics跟踪自定义事件【英文标题】:TrackingcustomeventwithGoogleAnalyticsbyYoastPluginforWordPress【发布时间】:2016-01-0710:11:37【问题描述】:大家好,谁能帮助我了解如何在使用YoastAnalyticsplugin时进行自定义... 查看详情
站点搭建从零開始wordpress站点的完好
1、WordPress站点前后端经常使用语言简单介绍和执行过程通常一个站点的整个构建过程中须要大量的技术支持,尤其是用到非常多种计算机语言。站点的构建主要分后端和前端两部分,后端代码在server上执行。而前端程序源代码... 查看详情
使 WordPress 站点可以从 LAN 内部和外部访问
】使WordPress站点可以从LAN内部和外部访问【英文标题】:MakingaWordPresssiteaccesiblefrominsideLANandoutsideit【发布时间】:2015-04-2507:47:45【问题描述】:我在局域网内的PC上配置了WordPress。因此,我可以使用localhost/WordPress从同一台计算机... 查看详情
wordpress免插件生成完整站点地图(sitemap.xml)的php代码
让这个代码更加完善,可以同时生成首页、文章、单页面、分类和标签的sitemap!一、PHP代码<?phprequire(‘./wp-blog-header.php‘);header("Content-type:text/xml");header(‘HTTP/1.1200OK‘);$posts_to_show=1000;echo‘<?xmlversion="1.0"encoding="U 查看详情
Wordpress:将多站点从服务器移动到本地主机
】Wordpress:将多站点从服务器移动到本地主机【英文标题】:Wordpress:MoveMultisitefromservertolocalhost【发布时间】:2015-01-0717:45:08【问题描述】:我正在尝试将已部署的WordPress多站点移动到本地环境。我备份了服务器数据库和文件。... 查看详情
从外部 API RapidApi 获取数据到 Wordpress 站点
】从外部APIRapidApi获取数据到Wordpress站点【英文标题】:FetchingdatafromexternalAPIRapidApitoWordpresssite【发布时间】:2019-07-1900:32:22【问题描述】:我正在尝试从此API获取数据:https://rapidapi.com/apilayernet/api/rest-countries-v1?endpoint=53aa5a08e4b0a... 查看详情
站点搭建从零開始wordpress的安装
前面说了非常多废话。如今最终转到正题。WordPress的安装。1、WordPress安装非常easy假设你的server能通过应用中心一键安装WordPress,这一节就非常轻松了,基本上不须要做什么。可是考虑到那种新手的方式,以后假设换了server可能... 查看详情
将 wordpress 站点从一个本地主机转移到另一个本地主机
】将wordpress站点从一个本地主机转移到另一个本地主机【英文标题】:transferingwordpresssiteformonelocalhosttoanotherlocalhost【发布时间】:2014-04-2702:04:26【问题描述】:我正在尝试将wp站点从我的工作计算机上的localhost移动到我的个人计... 查看详情
将 wordpress 站点从实时设置到本地,URL 不起作用
】将wordpress站点从实时设置到本地,URL不起作用【英文标题】:Setupwordpresssitefromlivetolocal,URLsarenotworking【发布时间】:2018-09-0519:47:27【问题描述】:3/26:我在实时服务器上创建了Wordpress网站。我下载了一个副本到我的XAMPP服务器... 查看详情
从 localhost 开发站点时更改了 WordPress 地址 (URL) 和站点地址 (URL)。现在,建立数据库连接时出错
】从localhost开发站点时更改了WordPress地址(URL)和站点地址(URL)。现在,建立数据库连接时出错【英文标题】:ChangedWordPressAddress(URL)andSiteAddress(URL)whendevelopingsitefromlocalhost.Now,errorestablishingadatabaseconnection【发布时间】:2020-09-2715:46:29... 查看详情
WordPress 多站点上的网络范围的 Cron 调度
】WordPress多站点上的网络范围的Cron调度【英文标题】:NetworkWideCronSchedulingonWordpressMultisite【发布时间】:2013-02-2311:36:47【问题描述】:我正在使用WPCron从插件安排网络范围的cron。无论从哪个博客激活,我都想每小时运行一次cron... 查看详情
从本地服务器传输后,Wordpress 站点不起作用(数据库问题?)
】从本地服务器传输后,Wordpress站点不起作用(数据库问题?)【英文标题】:WordpresssItedoesn\'tworkaftertransferfromlocalserver(databaseissue?)【发布时间】:2012-05-1408:39:40【问题描述】:我使用MAMP在本地开发了一个自定义woocommerce商店的... 查看详情
将 WordPress 插件集成到引导站点中
】将WordPress插件集成到引导站点中【英文标题】:IntergratingWordPressPluginsintoabootstrapsite【发布时间】:2021-06-0314:34:40【问题描述】:如果我询问有关bootstrap和WordPress的一般性问题,希望您不介意。我只使用引导程序创建了一个网... 查看详情
将 WordPress 从 WAMP 上传到 GoDaddy 时出现数据库错误(无法在本地访问站点)
】将WordPress从WAMP上传到GoDaddy时出现数据库错误(无法在本地访问站点)【英文标题】:DatabaseerrorwhenuploadingWordPressfromWAMPtoGoDaddy(cannotaccesssitelocally)【发布时间】:2016-10-2712:18:37【问题描述】:我绝不是任何专家,我真的希望这... 查看详情
php从yoast插件获取社交网络链接(代码片段)
尝试使用从另一个站点的 wordpress 数据库中动态获取的数据在新网页中创建菜单
】尝试使用从另一个站点的wordpress数据库中动态获取的数据在新网页中创建菜单【英文标题】:Tryingtocreateamenuinnewwebpageusingdatafetchedfromwordpressdatabaseofanothersitedynamically【发布时间】:2019-06-2616:05:06【问题描述】:我正在尝试使用... 查看详情