✨ Professional WordPress development, custom website builder, efficient on-line, cooperate and enjoy optimization services! 🚀

WordPress single page menu manually fill in the ID of the parent page to automatically get all the child pages under it

//手动填写父页面 ID 自动获取其下所有子页面
//[page_navigation page_id="123"]
//其中 page_id="123" 是你手动填写的父页面的 ID。替换 "123" 为你实际的父页面 ID

function get_all_child_pages($parent_page_id) {
    // 查询当前页面下所有的子页面
    $args = array(
        'post_type'   => 'page',
        'post_parent' => $parent_page_id,
        'order'       => 'ASC',            // 排序方式
        'orderby'     => 'menu_order',     // 根据菜单排序
        'posts_per_page' => -1,            // 不限制数量
    );

    $query = new WP_Query($args);
    $child_pages = array();

    // 收集所有子页面
    while ($query->have_posts()) {
        $query->the_post();
        $child_pages[] = array(
            'ID'    => get_the_ID(),
            'title' => get_the_title(),
            'url'   => get_permalink(),
        );
        
        // 如果该页面有子页面,则递归获取子页面
        $child_pages = array_merge($child_pages, get_all_child_pages(get_the_ID()));
    }

    // 重置查询
    wp_reset_postdata();

    return $child_pages;
}

function custom_page_navigation_shortcode($atts) {
    // 解析简码参数,默认 page_id 为 0
    $atts = shortcode_atts(array(
        'page_id' => 0, // 通过简码传递的页面 ID
    ), $atts);

    $page_id = intval($atts['page_id']);
    if ($page_id <= 0) {
        return '<p>请提供有效的页面 ID。</p>';
    }

    // 获取父页面的信息
    $parent_page = get_post($page_id);
    if (!$parent_page) {
        return '<p>父页面不存在。</p>';
    }

    // 获取所有子页面
    $child_pages = get_all_child_pages($page_id);

    // 构建输出结果
    $output = '<ul class="page-navigation">';
    
    // 显示父页面
    $output .= '<li class="parent-page"><span class="arrow">→</span><a href="' . esc_url(get_permalink($parent_page)) . '">' . esc_html(get_the_title($parent_page)) . '</a></li>';
    
    // 显示子页面
    if (!empty($child_pages)) {
        foreach ($child_pages as $child) {
            // 检查当前页面是否为点击的链接
            $is_current_page = (get_the_ID() === $child['ID']);
            
            // 设置不同样式的链接
            if ($is_current_page) {
                $output .= '<li class="current-page"><span class="arrow">→</span><a href="' . esc_url($child['url']) . '">' . esc_html($child['title']) . '</a></li>';
            } else {
                $output .= '<li><span class="arrow">→</span><a href="' . esc_url($child['url']) . '">' . esc_html($child['title']) . '</a></li>';
            }
        }
    } else {
        $output .= '<li>該頁面沒有子頁面。</li>';
    }

    $output .= '</ul>';

    return $output;
}
add_shortcode('page_navigation', 'custom_page_navigation_shortcode');

Instructions for use:

  1. exist functions.php or custom plugin to add this code.

  2. You can then use the following shortcode on any page

[page_navigation page_id="123"]

If directly inside the template PHP function call method, you can modify the code.

function get_all_child_pages($parent_page_id) {
    // 查询当前页面下所有的子页面
    $args = array(
        'post_type'   => 'page',
        'post_parent' => $parent_page_id,
        'order'       => 'ASC',            // 排序方式
        'orderby'     => 'menu_order',     // 根据菜单排序
        'posts_per_page' => -1,            // 不限制数量
    );

    $query = new WP_Query($args);
    $child_pages = array();

    // 收集所有子页面
    while ($query->have_posts()) {
        $query->the_post();
        $child_pages[] = array(
            'ID'    => get_the_ID(),
            'title' => get_the_title(),
            'url'   => get_permalink(),
        );
        
        // 如果该页面有子页面,则递归获取子页面
        $child_pages = array_merge($child_pages, get_all_child_pages(get_the_ID()));
    }

    // 重置查询
    wp_reset_postdata();

    return $child_pages;
}

