Support » Fixing WordPress » How to detect the page you’re currently viewing?

  • Resolved f00bar

    (@f00bar)


    Hi, hope someone can help me with this.

    I’m coding a loop to bring in page titles as a menu, using the following loop:

    <?php
    $curr = '';
    $menuitems = get_pages('sort_column=menu_order&sort_order=ASC');
      foreach ($menuitems as $menuitem) {
    	$menu = '<li class="tabstart '.$curr.'"></li>';
            $menu .= '<li class="tabmid '.$curr.'"><a href="'.get_page_link($menuitem->ID).'">';
    	$menu .= $menuitem->post_title;
    	$menu .= '</a></li>';
    	$menu .= '<li class="tabend '.$curr.'"></li>';
    	echo $menu;
      }
     ?>

    As you can see above there is a variable called $curr. This stands for “current” and there is a CSS class called current which changes the colour of the tab of the page you’re viewing. What I need to know is, is there a function or some kind of way of detecting which page the user is viewing in order to populate the $curr variable with the word “current” on the current page but remain empty for every other?

    I’m quite new to WordPress coding so any help is appreciated, thank you.

    Michael

Viewing 8 replies - 1 through 8 (of 8 total)
  • I suggest you look at the wp_list_pages function, which does what you are trying to do, including adding a class for the current page.

    http://codex.wordpress.org/Template_Tags/wp_list_pages

    Thank you for the help! That does exactly what i want.

    However – my tab menu requires extra list items before and after each link to display a rounded corner. So, the set up for each link would be:

    <li class="tabstart"></li>
    <li class="page_item">Home</li>
    <li class="tabend"></li>

    As you see above, tabstart and tabend are classes which do not contain any items but are simply there to round off the tabs. wp_list_pages does not allow me to do this since it literally echos it to the page and won’t let me put it in my own foreach loop.

    Is there anything I can do to override this?

    Thank you

    <?php wp_list_pages('title_li='); ?>

    Thank you drmanry 🙂 Now I just need to figure out the other problem I listed above 🙁

    Take a look at this short tutorial:

    http://rubiqube.com/wordpress-tutorial-rounded-corner-tab-menu-using-css/

    It is based on categories, but can easily be changed to pages instead.

    Thank you drmanry, that’s great!

    Can someone break out a line of code(or 2) that would simply tell me if I’m on the ‘blog’ page. My home page is static, but a link takes me to the blog page. The standard is_home() is not working.

    I need the following:

    <your cool code here>
    If on blog page, then set image var to ‘image.jpg.;

    not fancy, but I hope you understand.

    TIA!

    Hi Sanddlife,

    I was just looking for that too. Check out http://codex.wordpress.org/Conditional_Tags and scoll down to ‘A PAGE Page’…

    for example I’m using:

    <p <?php if (is_page("recruitment")) { ?> class="currentPage" <?php } ?> > Recruitment page link here </p>

    so that it only includes the text class=”currentPage” if the page is called recruitment. It’s all working good for me

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to detect the page you’re currently viewing?’ is closed to new replies.