WordPress.org

Ready to get started?Download WordPress

Forums

How to check page STATUS? (21 posts)

  1. sirtimo
    Member
    Posted 3 years ago #

    Hello,

    I want to display some text on my website, whenever the page with a specific name is published (i keep this page as a draft when i dont want to show this content)

    How do i do that?

    I thought this would do the trick;

    <?php if(get_page_by_title('Storing')) : ?>
    <h3>test</h3>
    <?php endif;?>

    But this shows the post under all circumstances, since it only seems to check if the page excists.

    Any clues?

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    To check if the current page being shown is "Storing" you can use conditional tags is_page() in your theme's page.php.

    <?php if(is_page('Storing')) : ?>
    <h3>test</h3>
    <?php endif;?>

    But I'm not sure this is what you need. Do you need the "Storing" text on your front page or just when a Page is shown?

  3. keesiemeijer
    moderator
    Posted 3 years ago #

    This code will check the post status of a Page with an ID of 2. You can use this code anywhere. Just change the $draft_page_id to your draft/publish Page.

    <?php
      $draft_page_id = 2; // example id of your draft/publish page Storing
      $draft_page = get_page( $draft_page_id );
      if ($draftpage->post_status == 'publish') :
        // post is published
        // these next to lines are for if you want to show the content of your draft Page
        $draft_content = apply_filters('the_content', $draftpage->post_content);
        $draft_content = str_replace(']]>', ']]>', $draft_content);
    ?>
    <h3>test</h3>
    <?php
     // if you want to show the content of your draft post
     echo $draft_content;
    ?>
    <?php endif; ?>
  4. sirtimo
    Member
    Posted 3 years ago #

    Great! Thanks for the reply, thats exactly what i needed. This will do the job for me.

    EDIT: There's something wrong, it doesn't give any output here. My page id is 60, i added that, and it's currently a draft. I've placed the code somewhere in a left floating div i made outside of the loop or anything. There's just no output.

    I

  5. sirtimo
    Member
    Posted 3 years ago #

    So as you got it already, i need the text to be shown when the page 'storing' is published, i use it as a page that is only shown when there is a problem, and then a button should appear with some text, on the left of my website.

  6. keesiemeijer
    moderator
    Posted 3 years ago #

    I made an edit to the second peace of code. Maybe you just missed it. it was echo $content; and needs to be echo $draft_content;. Check the code you have now. It only shows the text if "Storing" post status is set to "publish"

  7. sirtimo
    Member
    Posted 3 years ago #

    It still doesn't work here. Very odd.

    I've used the right id, the page is published, and the code doesn't give any errors. Upon looking in the source, there's nothing being printed. Also the TEST within the h3 tags isn't shown. So there moest be something wrong :/

  8. keesiemeijer
    moderator
    Posted 3 years ago #

    In what theme template file did you put this code?

  9. sirtimo
    Member
    Posted 3 years ago #

    in the page.php, it's in a div on the left of the loop (outside of the loop) just below the header.

  10. keesiemeijer
    moderator
    Posted 3 years ago #

    Are you sure you are on a Page and not on the frontpage or on a single post etc...? I just tested it on my test server and it shows up. This code does not have to be in the loop for it to work.
    Change this (for testing):

    $draft_page = get_page( $draft_page_id );

    to this:

    $draft_page = get_page( $draft_page_id );
    echo '<pre>';
    print_r($draft_page);
    echo '</pre>';

    If the ID is correct you should see an array with all your page content on the screen.

  11. keesiemeijer
    moderator
    Posted 3 years ago #

    Ok sorry this is the correct code. there were some errors in there, sorry:

    <?php
      $draft_page_id = 60; // example id of your draft/publish page
      $draft_page = get_page( $draft_page_id );
    
      if ($draft_page->post_status == 'publish') :
        // post is published
        // these next to lines are for if you want to show the content of your draft Page
        $draft_content = apply_filters('the_content', $draft_page->post_content);
        $draft_content = str_replace(']]>', ']]>', $draft_content);
    ?>
    <h3>test</h3>
    <?php
     // if you want to show the content of your draft post
     echo $draft_content;
    ?>
    <?php endif; ?>
  12. sirtimo
    Member
    Posted 3 years ago #

    This is giving my the array!
    So that seems to be working :)

    stdClass Object
    (
    [ID] => 60
    [post_author] => 1
    [post_date] => 2010-10-22 07:24:32
    [post_date_gmt] => 2010-10-22 07:24:32
    [post_content] =>
    [post_title] => Storing
    [post_excerpt] =>
    [post_status] => publish
    [comment_statu...

  13. keesiemeijer
    moderator
    Posted 3 years ago #

    I just posted the correct code above your last post. I think it will work now.

  14. sirtimo
    Member
    Posted 3 years ago #

    Woohoo. it does. You've been of great help, no need for sorry. thanks :)

    There's one more thing, is it easy to show a trim of the content, say, the first 200 characters or something?

  15. keesiemeijer
    moderator
    Posted 3 years ago #

    You can use this to trim the content from the code to 40 words:
    `
    <?php
    // if you want to show the content of your draft post

    $limit = 40; // this is 40 words
    $draft_content = explode(' ', $draft_content);
    if (count($draft_content) > $limit) {
    $draft_content = implode(' ', array_slice($draft_content, 0, $limit)) . '...';
    }
    echo $draft_content;
    ?>

  16. sirtimo
    Member
    Posted 3 years ago #

    that currently displays 'array' for me :)

  17. keesiemeijer
    moderator
    Posted 3 years ago #

    Ok, that code is wrong. Here is the correct version(I think):

    <?php
    // if you want to show the content of your draft post
    
    $limit = 40; // this is 40 words
    $content = explode(' ', $draft_content);
    if (count($content) > $limit) {
    $content = implode(' ', array_slice($content, 0, $limit)) . '...';
    } else { $content = $draft_content; }
    echo $content;
    ?>

    I need some coffee!

  18. sirtimo
    Member
    Posted 3 years ago #

    Get yourself some coffee! Thanks for the great help :) it's up and running!

  19. ddellostritto
    Member
    Posted 2 years ago #

    keesiemeijer - wonderful piece of code, thanks! Do you think you might be able to help me? I'd like to only display a text and have it link to the actual content, so I'd need to get the post url, not it's content and echo that into a link:

    ">link text

    Thanks in advance!

  20. ddellostritto
    Member
    Posted 2 years ago #

    the code didn't show, now it should:

    <a href="<?php the_post_link ?>">link text</a>
  21. ddellostritto
    Member
    Posted 2 years ago #

    Figured it out myself; but understanding the "published" status part was key. Thanks again!!!!!

    In case anyone else needs something similar.

    <?php
     $dd_post_id = 1;
     $dd_check_post_id = get_page( $dd_post_id );
     $dd_post_url = get_permalink( $dd_post_id );
    
      if ($dd_check_post_id->post_status == 'publish') echo '<div><a href="'.$dd_post_url.'">'.$dd_post_title.'</a></div>';
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic