• Resolved ogicu812

    (@ogicu812)


    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'); //#=> https://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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter ogicu812

    (@ogicu812)

    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?

    Thread Starter ogicu812

    (@ogicu812)

    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;
    }
    Thread Starter ogicu812

    (@ogicu812)

    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;
    }
    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

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

    $page = get_page_by_title('Title of Page');
    $link = get_permalink($page->ID);

    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);
    }

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Function for generating a page url?’ is closed to new replies.