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.

Doing IF statements in weblog entries

May 05, 2010 3:20pm

Subscribe [3]
  • #1 / May 05, 2010 3:20pm

    Carl W Crawley

    136 posts

    Having a small issue with some conditionals on a weblog entry.

    Essentially I have a series of entries in a weblog and the client has asked that certain ones appear on certain pages. I have therefore created the following:

    {if segment_1 == ""}
        {assign_variable:entryid="53"}
    {if:elseif segment_1 == "about"}
        {assign_variable:entryid="99"}
    {if:elseif segment_1 == "whatwedo"}
        {assign_variable:entryid="71"}
    {if:elseif segment_1 == "venues"}
        {assign_variable:entryid="103"}
    {if:elseif segment_1 == "wowfactors"}
        {assign_variable:entryid="98"}
    {if:elseif segment_1 == "news"}
        {assign_variable:entryid="107"}
    {if:elseif segment_1 == "contact"}
        {assign_variable:entryid="53"}
    {/if}
                    
    {exp:weblog:entries weblog="whatwedo" entry_id="{entryid}" disable="member_data|pagination|trackbacks|categories|category_fields" limit="1"}
    ......
    {/exp:weblog:entries}

    However, whatever page I’m on - the value of entryid always seems to be 53.

    Alternatively, I’ve tried unsucessfully to try it as follows :

    {exp:weblog:entries 
        weblog="whatwedo" 
        limit="1" 
        dynamic="off" 
        disable="member_data|pagination|trackbacks|categories|category_fields"
                            
    {if segment_1 == ""}
        entry_id="53"
    {if:elseif segment_1 == "about"}
        entry_id="99"
    {if:elseif segment_1 == "whatwedo"}
        entry_id="71"
    {if:elseif segment_1 == "venues"}
        entry_id="103"
    {if:elseif segment_1 == "wowfactors"}
        entry_id="98"
    {if:elseif segment_1 == "news"}
        entry_id="107"
    {if:elseif segment_1 == "contact"}
        entry_id="53"
    {/if}
                            
    }
    
    ...
    
    {/exp:weblog:entries}

    I don’t really want to have to repeat my {exp:weblog:entries} and HTML for *every* if statement - I’m sure I’ve done it like this similar in the past, but can’t for the life of me remember….

    Cheers!

    C.

  • #2 / May 05, 2010 3:42pm

    lebisol

    2234 posts

    1st example should work without need for weblog tag.
    What is the actual URL structure?
    site.com/index.php/segment_1/segment_2/segment_3 is the approach.

  • #3 / May 05, 2010 3:46pm

    Carl W Crawley

    136 posts

    yup - it’s http://www.site.com/about/ for example…

    I thought it *should* work as well, but I’m not understanding why it doesn’t…. debug doesn’t give me anything useful to go on :(

  • #4 / May 05, 2010 3:54pm

    Lisa Wess

    20502 posts

    It shouldn’t work, see the docs on assign variable:

    Variable assignment and replacement occurs instantly when the template is loaded and may not be affected by the result of another tag’s output.

  • #5 / May 05, 2010 3:57pm

    Carl W Crawley

    136 posts

    ahhh balls :(

    So whats the recommended way of doing it - loading the entire block of code based on the slug entry? PHP?

  • #6 / May 05, 2010 3:58pm

    Lisa Wess

    20502 posts

    Why not make Pages of those?  Then their URI will be in segment 1 natively.

  • #7 / May 05, 2010 4:00pm

    Carl W Crawley

    136 posts

    yes, I had thought of that - however, if I hadn’t already created the articles/content and the client been trained on how to use the system, thats probably what I’d be doing :/

  • #8 / May 05, 2010 4:01pm

    Lisa Wess

    20502 posts

    You can add Pages URIs so the existing content by clicking on the Pages tab.  It’s just one more step to add to the client’s workflow, so shouldn’t be that hard to train…

  • #9 / May 05, 2010 6:06pm

    Rob Sanchez

    335 posts

    How about some PHP on input?

    <?php
    global $IN;
    
    switch($IN->fetch_uri_segment(1))
    {
        case 'about':
            $entry_id = 99;
            break;
        default:
            $entry_id = 53;
    }
    ?>
    {exp:weblog:entries entry_id="<?php echo $entry_id; ?>"}

    Also, I think your second example might work if you converted your advanced conditionals to simple:

    {if segment_1 == ""}
        entry_id="53"
    {/if}
    {if segment_1 == "about"}
        entry_id="99"
    {/if}
    {if segment_1 == "whatwedo"}
        entry_id="71"
    {/if}
  • #10 / May 05, 2010 6:33pm

    e-man

    1816 posts

    Rob’s code will definitely work but for these “my client wants to control this…” have a look a Low Variables which can be made to parse early so they can be used as parameters in entry tags.
    http://loweblog.com/software/low-variables/

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

ExpressionEngine News!

#eecms, #events, #releases