ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Conditional php - if link target is current page

September 14, 2011 6:03am

Subscribe [2]
  • #1 / Sep 14, 2011 6:03am

    JamesBrighton

    146 posts

    I need to use some conditional PHP to add a class of ‘current’ to links in a menu ive made when your on their page. Currently im doing this with {if last_segment}, which looks for a url value and adds a class if the right one exists. This works fine but it will break if the page url changes in the future. Is there a better future proof way of doing things? Thanks

  • #2 / Sep 14, 2011 11:50am

    Mark Bowen

    12637 posts

    Hi James,

    Could you possibly give a little more information as to exactly what you’re doing here. I understand what you’re trying to do but don’t quite follow why when a url_title changes that it would break?

    If I’m following correctly then I take it that you have a single entry page :

    http://www.example.com/template-group/template-name/entry-title-here

    and on that page you have all the usual output of that entry and then in your menu you want to show that page with a different class, yes?

    So you would have something like this in the menu :

    {exp:channel:entries
        channel="default_site"
        dynamic="no"
        disable="member_data|trackbacks|categories|category_fields|pagination"}
    
    <a href="http://{url_title_path=%22template-group/template-name%22}" title="{title}" class="current">{title}</a>
    
    {/exp:channel:entries}

    This would then show the entry that you’re currently viewing differently to the rest. Is that what you’re doing here?

    If so then if the url_title of the single entry changes then this method should still work.

    Please let me know if I’m wrong in what you’re trying to achieve here though.

    Thanks,

    Mark

  • #3 / Sep 14, 2011 11:52am

    Mark Bowen

    12637 posts

    Agggh the forums just stripped out the conditional I had placed in the code.

    Give me a minute and I’ll upload it somewhere instead.

    Cheers,

    Mark

  • #4 / Sep 14, 2011 11:58am

    Mark Bowen

    12637 posts

    Okay I’ve uploaded the code here - http://d.pr/Dokn.

    Sorry about that but seems that the forum didn’t like the conditional within the link like that. Probably a protection thing.

    The conditional on it’s own would be like this :

    {if last_segment == url_title} class="current"{/if}

    Thanks,

    Mark

  • #5 / Sep 15, 2011 5:43am

    JamesBrighton

    146 posts

    Ah, I hadn’t thought about using url_title, i just used a static value. I think I can figure this out. Thanks

  • #6 / Sep 15, 2011 10:55am

    Mark Bowen

    12637 posts

    Hi James,

    Just wanted to see if you’re good to go on this one now?

    Is it okay for me to close out the thread?

    Thanks,

    Mark

  • #7 / Sep 15, 2011 1:10pm

    JamesBrighton

    146 posts

    Im going to revisit this later but yes, I think I can do it with the method you suggested. Thanks

  • #8 / Sep 15, 2011 1:48pm

    Mark Bowen

    12637 posts

    Hi James,

    Excellent. I’m going to go ahead and close this thread then but if anything else crops up then please don’t hesitate to post again as needed.

    Cheers,

    Mark

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases