Support » Fixing WordPress » hot to get the page title for a given page id?

  • Resolved piedro



    This question probably is too basic to find an answer by searching (I tried for quite a while now):

    How do I get wordpress to show a page title for a given ID?

    I want to place page links in a html text. Something like:

    “Please visit
    <a href="?page_id=86"> THE_TITLE_OF_THE_PAGE_WITH_ID_86 </a>
    for more information.”

    I just can’t find any clue …

    cya, piedro

Viewing 12 replies - 1 through 12 (of 12 total)
  • <?php
    $pageID = 86;
    $page = get_post($pageID);
    echo $page->post_title;

    you have to put the page ID you want the title of in a variable when calling get_post()

    Would it not be easier to use…

    <?php echo get_the_permalink( ID ); ?>

    Replacing ID with the given id..

    Rather the getting all the post’s (page’s) data just for the title, when you’re essentially turning it into a link anyway…

    Just a suggestion… 🙂

    Hi there!

    thx, this is what I#m looking for.

    >> t31os_ :
    That’s definitely true, a much better solution …
    But I get an undefined function error when using it.

    Any ideas?


    hi t31os_ !

    Your function doesn’t exist.
    There is however a function “get_permalink” but it say in the description:

    “Returns the permalink to a post for use in PHP. It does NOT display the permalink and can be used outside of The Loop.”

    But waht I want is to explicitely SHOW the title and, yes, you’re right on that, use it as a link.

    thx anyway,

    Yes sorry, did mean get_permalink…

    As long as you’ve got what you want though, no harm done… 😉

    Hi again!

    sorry, I had to work on other things the last days.
    Now back to the topic:

    I still can’t get it right.

    I have a page (not a post) with the id=13.
    The Title is “Our Sunny Page”, but sometimes the title changes, cause the author cam’t make up her mind.

    I want to add a link somewhere in a textblock (a normal text paragraph, nothing special, no loops etc.). This Link should look like this:

    “Please visit >>> Our Sunny Page , because it’s Sunday!”

    That’s it. I want to generate a link that displays the page title (not the permalink) and works as link (to the permalink) for a given page id. (Just for your interest: When it’s done the displayed link will show a different title and links to a different page on every day of the week … )

    This must be very simple, I just can’t figure it out, any ideas?

    thx, piedro

    hi stvwlf!

    I didn’t understand your solution (I still don’t, to be honest e.g. why is post_title the same as page_title?), but now I gave it a try just as it is (like a “let’s go with a blackbox” approach …).

    Works! 🙂

    Seems a bit clumsy, I thought, there’ll be a simple tag, that i just don’t know about, but: Hej, it works.

    I used:
    <a href="?page_id=86"><?php $pageID = 86; $page = get_post($pageID); echo $page->post_title; ?></a>

    and it does the job.

    thx for your efforts,

    <?php $ptitle = get_the_title( 86 ); $plink = get_the_permalink( 86 ); ?>
    <a href="<?php echo $plink ?>"><?php echo $ptitle; ?></a>

    Don’t need to use get_post …


    thx, t31os_ ,
    this solution is even more elegant.

    Both ideas work perfectly,
    thx to all for your help,

    get_post() will grab all the post data (well not all, but more then required)..

    Not sure about elegant, but more efficient to grab only what you need.

    🙂 Happy to help anyway.. 😉

    If you need just the first part of the link — say for image links — and plan on using it a lot, try this “elegant” solution… Put the following function in your theme’s functions file…

    function WPpageLinkStart($num)
    	return '<a href="' . get_page_link($num) . '" ' .
    	'title="' . get_the_title($num) . '">';

    Then you can include that function wherever you need it, ie…
    echo WPpageLinkStart(123) . '<img src="/path/to/image123.jpg" /></a>';




    Cool stuff!

    Just by reading all your different solutions I’m learning.
    Had some time now and changed everything, though working, to the “elegant solution” and will try out the “WPpageLinkStart” for the imagelinks quite soon.

    thx again, now after everythings working:


Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘hot to get the page title for a given page id?’ is closed to new replies.