首页 > 解决方案 > 从 Jetpack 的“热门帖子和页面”小部件中排除页面

问题描述

我有以下情况。(带有 Jetpack 的 WordPress)

某些“自定义”页面仅在提供数据(例如搜索词)时才显示某些内容。如果没有,页面通常是“空白的”。当它们被访问时(通过包括查询变量的链接),它们被计入统计数据中。但是...如果您在“热门帖子和页面”(小部件)下单击它们,它们只是空白。

有没有办法不在“热门帖子和页面”下列出这些特定页面?或者我可以将统计信息从子页面重定向到父页面吗?

提前致谢

标签: wordpresswidgetjetpack

解决方案


您可以挂钩jetpack_widget_get_top_posts以从热门帖子和页面小部件中排除这些页面。

将以下代码添加到主题的 functions.php 文件中:

function wp653886_exclude_from_top_posts( $posts, $post_ids, $count ) {

    $page_ids_to_exclude = array( 144, 764, 876 ); // Put here the IDs of the pages you wish to exclude.

    foreach ( $posts as $k => $post ) {
        // Remove this item from the list
        if ( in_array( $post['post_id'],  $page_ids_to_exclude ) ) {
            unset( $posts[$k] );
        }
    }

    return $posts;

}
add_filter( 'jetpack_widget_get_top_posts', 'wp653886_exclude_from_top_posts', 10, 3 );

编辑$page_ids_to_exclude数组以添加要从小部件中排除的页面的 ID,一切顺利。


推荐阅读