WordPress.org

Ready to get started?Download WordPress

Forums

is_single() is not working properly (7 posts)

  1. chris-babww
    Member
    Posted 1 year ago #

    Hello,
    I have a problem.

    I have a sidebar. In this sidebar I would like to show something just when a single post is displayed.

    The code I'm using is:

    <?php if ( is_single() ) { ?>
         <p>Show this just when a single post is displayed</p>
    <?php } ?>

    but it doesn't work.

    When I try with:

    <?php if ( is_home() ) { ?>
         <p>Show this just in the home page</p>
    <?php } ?>

    It shows the sentence everywhere, both in home and in the single post.

    What can I do? Thank you!!!

  2. Zoe
    Member
    Posted 1 year ago #

    What's the full text of sidebar.php (or wherever you're placing this snippet)? You'll probably need to use Pastebin if it's long.

  3. chris-babww
    Member
    Posted 1 year ago #

  4. Zoe
    Member
    Posted 1 year ago #

    Can you share a link to the site? Have you tried disabling plugins to see if it's caused by a plugin? What theme are you using? (It doesn't appear to have a widget-ready sidebar, which seems odd.)

  5. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    you have probably some code running before the sidebar - somewhere in the templates (could be from a plugin ?) - which modifies the original query string;

    possibly try to add <?php wp_reset_query(); ?> bofore your code.

  6. chris-babww
    Member
    Posted 1 year ago #

    It works. I have just added <?php wp_reset_query(); ?> before the code.

    If you want to have a look to the website, the url is: http://www.halcyon-hills.co.uk . The theme has been made by me and the designer of the company.

    Thank you for everything!

    Byeeeee!

  7. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    It works. I have just added <?php wp_reset_query(); ?> before the code.

    the wp_reset_query(); should be added just after your custom query in the sidebar; into line 12 of the pastebin code.

    http://codex.wordpress.org/Function_Reference/query_posts#Secondary_Loops
    quoted from that link:

    If you must use query_posts(), make sure you call wp_reset_query() after you're done.

Topic Closed

This topic has been closed to new replies.

About this Topic