get_query_var can't work on the sidebar (4 posts)

  1. mukhaidor
    Posted 8 years ago #

    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

  2. Kafkaesqui

    Posted 8 years ago #

    get_query_var() would not return a value for 'pagename' unless you've actually passed a query in the url for 'pagename', like:


    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:

    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:


    for the 'Product' Page.

  3. rat86
    Posted 8 years ago #

    but if the url is(when click on old post):
    function return ""

    what is th solution?

  4. seej
    Posted 8 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic