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.

Switching ID with Category...

September 10, 2011 4:15pm

Subscribe [4]
  • #1 / Sep 10, 2011 4:15pm

    MarkerMaker

    220 posts

    Does anyone know how I can switch an ID depending on category.

    I have 5 categories assigned to my channel ‘Work’ these are ‘All’ which is the parent category, ‘Print’, ‘Website’, ‘Branding’ and ‘Marketing’ which are child categories of ‘All’.

    My code is as follow…

    <div id="pp-img">
        {exp:channel:entries channel="work" limit="1" status="New|Open"}
            {project_img}
        {/exp:channel:entries}
            <a href="http://{site_url}work/category/all" title="Back to Work"> ← Back to work</a>
        </div>

    I need to change the id ‘pp-img’ assigned to the opening ‘<div>’ when a project is given the category of ‘Website’ to ID ‘pw-img’.

    Only problem is this ‘<div>’ resides outside of the Channel Entries Tag pair.

    Does anyone have any suggestions to how I might achieve this ID switch?

  • #2 / Sep 12, 2011 1:52am

    John Henry Donovan

    12339 posts

    Hi Marker Maker,

    What does the URL look like for this page?

    If it was something like this

    http://www.,ydomain.com/template_group/categories/website

    Then you can use the {segment_3} variable to achieve what you want.

    <div id="{if segment_3 == 'website'}pw-img{if:else}pp-img{/if}">
        {exp:channel:entries channel="work" limit="1" status="New|Open"}
            {project_img}
        {/exp:channel:entries}
            <a href="http://{site_url}work/category/all" title="Back to Work"> ← Back to work</a>
        </div>

    Does that help?

  • #3 / Sep 12, 2011 7:08am

    MarkerMaker

    220 posts

    No I am afraid my URL is not like that. It is on my main ‘Work’ page but this is my project view template, linked to by a ‘title_permalink’ variable so the URL is like this…

    http://www.mydomain.com/work/view/project_title

  • #4 / Sep 12, 2011 1:53pm

    MarkerMaker

    220 posts

    Does anyone have any further thoughts on this?

  • #5 / Sep 13, 2011 12:39am

    Dan Decker

    7338 posts

    Marker Maker,

    Since you are limiting {exp:channel:entries} to 1, there is no reason your <div></div> pair can’t be inside the loop. By limiting to 1, it will only loop once and the <div> will not repeat. So you could do this:

    {exp:channel:entries channel="work" limit="1" status="New|Open"}
        <div id=“{if {category_name} == 'Website'}pw-img{if:else}pp-img{/if}”>
            {project_img}
            <a href="http://{site_url}work/category/all" title="Back to Work"> ← Back to work</a>
        </div>
    {/exp:channel:entries}

    Let us know if that suits your needs.

    Cheers,

  • #6 / Sep 13, 2011 7:21am

    MarkerMaker

    220 posts

    Of course ... It seems so simple now. I will try this out and let you know how I got on. Many thanks for your response.

  • #7 / Sep 13, 2011 8:42am

    MarkerMaker

    220 posts

    No that did not work… Could it be something to do with that ‘Website’ in my case is a child category of ‘All’ and this somehow effects the outcome?

    Here is the code copied from my document.

    {exp:channel:entries channel="work" limit="1" status="New|Open"}
        <div id="{if {category_name} == 'Website'}pw-img{if:else}pp-img{/if}">
            {project_img}
            <a href="http://{site_url}work/category/all" title="Back to Work"> ← Back to work</a>
        </div>
        <!--// pp-img / pw-img when sorted size!-->
        {/exp:channel:entries}
  • #8 / Sep 13, 2011 11:52am

    Mark Bowen

    12637 posts

    Hi Marker Marker,

    When you say it doesn’t work, what do you mean by that? Are you getting any output from the Channel Entries tag above or nothing?

    As a note on the above code it seems that you are missing the {categories} variable pair which is needed here in order for that category_name to make any sense to the code.

    Could you therefore try something like this instead :

    {exp:channel:entries channel="work" limit="1" status="New|Open"}
    
    {categories}
    <div id="{if {category_name} == 'Website'}pw-img{if:else}pp-img{/if}">
    {/categories}
    
    {project_img}
    <a href="http://{site_url}work/category/all" title="Back to Work"> ← Back to work</a>
    </div>
    
    {/exp:channel:entries}

    Thanks,

    Mark

  • #9 / Sep 13, 2011 12:24pm

    MarkerMaker

    220 posts

    I am getting output fine but of course the ‘<div>’ has the id of ‘pp-img’ and I would like it to have the id of ‘pw-img’ when the category is the child category “Website”.

    This is my code as it stands…

    {exp:channel:entries channel="work" limit="1" status="New|Open"}
        <div id="{if {category_name} == 'Website'}pw-img{if:else}pp-img{/if}">
            {project_img}
            <a href="http://{site_url}work/category/all" title="Back to Work"> ← Back to work</a>
        </div>
        <!--// pp-img / pw-img when sorted size!-->
        {/exp:channel:entries}

    and this is the output for that code…

    <div id="pp-img">
            <a href="http://my_url/images/uploads/project_img/project_img.jpg">http://my_url/images/uploads/project_img/project_img.jpg</a>
            <a href="http://my_url/work/category/all" title="Back to Work"> ← Back to work</a>
        </div>
        <!--// pp-img / pw-img when sorted size!-->

    As you can see the ‘<div>’ has an id of ‘pp-img’ and not the id of ‘pw-img’ that I was hoping for.

    I also tried adding the {categories} tag pair as you suggested might be an option but this had no effect apart from duplicating the ‘<div>’.

    Not sure where to go from hear… any help would be very much appreciated.

  • #10 / Sep 13, 2011 12:36pm

    Mark Bowen

    12637 posts

    Hi Marker Marker,

    Should each entry only be in one category or is it likely that perhaps they are in more than one?

    What pointed me to asking that is that above you mentioned that when you added in the {categories} variable pair you were getting multiple divs output.

    You are definitely going to need that tag pair to be able to parse the category_name variable as it won’t work without it but what I’m thinking is probably happening here is that you have that entry in more than one category?

    Does that make sense?

    Mark

  • #11 / Sep 13, 2011 1:03pm

    MarkerMaker

    220 posts

    Thanks for this, well I am not sure… My category ‘Website’ is a child category of ‘All’ so does that mean that my entry is in two categories ... ‘All’ and it’s child ‘Website’?

  • #12 / Sep 13, 2011 2:00pm

    Mark Bowen

    12637 posts

    Hi Marker Marker,

    If you go and edit your entry and take a look at the categories section are both Website and All selected there?

    If so then yes this will be the reason why this is happening.

    There is a way to fix this though which in your case will be two-fold :

    1 - Go to Admin > Channel Administration > Global Preferences
    Change Auto-Assign Category Parents to be No instead of Yes

    2 - Edit your entries which have categories already assigned and ensure that the All category isn’t selected. Re-save those entries and this should hopefully fix it for you.

    Thanks,

    Mark

  • #13 / Sep 13, 2011 2:43pm

    MarkerMaker

    220 posts

    Thanks however the issue I have is that I do need entries to share two categories for another location in my site where I display entries with the category of ‘All’. This category which is the main parent category is needed so that I can show All of my work at once and the child categories including ‘Website’ are there so that the end user can view work within that category also to narrow the results down a bit.

    My code is ...

    {exp:channel:entries channel="work" limit="1" status="New|Open"}
        <div id="{if {category_name} == 'Website'}pw-img{if:else}pp-img{/if}">
            {project_img}
            <a href="http://{site_url}work/category/all" title="Back to Work"> ← Back to work</a>
        </div>
        <!--// pp-img / pw-img when sorted size!-->
        {/exp:channel:entries}

    Is there any way I can add some kind of ‘and’ operator so that it will only switch to the ‘pw-img’ id if the entry has an category of ‘Website’ and ‘All’. So it has to be that both these conditions are met for it to switch and be true?

  • #14 / Sep 13, 2011 3:22pm

    Mark Bowen

    12637 posts

    Hi Marker Marker,

    Try this code instead then :

    {exp:channel:entries channel="work" limit="1" status="New|Open"}
    
    {categories limit="1"}
    <div id='{if "{category_name}" == "Website" AND "{category_name}" == "All"}pw-img{if:else}pp-img{/if}'>
    {/categories}
    
    {project_img}
    <a href="http://{site_url}work/category/all" title="Back to Work"> ← Back to work</a>
    </div>
    
    {/exp:channel:entries}

    That should hopefully get you what you need here.

    Mark

  • #15 / Sep 13, 2011 4:31pm

    MarkerMaker

    220 posts

    No this has not done it.

    This is my code from my template.

    {exp:channel:entries channel="work" limit="1" status="New|Open"}
        {categories limit="1"}
        <div id='{if "{category_name}" == "Website" AND "{category_name}" == "All"}pw-img{if:else}pp-img{/if}'>
        {/categories}
            {project_img}
            <a href="http://{site_url}work/category/all" title="Back to Work"> ← Back to work</a>
        </div>
        <!--// pp-img / pw-img when sorted size!-->
        {/exp:channel:entries}

    Is there any differences in using single and double quotes? I really thought that would have done it.

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

ExpressionEngine News!

#eecms, #events, #releases