WordPress.org

Ready to get started?Download WordPress

Forums

Determine when accessing page? (10 posts)

  1. bmwboarder
    Member
    Posted 3 years ago #

    I can't figure out how to determine when this page is being processed:
    http://www.ranacreek.com/dev/projects/

    I know that "projects" is post 5, but if($post->ID==7){do something} doesn't work. It is essentially an index of custom post types. If I can determine when it is being loaded, I can fix the highlighting in the menus. Thanks.

  2. vtxyzzy
    Member
    Posted 3 years ago #

    It is possible that the menus have already been processed before the page is known. If this is not the case, if (is_page(5)) {do something} should work.

  3. bmwboarder
    Member
    Posted 3 years ago #

    Thanks vtxyzzy. I tried that (I meant a 5 in the example above), but it doesn't work.

    I ended up working around it by just setting a php variable in the php template file, before the header file is called. Probably not the simplest, but it works. Thanks for replying :)

    Brandon

  4. bmwboarder
    Member
    Posted 3 years ago #

    Actually, just in case anyone reads this... setting a php variable doesn't work either, though I thought it would. So I still don't have a good solution, but I'm done worrying about it.

  5. vtxyzzy
    Member
    Posted 3 years ago #

    Out of curiosity, did you declare the variable global? That might make a difference.

  6. bmwboarder
    Member
    Posted 3 years ago #

    I tried that, and still no luck. Does the get_header() not act as an include? I thought for sure it would work, I'm a little mystified why it doesn't. I couldn't get it to pass a global variable at all to the header.

  7. esmi
    Forum Moderator
    Posted 3 years ago #

    get_header() is a function - not a standard php include. Where do you want to test for Page 5 and what do you want to do if it is Page 5?

  8. bmwboarder
    Member
    Posted 3 years ago #

    If it is a function, wouldn't it have access to a global variable stated before the function?

    My project index page which is an index of a custom post type doesn't have the "current-menu-item" in the main navigation. I can fix it like this (an example that does fix another page):

    <?php if($post->ID==7){?>
            <style type="text/css">
                #bottom_nav .menu-item-110 a {
                    color: #333;
                }
            </style>
        <?php } ?>

    But I can't find anything to fit in the if statement that actually recognizes when it is on that page. I figured I could do basically the same thing with a php variable, but I haven't gotten that to work either. Thanks for the help.

  9. vtxyzzy
    Member
    Posted 3 years ago #

    Passing a variable works for me with this in the template before get_header():

    <?php
    global $mctemp; $mctemp='woohoo';
    ?>

    and this in header.php:

    <?php
    global $mctemp; echo '<!-- MCTEMP=' . $mctemp . ' -->';
    ?>
  10. bmwboarder
    Member
    Posted 3 years ago #

    Nice! That works! Thanks for helping me through that.

    I don't think I added "global" inside the header last time. Many thanks, that is nice to have working!

    Brandon

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.