• Resolved ceco

    (@ceco)


    I’ve followed this thread

    http://ww.wp.xz.cn/support/topic/69219?replies=3

    to get the title for an specific post but I cannot get it to work. The code I’m using is:

    <?php echo
    $my_id = 414;
    $post_id_414 = get_post($my_id);
    $title = $post_id_414->post_title;
    ?>

    where 414 is the id of the post in wordpress. When I run the script I get 414 instead of the title.

    Any suggestions?

Viewing 15 replies - 1 through 15 (of 17 total)
  • Where are you trying to do this?
    Can you just use the_title instead?

    Thread Starter ceco

    (@ceco)

    Not sure how to use the_title or if that’s what I need.
    I have a page with text and I would like to dynamically insert whatever the title (or body for that matter) of an specific post is. I should mention that post 414 is in wordpress but the page I’m trying to create isn’t run within wordpress although it’s in the same folder as the wordpress files.

    Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    Try this instead:
    $title = get_the_title(414);

    Might be simpler. 🙂

    the page I’m trying to create isn’t run within wordpress although it’s in the same folder as the wordpress files.

    Ahh. I missed that. Okay, then add this to the top of your page:
    require('./wp-blog-header.php');

    That will include the WordPress code into your file so that you can access the WordPress database and such.

    Thread Starter ceco

    (@ceco)

    thanks for that otto42 and blepoxp. I don’t want to use the header for this page in particular. The connection to the database is in place as I’m trying to create a form and the input goes to the db.
    Anyway ’round this?

    Is there a reason you don’t want to use the header. It gives you access to the wp functions as well as the database connection.

    I guess you could redefine the functions on that page.

    Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    I don’t want to use the header for this page in particular.

    Well, you were already trying to use get_post(), which is a WordPress function too, you know.

    If you’re not going to use the WordPress functions, then you have to query the database directly using your own code. SELECT * FROM wp_posts WHERE ID=414.

    Thread Starter ceco

    (@ceco)

    The reason is that the header contains elements like the menu. I would like to make this page stand-alone. Is it possible to save header.php with another name and strip it to the minimum and then link it to this page like:

    require(‘./wp-blog-header2.php’);

    so I can then use wp functions.

    Is there another way to be able to use the wp functions?

    I think you’re confusing the wpheader and the themes’ header.

    Thread Starter ceco

    (@ceco)

    yes, I was!

    Blepoxp, Is this the right syntax

    <?php
    require('./wp-blog-header2.php');
    ?>
    
    <?php echo
    $my_id = 414;
    $post_id_414 = get_post($my_id);
    $title = $post_id_414->post_title;
    ?>

    because I get nada

    Thread Starter ceco

    (@ceco)

    sorry, my mistake. it should be

    require('./wp-blog-header.php');

    i’ll try that now

    Thread Starter ceco

    (@ceco)

    I still don’t get anything, not even the 414

    Do you echo $title or else?

    Thread Starter ceco

    (@ceco)

    yes!! thank you both. It was looking at my face

    <?php
    require('wp-blog-header.php');
    ?>
    
    <?php echo
    $title = get_the_title(414);
    ?>
    Thread Starter ceco

    (@ceco)

    how do I insert it inside a <?php else: ?>

    what I’ve got is:

    <?php if($show_thanks): ?>
        Thank you. 
    
        <?php else: ?>
    
    	Submit your details below to be entered into
    
    <?php echo
    $title = get_the_title(414);
    ?>
    Thread Starter ceco

    (@ceco)

    ok, thanks to all, I finally got it. It is just like above.

Viewing 15 replies - 1 through 15 (of 17 total)

The topic ‘Get title for specific post’ is closed to new replies.