WordPress.org

Ready to get started?Download WordPress

Forums

get_page() Parameters Optional? (3 posts)

  1. Loubonez
    Member
    Posted 3 years ago #

    I am trying to use get_page() in a page template file to grab the slug, like so:
    $slug = get_page()->post_name;
    but I am getting the warning:
    PHP Warning: Missing argument 1 for get_page(), called in .../profiles-page.php on line 24 and defined in .../wp-includes/post.php on line 3110
    According to the Codex page, any parameters are optional. Anybody have any ideas about why I am getting this error?

  2. Jeremy Boggs
    Member
    Posted 3 years ago #

    The codex page says it's option, but you do need to pass it an ID by reference, which the codex page also says. Assuming you know your page's ID, you can do:

    <?php
    $pageId = '12';
    $slug = get_page($pageId)->post_name;
    ?>

    Of course, if you're on the page template and want to get the slug of the current page, the easiest way to do that is to use the global $post variable that's available:

    <?php
    $slug = $post->post_name;
    ?>
  3. Loubonez
    Member
    Posted 3 years ago #

    Holy cow, how did I not think of just using $post? Thank you so much for the help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags