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.

Set entries category id with global variable

October 28, 2012 4:43pm

Subscribe [3]
  • #1 / Oct 28, 2012 4:43pm

    Monarobase

    77 posts

    Hello,

    Is the following supposed to work ?

    Global variable called default_cat_id set to 5

    {exp:channel:entries channel="news" dynamic="no" year="2012" category="{default_cat_id}" orderby="date" sort="desc" }
    <h1>{title}</h1>
    {/exp:channel:entries}

    When I do it without a variable it works :

    {exp:channel:entries channel="news" dynamic="no" year="2012" category="5" orderby="date" sort="desc" }
    <h1>{title}</h1>
    {/exp:channel:entries}

    When I print :

    <h1>Category id is : {default_cat_id}</h1>

    I get :

    <h1>Category id is : 5</h1>

    Do you have any suggestions about how I could get this to work ?

    Thanks 😊

  • #2 / Oct 28, 2012 6:48pm

    ahmad saad

    364 posts

    Global variable will not work for EE tags, try to use snippets

  • #3 / Oct 28, 2012 8:54pm

    Monarobase

    77 posts

    The problem here is that I need to get the category URL, transform it to a category ID and specify the category ID to only show the entries of that category.

    I use a plugin (swanify) to get the category ID :

    {exp:swanify_category:get category_url=”{segment_1}”}

    I rearly need to be able to limit the entries to the ones in the category that’s in the URL.

    The following does not work :

    {exp:channel:entries channel="news" dynamic="no" year="2012" category="{exp:swanify_category:get category_url=”{segment_1}”}" orderby="date" sort="desc" }
    <h1>{title}</h1>
    {/exp:channel:entries}

    My customer want’s to be able to create a new news entry (with en english title, an optional french title, an optional spanish title), and select which languages it will appear in by ticking the corresponding categories. The langauge being in the URL (/en, /fr, /es …) and the categories being the same as the languages (‘en’, ‘fr’, ‘es’…).

    Thanks

     

     

     

  • #4 / Oct 29, 2012 5:48am

    ahmad saad

    364 posts

    this is simple , first try to change

    {exp:swanify_category:get category_url=”{segment_1}”}

    to

    {exp:swanify_category:get category_url="{segment_1}"}

    I notice u use ” not “

    if this not resolve u problem , you can use embeded varaibale and pass the cat_id from the main template like this:

    {embed="include/.news_list" cat_id='{exp:swanify_category:get category_url="{segment_1}"}'}

    then in the news list template change the code to :

    {exp:channel:entries channel="news" dynamic="no" year="2012" category="{embed:cat_id}" orderby="date" sort="desc" }
    <h1>{title}</h1>
    {/exp:channel:entries}
  • #5 / Oct 29, 2012 6:07am

    Monarobase

    77 posts

    I notice u use ” not “’

    I think it was my browser’s spell check or EE forums parser that replaced the code, the code was correct in my EE templates.

    The embed variable worked for the year {year} but not for the category…

    In the template :
    {embed="embed/.news_list" cat_id="1" year='2012'} 
    
    
    In the embed :
    {exp:channel:entries channel="news" dynamic="no" year="{year}" category="{cat_id}" orderby="date" sort="desc" }

    => doesn’t work

    In the template :
    {embed="embed/.news_list" cat_id="1" year='2012'} 
    
    In the embed :
    {exp:channel:entries channel="news" dynamic="no" year="{year}" category="5" orderby="date" sort="desc" }

    => works

  • #6 / Oct 29, 2012 7:10am

    ahmad saad

    364 posts

    u use the emebed var in the wrong way you must use it like this {embed:cat_id} not {cat_id}

  • #7 / Oct 29, 2012 7:20am

    Monarobase

    77 posts

    oops !

    That worked, thanks 😊

    I shame I have to use an embed to bypass this problem but at least it does the job !

  • #8 / Oct 30, 2012 11:00am

    Shane Eckert

    7174 posts

    Hey Monarobase,

    Awesome! I am glad that Ahmad was able to help you out here. The embed variable is an awesome tool, I use it very often.

    If you need anything else, please just let me know by opening a new thread.

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases