andymikester Posted December 5, 2006 Share Posted December 5, 2006 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 More sharing options...
Orio Posted December 5, 2006 Share Posted December 5, 2006 Yes you can, but you did it a bit wrong:[code]<?phpquery_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. Link to comment https://forums.phpfreaks.com/topic/29560-if-statement-that-contains-html-and-php/#findComment-135609 Share on other sites More sharing options...
andymikester Posted December 5, 2006 Author Share Posted December 5, 2006 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? Link to comment https://forums.phpfreaks.com/topic/29560-if-statement-that-contains-html-and-php/#findComment-135619 Share on other sites More sharing options...
Orio Posted December 5, 2006 Share Posted December 5, 2006 You are right. Are you sure that $posts is defined and it's value is smaller than 1??Orio. Link to comment https://forums.phpfreaks.com/topic/29560-if-statement-that-contains-html-and-php/#findComment-135628 Share on other sites More sharing options...
andymikester Posted December 5, 2006 Author Share Posted December 5, 2006 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]<?phpquery_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? Link to comment https://forums.phpfreaks.com/topic/29560-if-statement-that-contains-html-and-php/#findComment-135756 Share on other sites More sharing options...
andymikester Posted December 5, 2006 Author Share Posted December 5, 2006 I figured it out I needed to change:[code]if ($posts >= 1)[/code]to[code]if ($wp_query->post_count >= 1)[/code]You were right, $posts wasn't it.Thanks again though, I really appreciate you helping me out. Link to comment https://forums.phpfreaks.com/topic/29560-if-statement-that-contains-html-and-php/#findComment-135767 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.