WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to detect the page you're currently viewing? (9 posts)

  1. f00bar
    Member
    Posted 5 years ago #

    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

  2. stvwlf
    Member
    Posted 5 years ago #

    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

  3. f00bar
    Member
    Posted 5 years ago #

    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

  4. drmanry
    Member
    Posted 5 years ago #

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

  5. f00bar
    Member
    Posted 5 years ago #

    Thank you drmanry :) Now I just need to figure out the other problem I listed above :(

  6. drmanry
    Member
    Posted 5 years ago #

    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.

  7. f00bar
    Member
    Posted 5 years ago #

    Thank you drmanry, that's great!

  8. sanddlife
    Member
    Posted 4 years ago #

    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!

  9. nightscooter
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic