• Resolved meraj.khattak

    (@merajkhattak)


    Hello,

    I have a page where two posts are fetched and both are of custom type.

    • The actual singular page with content. (post id #460)
    • A testimonial shown in footer is also stored as custom post is also fetched. (post id #505)

    Now the requirement is to show ‘Edit’ option as sub menu in admin bar. I tried to do this via this this code:

    /**
     *  Add sub menu item to admin bar
     */
    function custom_admin_bar_render() {
        global $wp_admin_bar;
        global $post;
    
        if ( is_single() )
            $wp_admin_bar->add_menu( array(
                'id' => 'edit',
    
                //here define the name of parent under which this link is required to appear
                'parent' => 'new-content',
                'title' => __( 'Edit'),
                'href' => get_edit_post_link($post->id)
            ) );
    }
    add_action( 'wp_before_admin_bar_render', 'custom_admin_bar_render' );

    I see it adds a sub menu item under + New but it links to Testimonial shown in footer (post id #505), while I want it to link to actual singular page (post id #460).

    As per my understanding, it is doing this because footer post is called later and when this action ‘custom_admin_bar_render’ is processed, it gets details of post for testimonial.

    Is there any way through which I can access the post object called before the testimonial in footer?

    Thanks.

Viewing 1 replies (of 1 total)
  • Thread Starter meraj.khattak

    (@merajkhattak)

    Fixed this issue by using $wp_query->get_queried_object(); as the following in my code:

    /**
     *  Add sub menu item to admin bar
     */
    function custom_admin_bar_render() {
        global $wp_admin_bar;
        global $wp_query;
        $post_obj = $wp_query->get_queried_object();
        $post_id = $post_obj->ID;
    
        if ( is_single() )
            $wp_admin_bar->add_menu( array(
                'id' => 'edit',
    
                //here define the name of parent under which this link is required to appear
                'parent' => 'new-content',
                'title' => __( 'Edit'),
                'href' => get_edit_post_link($post_id)
            ) );
    }

    Hope this will help someone.

Viewing 1 replies (of 1 total)
  • The topic ‘Adding edit link to admin bar as sub menu’ is closed to new replies.