function get_page_navigation($parent_page_id) {
    // 获取父页面的信息
    $parent_page = get_post($parent_page_id);
    if (!$parent_page) {
        return '<p>父页面不存在。</p>';
    }

    // 获取所有子页面
    $child_pages = get_all_child_pages($parent_page_id);

    // 构建输出结果
    $output = '<ul class="page-navigation">';
    
    // 显示父页面
    $output .= '<li class="parent-page"><span class="arrow">→</span><a href="' . esc_url(get_permalink($parent_page)) . '">' . esc_html(get_the_title($parent_page)) . '</a></li>';
    
    // 显示子页面
    if (!empty($child_pages)) {
        foreach ($child_pages as $child) {
            // 检查当前页面是否为点击的链接
            $is_current_page = (get_the_ID() === $child['ID']);
            
            // 设置不同样式的链接
            if ($is_current_page) {
                $output .= '<li class="current-page"><span class="arrow">→</span><a href="' . esc_url($child['url']) . '">' . esc_html($child['title']) . '</a></li>';
            } else {
                $output .= '<li><span class="arrow">→</span><a href="' . esc_url($child['url']) . '">' . esc_html($child['title']) . '</a></li>';
            }
        }
    } else {
        $output .= '<li>该页面没有子页面。</li>';
    }

    $output .= '</ul>';

    return $output;
}

How to call it in the template:

In your WordPress template file, for example page.php,single.php Or a customized template file, which you can call directly via the get_page_navigation function to display the navigation:

<?php
// 传入父页面的 ID
$parent_page_id = 123; // 替换为你实际的父页面 ID

// 调用函数并输出页面导航
echo get_page_navigation($parent_page_id);
?>

 

WordPress Support Team
WordPress Support Team

💻 A sincere and meticulous young developer 🎓 specializing in the field of custom foreign trade website development.
🌟 specializes in WordPress website design customization and full-service development. Our unique advantage is not only proficient in website development technology, but also the international mainstream art design elements 🎨 skillful integration, while developing accurate and effective brand marketing strategy 📈.
💡 Services covered:
🔍 WordPress Theme Development for a unique website visual style and user experience.
💻 WordPress website customization, tailor-made exclusive website according to your foreign trade business needs.
Whether it's website architecture construction, interface aesthetic design, or brand promotion strategy, we can provide you with one-stop quality solutions to help your foreign trade business stand out on the Internet and step into the global market 🚀.

Articles: 45

Leave a Reply

Your email address will not be published. Required fields are marked *

Submit your request

Quote Collection Form

💥Website Builder | Strengths speak for themselves, not false!

Treasure, we say, a lot of parties are not happy with their own sites are peers copying homework, which is like you open a store, certainly do not want others to know your sources of supply is a reason, right? So, in order to give customers a full sense of security, we did not put any customer case work on the site of the link ha, this is our protection of customer privacy.

📌 But if there is a sincere desire to do the site of the boss, or treasure you just want to look at our case, then hurry up to add the contact information of our website, we snappy send you a few look at, the main one sincere!

If you have not decided to do what style of website, but also simple, you find a few peers of the site sent to us, according to do on the end of the matter. We choose to imitate the site does not mean that no skill, we just can not stand that kind of invalid communication and cumbersome to make people crazy process. Some parties to do a corporate display site, have to find dozens of suppliers to roll, to be honest, no real technology companies will be there to lick the party. We are not the same, we want to be down-to-earth to the customer to make cost-effective, so that customers are satisfied with the site straight pat. Those kneeling work, let those who only play lip service, no technology companies to do it, we do not serve! # Website Development

# Party A Party B # Rejected Inside Coil

Contact the WordPress Technical Team

We look forward to hearing from you.

Submit your request

Quote Collection Form