PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
gward1
Forum Newbie
Posts: 9 Joined: Thu Feb 11, 2010 1:41 am
Post
by gward1 » Thu Feb 11, 2010 1:45 am
Hello,
I need to place some code in a if else statement but the code I need to place in there has embedded <?php statements. Here's the if else statement:
Code: Select all
<?php
if ( is_category() ) {
$cat = get_query_var('cat');
$child_cats = get_categories('child_of='.$cat);
if ($child_cats) {
$args = array(
'title_li' => '',
'echo' => 1,
'child_of' => $cat
);
wp_list_categories($args);
} else {
//do whatever if no child cats like do a post loop
}
}
?>
Here's what I need to place in the else section:
Code: Select all
<div class="middle">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p><small><?php the_time('F jS, Y') ?> <?php the_author() ?></small> Posted in <?php the_category(', ') ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', '', ' | '); ?> <?php if(function_exists('the_views')) { the_views(); } ?></p>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<br />
<!-- AddThis Bookmark Post Button BEGIN -->
<?php echo "<div class=\"addthis\"><a href=\"http://www.addthis.com/bookmark.php?pub=blogohblog&url=".get_permalink()."&title=".get_the_title($id)."\" title=\"Bookmark using any bookmark manager!\" target=\"_blank\"><img src=\"http://s9.addthis.com/button1-bm.gif\" width=\"125\" height=\"16\" border=\"0\" alt=\"AddThis Social Bookmark Button\" /></a></div>"; ?>
<!-- AddThis Bookmark Post Button END -->
Any help would be appreciated. Thanks.
learnerabn
Forum Commoner
Posts: 48 Joined: Wed Feb 10, 2010 12:56 am
Post
by learnerabn » Thu Feb 11, 2010 2:05 am
could u explain me clearly what u need
do u need place the html tags to place in the else part?
learnerabn
Forum Commoner
Posts: 48 Joined: Wed Feb 10, 2010 12:56 am
Post
by learnerabn » Thu Feb 11, 2010 2:06 am
if u want to place the html tags in the else part means just paste those codes in a echo".." inside the else part.
gward1
Forum Newbie
Posts: 9 Joined: Thu Feb 11, 2010 1:41 am
Post
by gward1 » Thu Feb 11, 2010 5:03 am
That's exactly what I tried and I'm still getting syntax errors. Here's the code in the else statement with the <?php statements removed...
Code: Select all
<?php
if ( is_category() ) {
$cat = get_query_var('cat');
$child_cats = get_categories('child_of='.$cat);
if ($child_cats) {
$args = array(
'title_li' => '',
'echo' => 1,
'child_of' => $cat
);
wp_list_categories($args);
} else {
echo"<div class=\"middle\">";
if (have_posts()) :
while (have_posts()) : the_post();
echo"<div id=\"post-";
the_ID();
echo"\">";
echo"<h2><a href=\"";
the_permalink();
echo"\" rel=\"bookmark\" title=\"Permanent Link to";
the_title();
echo"\">";
the_title();
echo"</a></h2><p><small>";
the_time('F jS, Y');
the_author();
echo"</small> Posted in";
the_category(', ');
comments_popup_link('No Comments »', '1 Comment »', '% Comments »');
edit_post_link('Edit', '', ' | ');
if(function_exists('the_views')) { the_views(); }
echo"</p><div class=\"entry\">";
the_content('Read the rest of this entry »');
echo"</div><br />";
echo "<div class=\"addthis\"><a href=\"http://www.addthis.com/bookmark.php?pub=blogohblog&url=".get_permalink()."&title=".get_the_title($id)."\" title=\"Bookmark using any bookmark manager!\" target=\"_blank\"><img src=\"http://s9.addthis.com/button1-bm.gif\" width=\"125\" height=\"16\" border=\"0\" alt=\"AddThis Social Bookmark Button\" /></a></div>";
}
}
?>
Now I'm getting "Parse error: parse error, unexpected '}' in category.php on line 45" which is actually line 43 on here. I've counted the brackets and the opening and closing ones add up.
learnerabn
Forum Commoner
Posts: 48 Joined: Wed Feb 10, 2010 12:56 am
Post
by learnerabn » Thu Feb 11, 2010 8:15 am
i think you missed endif and endwhile keywords.
check it out.
limitdesigns
Forum Commoner
Posts: 25 Joined: Sat Feb 06, 2010 9:05 pm
Post
by limitdesigns » Thu Feb 11, 2010 2:39 pm
Just close the php using ?> after the open curly bracket for your else statement, and re-start php using <?php right before the closed curly bracket. You don't have to maintain the PHP in loops like this; you can close the PHP tags and place regular HTML and some simple php tags embedded, like you have.
gward1
Forum Newbie
Posts: 9 Joined: Thu Feb 11, 2010 1:41 am
Post
by gward1 » Thu Feb 11, 2010 3:38 pm
Thanks for the help, I did what you said with the <?php and it did make it easier, also with the closing brackets I had to put them after the endif and endwhile which was further down the page. Here's the working code:
Code: Select all
<div class="middle">
<?php
if ( is_category() ) {
$cat = get_query_var('cat');
$child_cats = get_categories('child_of='.$cat);
if ($child_cats) {
$args = array(
'title_li' => '',
'echo' => 1,
'child_of' => $cat
);
wp_list_categories($args);
} else { ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p><small><?php the_time('F jS, Y') ?> <?php the_author() ?></small> Posted in <?php the_category(', ') ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', '', ' | '); ?> <?php if(function_exists('the_views')) { the_views(); } ?></p>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<br />
<!-- AddThis Bookmark Post Button BEGIN -->
<?php echo "<div class=\"addthis\"><a href=\"http://www.addthis.com/bookmark.php?pub=blogohblog&url=".get_permalink()."&title=".get_the_title($id)."\" title=\"Bookmark using any bookmark manager!\" target=\"_blank\"><img src=\"http://s9.addthis.com/button1-bm.gif\" width=\"125\" height=\"16\" border=\"0\" alt=\"AddThis Social Bookmark Button\" /></a></div>"; ?>
<!-- AddThis Bookmark Post Button END -->
</div>
<div class="br"><br /></div>
<?php endwhile; ?>
<div>
<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
</div>
<br />
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
<?php
}
}
?>
</div>