也想出现在这里?联系我们

如何跳过WordPress中相邻帖子的某些文章链接

2023.12.17 wordpress教程
  • 文章介绍
  • 升级版本
  • 评价&建议

要在WordPress中跳过相邻帖子中的某些文章链接,您可以使用以下方法:

  1. 使用自定义查询(Custom Query):
    在您的主题文件中,您可以使用自定义WP_Query来检索相邻帖子,并排除您不希望在链接中显示的文章。以下是一个示例代码片段:

    $args = array(
       'post_type' => 'post',
       'posts_per_page' => 2, // 获取相邻的两篇文章
    );
    
    $query = new WP_Query($args);
    
    while ($query>have_posts()) {
       $query>the_post();
       // 检查文章是否要排除
       if (!in_array(get_the_ID(), $excluded_post_ids)) {
           // 显示文章链接或内容
       }
    }
    
    wp_reset_postdata();

    在上面的代码中,您需要定义一个 $excluded_post_ids 数组,其中包含您想要跳过的文章的ID。

  2. 使用WordPress钩子(Hooks):
    您可以使用WordPress的钩子来修改相邻帖子链接的输出。在您的主题的functions.php文件中添加以下代码:

    function exclude_adjacent_posts_links($output, $format, $link, $post, $adjacent) {
       // 在这里检查是否需要排除某些文章
       if (in_array($post>ID, $excluded_post_ids)) {
           return '';
       }
       return $output;
    }
    
    add_filter('next_post_link', 'exclude_adjacent_posts_links', 10, 5);
    add_filter('previous_post_link', 'exclude_adjacent_posts_links', 10, 5);

    这会通过过滤器函数 exclude_adjacent_posts_links 来检查是否要排除某些文章链接。

请记得在代码中适当地设置$excluded_post_ids,以指定您想要排除的文章的ID。此外,如果不熟悉WordPress主题文件的修改,请务必备份您的主题文件,以免出现错误。

有用0
  • 2023.12.17初次和大家见面了!

等待您对该主题的建议

发表评论

还能输入240个字

Hi, 欢迎加入Wordpress技术交流群,带你装逼带你飞!

我要入群
也想出现在这里?联系我们
wordpress加速

我来推荐一个更牛逼的给你看看?

  • 猛戳我吧