0

I have a news section on our website and I want to only show the current years articles unless it is January, February or March. Since those months are at the beginning of the year, I don't want the page to not have any content.

I have been able to figure out the PHP to get the current date and subtract 2 years (so I can show last year's posts).

<?$date = strtotime('{current_time format="%Y"} -2 year');?>

I also figured out how to do the {if/if:elseif} statements to check which month we are currently in and if it is January do this. February do this. March do this. All the other months do that.

{if {current_time format="%m"} == "01"}
<p>This is January</p>
{if:elseif {current_time format="%m"} == "02"}
<p>This is February</p>
{if:elseif {current_time format="%m"} == "03"}
<p>This is March</p>
{if:else}
<p>This is all the other months</p>
{/if}

Both of the codes above work well independently of each other, but when I put them together the {if:elseif} quits working and it defaults to the last {if:else} content.

Anyone know why? Here is the final code I tried:

{if {current_time format="%m"} == "01"}
<?$date = strtotime('{current_time format="%Y"} -2 year');?>
{if:elseif {current_time format="%m"} == "02"}
<?$date = strtotime('{current_time format="%Y"} -2 year');?>
{if:elseif {current_time format="%m"} == "03"}
<?$date = strtotime('{current_time format="%Y"} -2 year');?>
{if:else}
<?$date = strtotime('{current_time format="%Y"} -1 year');?>
{/if}
1

Would this work:

Configure the template to execute PHP before EE code.

Handle your conditionals in PHP rather than EE.

Something like:

<?

$todays_month = date('n');

if ($todays_month == "1") {

echo "your EE code for January goes here";

} elseif ($todays_month == "2") {

echo "your EE code for February goes here";

} else {

echo "your EE code for March and after goes here";

}

?>
| improve this answer | |
  • This, you're not going to be able to influence PHP with EE conditionals. If you check out Low's EE Parse Order Document it's plainly obvious that your advanced conditionals (if:elseif is advanced) are parsed way after PHP, either in before or after mode). The correct answer s to serve conditionals like this via PHP. If you absolutely, definatly have to mix EE and PHP conditionals You'll need to use simple conditionals (i.e. if without any else's) and use PHP on Output, but thats just bonkers... – Blatant May 13 '15 at 17:06
0

After doing some extensive research on this, I found the problem.

Advanced conditionals in the ‘global’ template scope (not within a tag) are generally parsed by the EE template parser near the end of the process. This unfortunately means that any tags within non-matching conditions are parsed first, before being removed. This can have a significant performance impact if the nested tags are doing a lot of processing, such as is the case with the {exp:channel} tag.

I did however find a plugin by Mark Croxton called "ifelse" that forces the advanced conditional to be processed first, but it is not working with my version of EE (2.4.0) even though it states that it can so it might be conflicting with one of my other plugins.

Anyone else come across a solution to force advance conditionals to be processed sooner for ExpressionEngine?

| improve this answer | |
  • 1
    Hi Trevor, rather than adding more info as an "answer" please update your original question. – AllInOne Jan 9 '15 at 22:29
  • The handling of advanced conditionals is much improved starting in EE 2.9.0 – AllInOne Jan 12 '15 at 14:54
  • Yet still won't mix with PHP well due tothe parse order. As AllInOne said, it's better to edit your OP than add answers that have no answer... On this post, there's absolutely no benifit at all in trying to force EE to parse advanced conditionals sooner, use PHP and native EE libs. Also Mark's plugin simply moves the ifelse to the module tag parsing phase, doesn't change when your PHP is parsed. – Blatant May 13 '15 at 17:10
  • Sorry Blatant and AllInOne about posting as an "Answer". Finding out that it was the parsing order was the answer for me, but it forced a new question because of that answer. – Trevor Griffith May 14 '15 at 19:46
0

Why not just the mighty Low's Yearly Archive?

http://gotolow.com/addons/low-yearly-archives

From the looks of what you're trying to do it could work without using php in your templates. You'll then want to write conditionals using his tag pairs to check which month you're in. Should be pretty straightforward.

| improve this answer | |
  • Awesome! Just mark my answer as the answer:) – Mark Busnelli Jr May 14 '15 at 19:48
  • If you need further help just let me know. The documentation has some nice examples too. – Mark Busnelli Jr May 14 '15 at 19:49
0

Do not ever mix EE code with PHP code. This not only slows down things significally, but you may easily run into unpredictable issues that will be hard to debug (like this one).

If you're really not sure you can't achieve what you want with EE add-ons, turn your code into pure PHP.

Something like this:

<?php
if (ee()->localize->format_date('%m')=="01")
{
    $date = strtotime(ee()->localize->format_date('%m').' -2 year');
{
    else
{
    //some more code
}    
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.