You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中如何获取与当前页面URL结构相似的页面列表?

获取同路径结构页面列表的解决方案

问题出在哪

你之前的代码用pagename去匹配路径片段是错误的——pagename仅对应单个页面的slug,无法识别URL里的层级路径,所以查不到正确结果。

推荐方法:利用页面层级查询(高效)

如果你的页面是按层级创建的(比如sweet-fruits是fruits的子页面,mango又是sweet-fruits的子页面),直接通过父页面ID查询所有子页面最靠谱:

function get_list($atts) {
    $atts = shortcode_atts(
        array(),
        $atts,
        'fruit_pages'
    );

    // 获取当前页面对象
    global $post;
    if (!$post || $post->post_type !== 'page') {
        return 'No pages found.';
    }

    // 解析当前页面URL,提取父路径对应的页面
    $current_permalink = get_permalink($post);
    $path = parse_url($current_permalink, PHP_URL_PATH);
    $path_parts = array_filter(explode('/', $path));
    
    // 针对示例URL,取前两个路径片段(fruits/sweet-fruits)查找对应父页面
    if (count($path_parts) >= 2) {
        $parent_page_path = implode('/', array_slice($path_parts, 0, 2));
        $parent_page = get_page_by_path($parent_page_path);
        if (!$parent_page) {
            return 'No pages found.';
        }
        $parent_id = $parent_page->ID;
    } else {
        return 'No pages found.';
    }

    // 查询该父页面下的所有子页面
    $fruit_pages_query = new WP_Query(
        array(
            'post_type' => 'page',
            'posts_per_page' => -1,
            'post_parent' => $parent_id,
            'orderby' => 'title',
            'order' => 'ASC'
        )
    );

    if ($fruit_pages_query->have_posts()) {
        $output = '<ul>';
        while ($fruit_pages_query->have_posts()) {
            $fruit_pages_query->the_post();
            // 用esc_url和esc_html防止XSS
            $output .= '<li><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></li>';
        }
        $output .= '</ul>';

        wp_reset_postdata();
        return $output;
    } else {
        return 'No pages found.';
    }
}
add_shortcode('fruit_pages', 'get_list');

备选方法:直接匹配URL路径(灵活)

如果页面不是严格层级关系,只是URL包含特定片段,直接过滤页面的永久链接即可:

function get_list($atts) {
    $atts = shortcode_atts(
        array(),
        $atts,
        'fruit_pages'
    );

    $current_permalink = get_permalink();
    $path = parse_url($current_permalink, PHP_URL_PATH);
    
    // 提取目标路径片段,比如/fruits/sweet-fruits/
    preg_match('#/fruits/[^/]+/#', $path, $matches);
    if (empty($matches[0])) {
        return 'No pages found.';
    }
    $target_path = $matches[0];

    // 查询所有已发布页面
    $fruit_pages_query = new WP_Query(
        array(
            'post_type' => 'page',
            'posts_per_page' => -1,
            'post_status' => 'publish'
        )
    );

    if ($fruit_pages_query->have_posts()) {
        $output = '<ul>';
        while ($fruit_pages_query->have_posts()) {
            $fruit_pages_query->the_post();
            $page_permalink = get_permalink();
            // 检查当前页面链接是否包含目标路径
            if (strpos($page_permalink, $target_path) !== false) {
                $output .= '<li><a href="' . esc_url($page_permalink) . '">' . esc_html(get_the_title()) . '</a></li>';
            }
        }
        $output .= '</ul>';

        wp_reset_postdata();
        
        // 过滤后无结果的话返回提示
        if (strpos($output, '<li>') === false) {
            return 'No pages found.';
        }
        return $output;
    } else {
        return 'No pages found.';
    }
}
add_shortcode('fruit_pages', 'get_list');

重要提醒

  • 两个方法都加了esc_url和esc_html,这是WordPress开发必须的安全措施,防止XSS攻击。
  • 层级查询法效率更高,直接从数据库筛选;路径匹配法更灵活,但页面多的时候会慢一点,根据你的实际情况选择。
  • 如果用了自定义帖子类型或自定义URL规则,记得调整post_type参数或者查询条件。

内容的提问来源于stack exchange,提问作者Sau Thapa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 14:36:06