Jump to content

IF Statement That Contains HTML and PHP


andymikester

Recommended Posts

I am using Wordpress and I want to wrap some Wordpress PHP and HTML inside a regular PHP IF statement.

I read through some basic PHP tutorials and this is what I have so far:

[code]<?php query_posts('cat=2&year=2006&monthnum=10'); ?>
<?php
if ($posts >= 1) {
<dl>
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<dt>October</dt>
<dd><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"></a></dd>
<?php endwhile; endif; ?>
</dl>
}
?>[/code]

So I query the database with Wordpress and fill $posts with the requested posts from the database. Then I start my IF statement. IF there are 1 or greater posts I want to run the HTML code, including the Wordpress PHP seen in between the {}. If there are 0 posts I want nothing to happen. Obviously I need another step in here somewhere because I am getting a parse error.

Any help would be great.

Thanks
Link to comment
https://forums.phpfreaks.com/topic/29560-if-statement-that-contains-html-and-php/
Share on other sites

Yes you can, but you did it a bit wrong:

[code]<?php
query_posts('cat=2&year=2006&monthnum=10');
if ($posts >= 1)
{
?>
<dl>
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<dt>October</dt>
<dd><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"></a></dd>
<?php endwhile; endif; ?>
</dl>
<?php
}
?>[/code]

I dont understand why you have "endwhile; endif;" but whatever...

Orio.
Thanks Orio it worked!

The "endwhile; endif;" has to do with the [url=http://codex.wordpress.org/The_Loop]Wordpress Loop[/url], more specifically:

[quote]If the syntax looks unfamiliar, that's because it's using the alternative syntax for PHP control structures. have_posts() and the_posts() are both wrapper functions for WP_Query functions[/quote]

One other question, it is outputting an empty <dl> in the source code when there are no posts for that month. How is that possible? Shouldn't the <dl> be skipped with everything else when the IF statement is false?
Thanks for helping me out Orio I really appreciate it.

To answer your question about $posts -> [url=http://codex.wordpress.org/Function_Reference/WP_Query]Function Reference-WP Query[/url]. So I am pretty sure it is checking the $posts but the IF statement is only affecting what is inside the Wordpress PHP Loop, instead of everything inside the {}.

For example with this code:

[code]<?php
query_posts('cat=2&year=2006&monthnum=10');
if ($posts >= 1)
{
?>
<dl>
        <dt>October</dt>
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<dd><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"></a></dd>
<?php endwhile; endif; ?>
</dl>
<?php
}
?>
[/code]

When there are 0 posts in October it outputs:

[code]<dl>
<dt>October</dt>
</dl>[/code]

and with the original code you fixed it just outputs just the <dl></dl> because the <dt>October</dt> is inside the Wordpress Loop.

Any idea whats happening?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.