WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Function for generating a page url? (7 posts)

  1. ogicu812
    Member
    Posted 5 years ago #

    Is there a function that would generate the url of a page? I would like it to accept the page title as a parameter and have it return the url for the page, taking into account whether or not "pretty permalinks" are being used.

    For example:
    <?php echo mystery_func('my_page_title'); //#=> http://localhost/?page_id=55 ?>

    I saw the get_page_by_title function in the Codex, but it returns an object and I can't find where its methods/attributes are documented. Also, I saw the get_page_uri function but that just returns the page title given a page_id.

  2. iridiax
    Member
    Posted 5 years ago #

  3. ogicu812
    Member
    Posted 5 years ago #

    Thanks for the reply. I've created some custom pages using custom templates. On some of them I have included links that need to go to other pages and also a form submission where the form action is the url of another page. I would like some way of generating the url as shown in my original post. I tried the following but it starting messing up how posts were displayed in WordPress 2.8:

    function get_page_url_by_pagename($pagename) {
      $p = query_posts("pagename=$pagename");
      $p = $p[0];
      return $p->guid;
    }

    Because I need this functionality outside "The Loop", the_permalink() functions, etc. won't work for me.

    Any thoughts?

  4. ogicu812
    Member
    Posted 5 years ago #

    Got it...

    The query_posts() method modifies "The Loop". You can use the get_posts() method instead:

    function get_page_url_by_pagename($pagename) {
      $p = get_posts("pagename=$pagename");
      $p = $p[0];
      return $p->guid;
    }
  5. ogicu812
    Member
    Posted 5 years ago #

    Even better... (handles permalinks)

    function get_page_url_by_pagename($pagename) {
      $p = get_posts("pagename=$pagename");
      $p = $p[0];
      $url = get_permalink($p);
      return $url;
    }
  6. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    If you know the page title, you can do this:

    $page = get_page_by_title('Title of Page');
    $link = get_permalink($page->ID);
  7. harianto
    Member
    Posted 4 years ago #

    i love to make things very short using Otto42 example

    function get_permalink_by_title($title) {
      return get_permalink(get_page_by_title($title)->ID);
    }

Topic Closed

This topic has been closed to new replies.

About this Topic