WordPress.org

Support

Support » How-To and Troubleshooting » How to check page STATUS?

How to check page STATUS?

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

Viewing 15 replies - 1 through 15 (of 20 total)
  • Moderator keesiemeijer

    @keesiemeijer

    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?

    Moderator keesiemeijer

    @keesiemeijer

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

    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

    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.

    Moderator keesiemeijer

    @keesiemeijer

    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”

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

    Moderator keesiemeijer

    @keesiemeijer

    In what theme template file did you put this code?

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

    Moderator keesiemeijer

    @keesiemeijer

    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.

    Moderator keesiemeijer

    @keesiemeijer

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

    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…

    Moderator keesiemeijer

    @keesiemeijer

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

    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?

    Moderator keesiemeijer

    @keesiemeijer

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

    that currently displays ‘array’ for me 🙂

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘How to check page STATUS?’ is closed to new replies.
Skip to toolbar