Forums

A function that tells you whether the current page is the homepage? (10 posts)

  1. Trahald
    Member
    Posted 2 years ago #

    Is there an is_homepage() type of function? And/or perhaps one that shows whether you are on the homepage, a post, a page, a category, an archive, etc.?

  2. MichaelH
    Volunteer
    Posted 2 years ago #

  3. MichaelH
    Volunteer
    Posted 2 years ago #

    Also not related but of interest:
    https://core.trac.wordpress.org/ticket/6801

  4. Trahald
    Member
    Posted 2 years ago #

    Thank you.

  5. Trahald
    Member
    Posted 2 years ago #

    Actually I'm having an issue trying to use is_front_page() within a plugin. The function fails at the "is_page(get_option('page_on_front'))" step, because it seems that the global $wp_query is NULL? So is_page() returns FALSE.

    How might I deal with $wp_query being NULL within the plugin?

  6. MichaelH
    Volunteer
    Posted 2 years ago #

    For some templates it has been necessary to use
    wp_reset_query();

  7. Trahald
    Member
    Posted 2 years ago #

    Sorry for the late response, but wp_reset_query() does not help. I also tested using the default theme. global $wp_query is still NULL within the plugin.

  8. Trahald
    Member
    Posted 2 years ago #

    I was able to get this working by adding query_posts() after wp_reset_query().

    wp_reset_query();
    query_posts();

    However I'm not sure what to put as an input to query_posts(), and without any input I get the warning "Warning: Invalid argument supplied for foreach() in /home/.../wp-includes/query.php on line 1401"

    What argument should I be givnig to query_posts()?

  9. MichaelH
    Volunteer
    Posted 2 years ago #

    Try

    query_posts($query_string);
  10. Trahald
    Member
    Posted 2 years ago #

    Thanks again but that doesn't quite work. global $query_string is blank even after wp_reset_query(). So I get the same warning.

    I tried passing globals wp_the_query and wp_query as well.

    I can just suppress the warning, but I'm not sure if I'll get some unforeseen bugs later if I don't pass a valid argument to query_posts().

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.