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.

Categories and multiple IF statements

July 06, 2007 8:05pm

Subscribe [2]
  • #1 / Jul 06, 2007 8:05pm

    ryal001

    42 posts

    Can anybody tell me why this doesn’t work?  The idea is that if the viewer selects a story to “read more” or to comment on, the page they are taken to has a side bar that has a non-changing description of the category that the story belongs to.  Thus, if the category is racing the side bar will have a little intro on racing at our Club.  These side bar stories are just normal weblog entries, one for each category, that are referred to by entry_id.

    {exp:weblog:category_heading weblog="{my_weblog}"}
    
                    <h1 class="first">{category_name}</h1>
                    
     
                    {if category_name == "Scholarship"}{exp:weblog:entries weblog="{my_weblog}" entry_id="9"}
                        {if:elseif category_name == "Racing"}{exp:weblog:entries weblog="{my_weblog}" entry_id="10"}
                    {/if}
                        {body} 
                        {if allow_comments}
                            <dl class="nav3-nobullet">
                                <dt><a href="http://{url_title_path="> Comments</a> ({comment_total})</dt>
                            </dl>
                        {/if}
                        {/exp:weblog:entries}           
    
             {/exp:weblog:category_heading}

    Using the above code, if the category of the story is Scholarship it works but if the category is Racing nothing happens. If I swap Racing and Scholarship (and their entry_id numbers) then Racing works and Scholarship doesn’t.  So it seems that the code stops working at the first “if”.

    Maybe I should use PHP to create a variable to contain the entry_id number?

  • #2 / Jul 06, 2007 8:53pm

    allgood2

    427 posts

    Try fiddling with spacing.  For example:

    {if category_name == "Scholarship"}
    
             {exp:weblog:entries weblog="{my_weblog}" entry_id="9"}
    
    {if:elseif category_name == "Racing"}
    
             {exp:weblog:entries weblog="{my_weblog}" entry_id="10"}
    
    {/if}

    If that doesn’t work, I’d try using cat_id over cat_name.  But I’ve had a great deal of success with making really long and complex if statements work like a charm, just by adjusting their spacing. Go figure.

  • #3 / Jul 06, 2007 9:01pm

    ryal001

    42 posts

    Thanks.  I tried both suggestions but neither made a difference in this case.

  • #4 / Jul 06, 2007 9:11pm

    ryal001

    42 posts

    In fact I think I’ve discovered the problem: the first {exp:weblog:entries} tag is being parsed (or whatever the correct terminology is) regardless of the IF statement.  I replaced category_name == “Scholarship” with John == “Scholarship” and it still included the scholarship weblog entry.

    So, I will try using the IF statement to create the entry_id number and then have only one {exp:weblog:entries} tag.

  • #5 / Jul 06, 2007 9:31pm

    Lisa Wess

    20502 posts

    You can’t do that.

    First - you can’t nest the weblog entries tag inside the category heading like that and expect to get good results.

    Second - you have to keep containers together due to parsing, you can not break them up in the way that you have.

    Now - I’m confused as to what you’re doing.  You have a description for each category that you want shown at some point, correct?  Why not use the category description for or another category custom field to handle this?

  • #6 / Jul 06, 2007 9:57pm

    ryal001

    42 posts

    Thanks, category description will work.  I never thought of the obvious. 

    I will have to put tags in the category description to get paragraphs formatted properly though, and I can’t have comments associated or use the description for anything else.  Nevertheless it does seem to be the best solution at this stage.

    Complete code is now:

    {exp:weblog:category_heading weblog="{my_weblog}"}
    
        <h1 class="first">{category_name}</h1>
        
         
        <div class="justified">{category_description}</div>
                            
    {/exp:weblog:category_heading}
  • #7 / Jul 06, 2007 10:10pm

    allgood2

    427 posts

    Yeah, it helps to actually pay attention to the code in use. Lisa’s correct you really can’t do what your trying to do, especially, breaking the open and closing {exp:weblog:entries} tags.  But, you can use segments, category_descriptions, or separate the category display article into a secondary weblog, that uses the same custom categories as the category archive or list that you’re attempting to display.

    If you use the url {segment} so a statement like:

    {if segment_3=="C9"}
    
           {exp:weblog:entries weblog="{my_weblog}" entry_id="9"}{body}{/exp:weblog:entries}
    
    {if:elseif segment_3=="C10"}
    
           {exp:weblog:entries weblog="{my_weblog}" entry_id="10"}{body}{/exp:weblog:entries}
    
    {if:else}
    
           {exp:weblog:entries weblog="{my_weblog}" entry_id="1"}{body}{/exp:weblog:entries}
    
    {/if}

    If you use, two weblogs, then the code would look something like

    {exp:weblog:category_heading}
           <h2>{category_name}</h2>
    <p> {/exp:weblog:category_heading}<br />
        <br />
    {exp:weblog:entries weblog="sectional" orderby="date" sort="desc" limit="1"}{summary}{/exp:weblog:entries}</p>
    
    <p>        </p><h3>Publication List</h3>
    <p>    <ul>{exp:weblog:entries weblog="main" orderby="date" sort="desc" limit="10"}</p>
    
    <p>       <li><a href="http://{title_permalink=main/articles}" title="{title}">{title}</a></li><br />
        <br />
        {/exp:weblog:entries}</ul>

    The category ID in the url, will filter the articles from both weblogs simultaneously, so if your 2nd weblog, only contains one post for each category, that post will always display, or it should. I typically use the segment tag.

  • #8 / Jul 06, 2007 10:11pm

    Lisa Wess

    20502 posts

    You could use a category custom field, they allow all sorts of formatting; that might be a good option for you.

    As for not allowing comments on them - well, I can’t think of a good option there, but do you really need commenting on the description itself?

  • #9 / Jul 06, 2007 10:43pm

    ryal001

    42 posts

    Thank you both for your responses; I’m learning EE as fast as I can.

    I am going with the URL segment option at this stage.  It’s a bit long winded but it does exactly what I want and allows me to have a default entry if I don’t have a specific side bar description for the category chosen.

    The reason I want the “description” to be a standard entry is to allow simple editing (although editing the category decription is not hard, it’s less convenient if I want to allow other less technical people to update it) and secondly to allow comments.  The reason for comments is that people might say things like “I did Scholarship and had a great time” and so give some “life” or “community” to the descripton. Well, we’ll see anyway.

    The final code is:

    {exp:weblog:category_heading weblog="{my_weblog}"}
    
          <h1 class="first">{category_name}</h1>
          
         
          <div class="justified">
                    
              {if segment_4=="scholarship"}
                  {exp:weblog:entries weblog="{my_weblog}" entry_id="9"}
                      {body} 
                      {if allow_comments}
                          <a href="http://{url_title_path=">Comments</a> ({comment_total})
                      {/if}
                  {/exp:weblog:entries}
    
               {if:elseif segment_4=="racing"}
                  {exp:weblog:entries weblog="{my_weblog}" entry_id="10"}
                      {body}
                      {if allow_comments}
                           <a href="http://{url_title_path=">Comments</a> ({comment_total})
                      {/if}
                {/exp:weblog:entries}
                     
                {if:else}
                    {exp:weblog:entries weblog="{my_weblog}" entry_id="1"}{body}{/exp:weblog:entries}
                {/if}
    
             {/exp:weblog:category_heading}

    Thanks again 😊

  • #10 / Jul 06, 2007 10:47pm

    Lisa Wess

    20502 posts

    You do realize, of course, that you could greatly simplify this by using related entries, if you want to pull it in that way?

    I’m not a fan of complicated templates or conditionals when there are much easier ways to handle this. You might find that related entries would greatly simplify this while giving you the easy editing and commenting features you want.  Worth looking at. =)

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

ExpressionEngine News!

#eecms, #events, #releases