开发者

ExpressionEngine - PHP in template, Print something if entry has certain field value?

I have a template that is used for entries. The entries have a field that will always have 1 of 2 values. Can I write some PHP to show something different depending on the field value?

Ive tried the following but it gives me 开发者_开发技巧a PHP error:

<?php if($my_field == 'value1') { ?>
   <h3>Value 1</h3>
   <?php } else { ?>
   <h3>Value 2</h3>
<?php } ?> 

Thanks


You don't have to use PHP.

{if my_field == 'foo'}
    Value 1
{if:else}
    Value 2
{/if}

If you're planning on doing any EE tag processing within those conditionals, you shouldn't use the if:else syntax, as with it, the content between the conditionals will always be parsed, but just not displayed, which needlessly uses server resources and increases load time.

So in that case, use simple conditionals instead:

{if my_field == 'foo'}
    Value 1
{/if}
{if my_field == 'bar'}
    Value 2
{/if}

See: http://expressionengine.com/user_guide/templates/globals/conditionals.html


You can use ExpressionEngine's Conditional Global Variables to display your content, without having to use PHP in your templates.

Rewriting your example using native ExpressionEngine's Simple Conditional tags would result in the following:

{exp:channel:entries channel="channel_name" dynamic="off"}
    {if "my_field" == "value1"}
        Value 1
    {/if}

    {if "my_field" == "value2"}
        Value 2
    {/if}
{/exp:channel:entries}

You can use simple or complex conditionals anywhere in your templates, with the former being less resource expensive, but ExpressionEngine's Parse Order (PDF, 32 KB) may affect how they're evaluated and replaced.

In most cases, you'll need to ensure your custom fields and conditionals are within the {exp:channel:entries} tag loop for the values to be properly output and tested when the page is being built.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