WordPress.org

Support

Support » WP-Advanced » get_query_var can’t work on the sidebar

get_query_var can’t work on the sidebar

  • hai..
    i have trouble with get_query_var. when I write a page using template page and saved by name ‘product’, get_query_var(‘pagename’) couldn’t display a value. when i called it, it should be generate a value = ‘product’ right…?!
    the script was put on the sidebar. i don’t know why the value can displayed on the header’s area….
    anybody can explain that…?thanks
    regards

Viewing 3 replies - 1 through 3 (of 3 total)
  • get_query_var() would not return a value for ‘pagename’ unless you’ve actually passed a query in the url for ‘pagename’, like:

    /?pagename=product

    I assume this is not how your permalinks are set up. Anyway, you can also access the pagename (post/Page slug) through the $post object:

    <?php
    if( is_page() ) {
    	global $post;
    	echo $post->post_name;
    }
    ?>

    EDIT: Ok, not thinking clear tonight… Using:

    <?php echo get_query_var('pagename'); ?>

    would display the pagename on a Page when using custom permalinks that generate a url like:

    /product/

    for the ‘Product’ Page.

    but if the url is(when click on old post):
    http://URL/photos/photo/234243
    function return “”

    what is th solution?

    Did you ever find a solution for this? I’m trying to pass a variable through the URL to a single.php in an iframe like:

    home page has:
    href="?p=<?php the_ID(); ?>?wh=1"

    single.php has:
    get_query_var("wh"); echo "wh=" . $wh;

    I would expect it to show the variable 1 on the single.php page. But it doesn’t.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_query_var can’t work on the sidebar’ is closed to new replies.