WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. mukhaidor
    Member
    Posted 6 years ago #

    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

  2. Kafkaesqui

    Posted 6 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:

    /?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.

  3. rat86
    Member
    Posted 5 years ago #

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

    what is th solution?

  4. seej
    Member
    Posted 5 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