Support

Account

Home Forums Gutenberg How to get the id of the current page while in edit mode?

Solving

How to get the id of the current page while in edit mode?

  • Hello,
    When using some of the core functions in Gutenberg in the backend they appear blank. Because of this I am doing hokey stuff like this:

    <?php if (is_admin() ): ?>
    <H2 data-aos="fade-right">Page Title</H2>
    <?php else: ?>
    <H2 data-aos="fade-right"><?php echo get_the_title(); ?></H2>
    <?php endif; ?>

    It sure would be nice to just be able to grab the page Id so that these functions could return the correct data.

    Any ideas on how to do this? Is there already a function for this? Of course I tried the basic stuff like get_the_id() and echoing out the global post.

  • There isn’t anything in ACF that I know of, and most of the things you can do could also be considered “hokey”

    you can try

    
    global $post;
    echo $post->post_title;
    

    you can also try looking that the query string

    
    echo get_the_title($_GET['post']);
    

    You may also be able to use

    
    $post = get_queried_object();
    

    I’m not exactly sure which, if any, of the above will work with what you’re trying to do.

  • Yeah, unfortunately none of those methods seem to work in the post edit page.

    However I did figure it out by looking through some other gutenberg examples:

    $post_id = get_the_ID() ? get_the_ID() : $_POST['post_id'];

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.