WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to determine when the_loop is running? (7 posts)

  1. Colin Devroe (Plain)
    Member
    Posted 8 months ago #

    Hello fellow WordPress hackers. We've run into an interesting issue that I hope someone with deep WordPress insight can help us tackle.

    We're writing an action that plugs into the_title and the_content that we'd like to run only for the page title and content or blog post title and content but not when it is in the sidebar or in a recent posts area in a footer, etc.

    Running debug_backtrace allowed us to determine when get_sidebar or similar was loading but, obviously, that PHP function is disabled on many many hosts.

    Has anyone ran into a similar corner and have any suggestions as to how we can run our action only when the_title and the_content are being run in the main content areas?

  2. esmi
    Forum Moderator
    Posted 8 months ago #

    Have you tried using is_page() and is_post_type_archive()?
    http://codex.wordpress.org/Conditional_Tags

  3. Colin Devroe (Plain)
    Member
    Posted 8 months ago #

    esmi: I should have been much more specific, perhaps, sorry. Yes. We already are using is_page() and is_single() to determine whether or not we're on the page or blog post permalink. But, even those pages call the_title numerous times such as in navigation, footers, and sidebars on many themes.

    What we'd like to figure out is to only call our action hook in our plugin only for the actual blog post title and content but not in the navigation, footer, or sidebar.

  4. esmi
    Forum Moderator
    Posted 8 months ago #

    There's also in_the_loop() but I'm not sure how it would work if there was secondary custom loops in a sidebar or footer. Another approach might be seeing if you can grab the current template name and use that as a conditional.

  5. Colin Devroe (Plain)
    Member
    Posted 8 months ago #

    esmi: Yes. in_the_loop will always return true in these cases.

  6. jgclarke
    Member
    Posted 8 months ago #

  7. Colin Devroe (Plain)
    Member
    Posted 8 months ago #

    jgclarke: Thanks so much! This fixes the issue in 90% of the cases we've been able to test against. Good enough! Thanks again.

Reply

You must log in to post.

About this Topic