WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] hot to get the page title for a given page id? (13 posts)

  1. piedro
    Member
    Posted 4 years ago #

    Hi!

    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 ...

    thx,
    cya, piedro

  2. stvwlf
    Member
    Posted 4 years ago #

    <?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()

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    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... :)

  4. piedro
    Member
    Posted 4 years ago #

    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?

    piedro

  5. piedro
    Member
    Posted 4 years ago #

    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,
    p.

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    Yes sorry, did mean get_permalink...

    As long as you've got what you want though, no harm done... ;)

  7. piedro
    Member
    Posted 4 years ago #

    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

  8. piedro
    Member
    Posted 4 years ago #

    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,
    piedro

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

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

  10. piedro
    Member
    Posted 4 years ago #

    Hi!

    thx, t31os_ ,
    this solution is even more elegant.

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

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

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

  12. Zoinks! Graphics
    Member
    Posted 4 years ago #

    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>';

    Excelsior!

  13. piedro
    Member
    Posted 4 years ago #

    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:
    RESOLVED

    :-)
    p.

Topic Closed

This topic has been closed to new replies.

About this Topic