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.

Found a Category Number now I need some help putting it to good use - perhaps turning it into a variable - (exp:channel:prev_entry category="&#63

February 27, 2012 10:12pm

Subscribe [4]
  • #1 / Feb 27, 2012 10:12pm

    Kubik101

    155 posts

    OK
    This is a little hard to explain.
    What I am about to show by no means works but hopefully gives you an idea of what I am trying to achieve.

    I am using gwcode to handle my categories I can find the category id number using the gwcode below which needs to be wrapped in a channel entries tag so it can find the entry’s url_title.

    So this code gets my category number which for example is “7” - So I can print the cat_id I am after.

    {exp:channel:entries}{exp:gwcode_categories entry_id='{entry_id}' style='linear' last_only='yes'}{cat_id}{/exp:gwcode_categories}{/exp:channel:entries}

    Then I want to use that cat number to determine the category the next/prev buttons use.

    {exp:channel:prev_entry category="CAT ID HERE"}<a href="http://{site_url}{segment_1}/{url_title}" title="Previous Vehicle" class="prevButton"><span>Previous Vehicle</span></a>{/exp:channel:prev_entry}
    {exp:channel:next_entry category="CAT ID HERE"}<a href="http://{site_url}{segment_1}/{url_title}" title="Next Vehicle" class="nextButton"><span>Next Vehicle</span></a>{/exp:channel:next_entry}

    Obviously I want this to be dynamic and can’t hard code the category number.
    So I was thinking of doing something like this - which obviously didn’t work.

    {preload_replace:current_category="{exp:channel:entries}{exp:gwcode_categories entry_id='{entry_id}' style='linear' last_only='yes'}{cat_id}{/exp:gwcode_categories}{/exp:channel:entries}"}
        
    {exp:channel:prev_entry category="{current_category}"}<a href="http://{site_url}{segment_1}/{url_title}" title="Previous Vehicle" class="prevButton"><span>Previous Vehicle</span></a>{/exp:channel:prev_entry}
    {exp:channel:next_entry category="{current_category}"}<a href="http://{site_url}{segment_1}/{url_title}" title="Next Vehicle" class="nextButton"><span>Next Vehicle</span></a>{/exp:channel:next_entry}

    I’m hoping that gets the point across, happy to explain more if need be.

    My URL structure is: http://domain_name.com.au/channel/url_title

     

  • #2 / Feb 28, 2012 12:56am

    Cheif

    626 posts

    Grab a copy of stash

    Set it up something like this:

    {exp:channel:entries}
        {exp:gwcode_categories entry_id='{entry_id}' style='linear' last_only='yes'}
            {exp:stash:set name="catid"}{cat_id}{/exp:stash:set} //Set the variable
        {/exp:gwcode_categories}
    {/exp:channel:entries}
    
    
    //Call the variable wherever you damn well please
    
    {exp:channel:prev_entry category="{exp:stash:get name='catid'}"}
        <a href="{site_url}{segment_1}/{url_title}" class="prevButton" title="Previous Vehicle"><span>Previous Vehicle</span></a
    {/exp:channel:prev_entry}


    That’s the tip of the iceberg !! over to you to find the rest 😛

  • #3 / Feb 28, 2012 12:09pm

    Shane Eckert

    7174 posts

    Hello Kubik101,

    Thank you for posting your question here on the ExpressionEngine forums.

    I think I follow what you are trying to accomplish. It looks like the Add-On that Chief has mentioned would help you with this. Especially with the pagination and catid.

    I can also move this thread over to Community Help if you would like. That will get a lot more eyes on it and a lot more ideas on how to accomplish your goal.

    Let me know what you think of both moving the thread and using stash as Chief suggested.

    Cheers,

  • #4 / Feb 28, 2012 11:26pm

    Kubik101

    155 posts

    Thanks for the suggestion Chief.

    Straight off the bat it seemed to break gwcode by outputting the Category Group name wrapped in a h2 with the category name following.

    After some experimenting to see if stash was working, I found that stash can not place what it has set in something that is not inside a channel entries tag.

    Which also means in this case it can not throw the value inside the exp:channel:prev_entry tag either.

    I hard coded the cat_id as you can see in the example.
    I got my red 31 but no action on the buttons.

    {exp:channel:entries}{exp:stash:set name="current_category"}31{/exp:stash:set}{/exp:channel:entries}
    {exp:channel:prev_entry category="{exp:stash:get name='current_category'}"}<a href="http://{site_url}{segment_1}/{url_title}" title="Previous Vehicle" class="prevButton"><span>Previous Vehicle</span></a>{/exp:channel:prev_entry}
    {exp:channel:next_entry category="{exp:stash:get name='current_category'}"}<a href="http://{site_url}{segment_1}/{url_title}" title="Next Vehicle" class="nextButton"><span>Next Vehicle</span></a>{/exp:channel:next_entry}
    <br class="clear" >
    {exp:channel:entries}<span>{exp:stash:get name='current_category'}</span>{/exp:channel:entries}

    So 2 problems, stash seems to break gwcode’s output of the category number and stash can not seem to throw it’s value inside exp:channel:prev_entry

    I’m hoping there may be another solution out there.

    Shane, if you could move the post as you suggested, some more eyes would be good.

    Thanks.
    =)

  • #5 / Feb 29, 2012 1:36pm

    Leon Dijk

    131 posts

    Hi Trent,

    You asked me to join this thread, so here I am 😊

    I haven’t used GWcode Categories in combination with Stash myself and I’m swamped with work at the moment so I can’t give it a try myself, but I think I can answer your question regarding the first problem you’re having.

    If you use

    {exp:gwcode_categories}{/exp:gwcode_categories}

    without any so called “tagdata” (something in between the opening and closing tag), GWcode Categories assumes you just want to see if the plugin works and so it outputs category group names in H2 tags and the list of categories underneath that.

    That’s what you’re getting now, probably because the stash tags inside it are already being parsed and not providing any output?

    Anyway, I’ve played with the code for a bit and this is what I came up with. Not functional, but it may get you a bit further hopefully.

    {exp:channel:entries channel="test" entry_id="2" dynamic="off"}
     {exp:stash:set name="catid" parse_tags="yes"}
      {exp:gwcode_categories entry_id='{entry_id}' style='linear' last_only='yes'}{cat_id}{/exp:gwcode_categories}
     {/exp:stash:set}
    {/exp:channel:entries}
    
    {embed="site/.navlinks"}

    And the code for the site/.navlinks template:

    CatID: {exp:stash:get name='catid'}
    
    {exp:channel:next_entry channel="test" category="{exp:stash:get name='catid'}"}
        Next entry: <a href="http://{path=site/comments}">{title}</a>
    {/exp:channel:next_entry}
    
    {exp:channel:prev_entry channel="test" category="{exp:stash:get name='catid'}"}
        Previous entry: <a href="http://{path=site/comments}">{title}</a>
    {/exp:channel:prev_entry}

    This does somewhat work, as you can see the CatID: outputs the correct value.

    However, it isn’t being sent to the exp:channel:next_entry or exp:channel:prev_entry tags. In my template debug screen, I see this:

    Tag: {exp:channel:next_entry channel="test" category="M00o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr"}

    ..so the value for the category parameter is wrong. Not sure why, but probably a parsing order issue. Google for that value and you’ll see some posts about it.

    Hopefully this will get you a bit further. I wish I had more time to try things, but I have deadlines to deal with at the moment 😊

  • #6 / Feb 29, 2012 5:52pm

    Shane Eckert

    7174 posts

    Hello Kubik101,

    Many thanks to Leon!

    I will move this post as requested.

    Also, the Parse Order order would be easier to diagnose if you include the entire template.

    Thanks again for posting your question.

    Cheers,

  • #7 / Feb 29, 2012 6:48pm

    Kubik101

    155 posts

    Thank you Leon.

    Putting the parse tags property on got me a step further.

    The following code gives me my red ID number at the bottom in the span, which means the gwcode is working nicely.

    So the only problem seems to be getting the value into exp:channel:prev_entry and next_entry tags.

    Any ideas?

    {exp:channel:entries}{exp:stash:set name="current_category" parse_tags="yes"}{exp:gwcode_categories entry_id='{entry_id}' style='linear' last_only='yes'}{cat_id}{/exp:gwcode_categories}{/exp:stash:set}{/exp:channel:entries}
    {exp:channel:prev_entry channel="new-vehicles" category="{exp:stash:get name='current_category'}"}<a href="http://{site_url}{segment_1}/{url_title}" title="Previous Vehicle" class="prevButton"><span>Previous Vehicle</span></a>{/exp:channel:prev_entry}
    {exp:channel:next_entry channel="new-vehicles" category="{exp:stash:get name='current_category'}"}<a href="http://{site_url}{segment_1}/{url_title}" title="Next Vehicle" class="nextButton"><span>Next Vehicle</span></a>{/exp:channel:next_entry}
    <br class="clear" >
    {exp:channel:entries}<span>{exp:stash:get name='current_category'}</span>{/exp:channel:entries}
  • #8 / Feb 29, 2012 10:56pm

    Cheif

    626 posts

    I’d look at saving the stash to a snippet

    {exp:stash:set name="current_category" parse_tags="yes" type = 'snippet'}
     {exp:gwcode_categories entry_id='{entry_id}' style='linear' last_only='yes'}{cat_id}{/exp:gwcode_categories}
    {/exp:stash:set}

    Then in your pagination call the snippet

    {exp:channel:prev_entry channel="new-vehicles" category="{current_category}"}
     <a href="http://{site_url}{segment_1}/{url_title}" title="Previous Vehicle" class="prevButton"><span>Previous Vehicle</span></a>
    {/exp:channel:prev_entry}

    Stash:  A ‘snippet’ works just like snippets in ExpressionEngine, and can be used directly as a tag {my_variable} If using snippets, please be careful to namespace them so as not to overwrite any existing EE globals.

  • #9 / Mar 01, 2012 12:41am

    Kubik101

    155 posts

    Chief the code example you gave didn’t work.
    gwcode needs to be wrapped in a channel entries tag so it can find the entry_id
    Also adding type=“snippet” to the code below broke things and no longer outputted the cat_id inside my red span.

    The code below is working and outputting the cat_id using stash (the red span), but stash can’t get it inside category=”” of the channel:prev_entry and next_entry tags. Either that or EE isn’t allowing that to be parsed there.

    Any more ideas?

    {exp:channel:entries}{exp:stash:set name="current_category" parse_tags="yes"}{exp:gwcode_categories entry_id='{entry_id}' style='linear' last_only='yes'}{cat_id}{/exp:gwcode_categories}{/exp:stash:set}{/exp:channel:entries}
        {exp:channel:prev_entry channel="new-vehicles" category="{exp:stash:get name='current_category'}"}<a href="http://{site_url}{segment_1}/{url_title}" title="Previous Vehicle" class="prevButton"><span>Previous Vehicle</span></a>{/exp:channel:prev_entry}
        {exp:channel:next_entry channel="new-vehicles" category="{exp:stash:get name='current_category'}"}<a href="http://{site_url}{segment_1}/{url_title}" title="Next Vehicle" class="nextButton"><span>Next Vehicle</span></a>{/exp:channel:next_entry}
        <br class="clear" >
        {exp:channel:entries}<span>{exp:stash:get name='current_category'}</span>{/exp:channel:entries}
  • #10 / Mar 01, 2012 1:06am

    Cheif

    626 posts

    I left off the {entries} tags for the sake of cleaner presentation in the forums I never intended them to be left off completely.

    Basically, you need to make the variable a snippet as snippets get parsed B4 {exp} tags.

    I think you may benefit from reading the manual for Stash as what you are trying to achieve is do-able, it comes down to parse order and usage.

    Docs: https://github.com/croxton/Stash
    Help: http://devot-ee.com/add-ons/support/stash/
    Lows Parse Order PDF: http://loweblog.com/downloads/ee-parse-order.pdf

  • #11 / Mar 01, 2012 4:21am

    Mark Croxton

    319 posts

    Hi Kubik

    Parse order is the key issue here. An EE template parses module tags like the layers of a russian doll. The first outer layer is parsed top to bottom, then the next inner layer is parsed top to bottom, and so on…

    If you want to GET a Stash variable in the same template you SET it, you need to make sure the GET happens AFTER the SET.

    If you turn on template debugging you will see that Stash outputs what it is doing in your template.

    Stash has a special parameter parse_tags=“yes” that allows it to parse the tags it is wrapping immediately. It can do this to an arbitrary depth specified by parse_depth=“X” where X is the number of layers of tags to parse. By default it will only parse 1 layer.

    Your code would need to be refactored like this if you want to use the Stash variable in the same template:

    {exp:stash:set name="catid" parse_tags="yes" parse_depth="2"}{exp:channel:entries limit="1"}{exp:gwcode_categories entry_id='{entry_id}' style='linear' last_only='yes'}{cat_id}{/exp:gwcode_categories}{/exp:channel:entries}{/exp:stash:set}
    
    
    {exp:channel:prev_entry category="{exp:stash:get name='catid'}" parse="inward"}
        <a href="http://{site_url}{segment_1}/{url_title}" title="Previous Vehicle" class="prevButton"><span>Previous Vehicle</span></a>
    {/exp:channel:prev_entry}

    Alternatively, you could do this:

    {exp:channel:entries limit="1"}
    
    {exp:stash:set name="catid" parse_tags="yes"}{exp:gwcode_categories entry_id="{entry_id}" style="linear" last_only="yes"}{cat_id}{/exp:gwcode_categories}{/exp:stash:set}
    
    {exp:channel:prev_entry category="{exp:stash:get name='catid'}" parse="inward"}
        <a href="http://{site_url}{segment_1}/{url_title}" title="Previous Vehicle" class="prevButton"><span>Previous Vehicle</span></a>
    {/exp:channel:prev_entry} 
    
    {/exp:channel:entries}

    Note the use of parse=“inward” on the exp:channel:prev_entry tag. This tells the template parser to parse any tags passed as a parameter.

     

     

  • #12 / Mar 01, 2012 4:09pm

    Leon Dijk

    131 posts

    Nice post Mark! Great explanation. I should definitely use stash more often 😊
    Kubik, I hope Mark’s solution solves your problem!

  • #13 / Mar 09, 2012 8:12pm

    Kubik101

    155 posts

    Apologies for not responding sooner, I didn’t receive a notification email for some reason.

    Mark, I could kiss you my friend.
    It bloody works..!! I am so so happy, I had given up on this as I didn’t think there was a solution.

    I can’t believe it, I am so grateful for everyones help here.
    It has brought some great interactivity to the site.

    Thank you thank you thank you.

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

ExpressionEngine News!

#eecms, #events, #releases