• 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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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;
    ?>
    Thread Starter Loubonez

    (@loubonez)

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_page() Parameters Optional?’ is closed to new replies.