How to create a siblings menu with sage9 - SOLVED

this is both a wordpress / sage question, i’ve been looking into the wordpress documentation on how to create a siblings menu for pages like this hierarchy

parent page
    child a
    child b
    child c

and i couldn’t find any good function that returned an array of results, all i found returns the actual menu, so i’m confused in how to implement this and loop the result on the blade template.

can anyone point me to the right direction? :face_with_raised_eyebrow:

if you need a menu with a class to know where are you placed (aka .current)

you’ll need a new simple function:

public function childMenu() {
    global $post;
    // Set up the objects needed
    $childQuery = new \WP_Query();
    $pages = $childQuery->query(array('post_type' => 'page', 'posts_per_page' => '-1'));
    
    if ( is_page() && $post->post_parent ){
        $childpages = get_page_children( $post->post_parent, $pages);
    }
    else{
        $childpages = get_page_children( $post->ID, $pages);
    }
    return $childpages;
}

//we need this to compare with the current page
    public function currentUrl() {
        return wp_make_link_relative(get_permalink( $post->ID ));
    }

so with those 2 functions on your app controller you’ll be able to make a loop in your template like this one:

<nav class="secondary-nav">
    <ul>
        @foreach($child_menu as $child)
            <li @if(wp_make_link_relative(get_permalink($child->ID)) == $current_url)class="is-active" @endif>
                <a href="{!! get_permalink($child->ID) !!}">{{$child->post_title}}</a>
            </li>
        @endforeach
    </ul>
</nav>

learned the hard way that you can use wordpress functions without the @php wrapper.
thanks @alwaysblank for your guide and recomendations

I would strongly caution against using the guid as your URL. The guid's only purpose is to serve as a globally unique identifier for whatever object it’s attached to: The fact that it contains a URL is nice, but not something you should rely on. From a development perspective, you should generally regard the fact that the guid includes a URL to the resource as a coincidence, not as a feature you can rely on.

If you want the URL of a post or page, you should just use get_permalink(get_the_ID()) or some variation on that theme.

1 Like

I believe this code is not working anymore :confused: