- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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)。
最佳答案
我知道这个问题已经得到解答,但我需要一种更动态的方法来解释 @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' );
关于php - Wordpress Yoast 从 XML 站点地图中排除帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44530358/
我想从我的 Android 应用程序发布帖子并插入到我的数据库中。我的第一个方法是从我的应用程序发送帖子并仅显示值,但它不起作用。 我的应用程序代码是 public void postData() {
我在谷歌上进行了长时间的搜索,试图找到解决这个问题的方法...我正在创建一个 cv 管理器主题,使用 WordPress 安装来控制内容。我已经设法按类别组织所有 WP 帖子,但也想在年份分组中列出这
获取数据:{ error: 'invalid_request', error_description: 'Missing grant type' } } Content-Type 是正确的,不知道哪里
我试图访问我的路由“posts.js”,但是当我启动服务器并连接到 localhost:5000/posts 时。此错误显示为“无法获取/发布” 代码:服务器/路由/posts.js import e
是否有任何可能的方法可以按标题对新的 WordPress 帖子查询进行排序,但按数字而不是按字母顺序排序? 我有一些标题,它们按字母顺序有很多相同的名称,然后有一个数字后记,所以当然,例如 Wordp
我有一个 WCF RESTFul 服务,声明如下: [ServiceContract] public interface IGasPriceService { [OperationContra
我希望创建一个网站,允许用户创建群组,然后在这些群组内聊天/发帖。但是,当在组内发帖/聊天时,我不希望用户必须重新加载页面才能查看该组内的这些新帖子/聊天。我的问题归结为:您对如何做到这一点(语言、网
我们有一个 Android 应用程序,通过无状态 JSON 协议(protocol)与 php/MySQL 服务器通信。 用户已登录应用并拥有相应的用户 ID。 应用根据请求从服务器接收项目/帖子列表
我正在尝试找出帖子、评论和对评论的回复的架构,其中回复只有单级(没有回复回复)。 帖子: 1) id 2) user_id 3) contents 4) privacy 评论: 1) id 2) us
我正在使用 YITH Woocommerce 订阅的免费版本,让我的 Wordpress 网站的用户能够在订阅的基础上购买产品。当用户购买订阅时,会发生几件事。为订单创建了一个新帖子,为订单创建了一个
在我之前的项目中,我将帖子和评论作为两个表: 发布 编号 正文 时间戳 用户名 评论 编号 留言 时间戳 用户名 zip 现在我必须设计对评论的回复。回复只有一级,所以用户只能回复评论,不能回复。树结
在不添加任何标签或类别的情况下,我需要一种方法来生成一个页面,该页面列出所有包含单词的 Wordpress 帖子,例如,其中某处包含“设计”。有谁知道如何做到这一点? 最佳答案 您可以使用 WP_Qu
我正在使用 $routeProvider 设置一条类似 的路线 when('/grab/:param1/:param2', { controller: 'someController',
我正在尝试使用 K6 加载测试 prometheus pushgateway,它需要以下格式的帖子。 http_request_duration_seconds_bucket{le="0.05"} 2
在 DART lang 中,如何指定 POST 请求 Content-Type 为 multipart/form-data 我的 DART 代码是: sendDatas(dynamic data) {
我有一个功能可以在 2014-11-01 和 2015-10-31 之间抓取比特币 subreddit 中的所有帖子。 但是,我只能提取到 10 月 25 日为止的大约 990 个帖子。我不明白发生了
如何遍历 Jekyll 站点帖子,但仅对年份等于特定值的帖子采取行动? {% for post in site.posts %} {% if post.date.year == 2012 %}
我想在一个页面上显示所有 Wordpress 帖子,并让结果显示如下示例: 9 月(当月)的帖子 1- 第一篇文章2-秒发帖3- 第三个帖子 下个月的帖子 2- 第一篇文章2-秒发帖3- 第三个帖子
Recent posts {% for post in site.posts %} » {{ post.title }} {% endfor %}
我想在 WordPress 的页面中显示所有最近的 WordPress 帖子。我尝试了一些插件,但运气不佳。我只想显示最后 10 篇帖子的标题和摘录。有人能指出我正确的方向吗? 感谢任何帮助。 谢谢,
我是一名优秀的程序员,十分优秀!