PHP / Wordpress Question (Conditionals)

Status
Not open for further replies.

jerxs

New member
Jun 24, 2006
1,806
24
0
North East PA
I am stuck here, though that doesnt say much, I know squat about PHP for how long I have been trying to learn, but anyway I need some help with some conditional code for a WordPress theme, but first let me explain.

Ok so I have a WP site up that uses a different template file for one specific category and single "a reviews area to be exact. Now I have a list of the 5 latest posts from each category on the site which works pretty decent, though I would like to show different latest posts from the reviews section in the footer, so throughout the whole site all the links in the footer remain the same sitewide, though I would like to show something different in the reviews section, so this is what I have now that works in the footer;

Code:
<?php 
$lastposts = get_posts('numberposts=5&category=1');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Metal Detecting Talk</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=6');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Metal Detecting Articles</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=7');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Metal Detector Reviews</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=4');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>
<br style="clear:both;"  />

<ul class="latest-footer">
<li><strong>Metal Detecting Stories</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=5');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Metal Detecting Finds</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=3');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Detecting & Treasure News</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=8');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Detecting Research</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=9');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<?php endif; ?>
This works excelent and displays the 5 latest from each category, now I want to set something up so that if in category #x it will show this data..

Code:
<?php if ( in_category('4') ) : ?>

<div id="more-detector-reviews">     
<?php if ( in_category('14') ): ?>
  <li><strong>More Whites Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=14');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('10') ): ?>
  <li><strong>More Fisher Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=10');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('12') ): ?>
  <li><strong>More Bounty Hunter Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=12');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('13') ): ?>
  <li><strong>More Garrett Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=13');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('11') ): ?>
  <li><strong>More Minelab Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=11');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('15') ): ?>
  <li><strong>More Tesoro Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=15');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('16') ): ?>
  <li><strong>More Troy Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=16');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('10') ): ?>
  <li><strong>More Fisher Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=10');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>
 


So I try something like this and a few other variations, but nothing I pull out of my ass is working, so here it is all put together....

Code:
<ul class="latest-footer">
<li><strong>Metal Detector Reviews</strong></li>
[COLOR=Red]<?php if ( in_category('4') ) : ?>[/COLOR]

<div id="more-detector-reviews">     
<?php if ( in_category('14') ): ?>
  <li><strong>More Whites Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=14');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('10') ): ?>
  <li><strong>More Fisher Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=10');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('12') ): ?>
  <li><strong>More Bounty Hunter Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=12');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('13') ): ?>
  <li><strong>More Garrett Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=13');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('11') ): ?>
  <li><strong>More Minelab Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=11');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('15') ): ?>
  <li><strong>More Tesoro Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=15');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('16') ): ?>
  <li><strong>More Troy Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=16');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>

</div>

<div id="more-detector-reviews">     
<?php if ( in_category('10') ): ?>
  <li><strong>More Fisher Metal Detectors</strong></li>
<?php 
$lastposts = get_posts('category=10');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
<?php endif; ?>

</div>


[COLOR=Red]<?php else; ?>[/COLOR]


     
<?php 
$lastposts = get_posts('numberposts=5&category=1');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Metal Detecting Talk</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=6');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Metal Detecting Articles</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=7');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Metal Detector Reviews</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=4');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>
<br style="clear:both;"  />

<ul class="latest-footer">
<li><strong>Metal Detecting Stories</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=5');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Metal Detecting Finds</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=3');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Detecting & Treasure News</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=8');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

<ul class="latest-footer">
<li><strong>Detecting Research</strong></li>
<?php 
$lastposts = get_posts('numberposts=5&category=9');
foreach ($lastposts as $post) :
setup_postdata($posts);
?>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li>
<?php endforeach; ?>
</ul>

[COLOR=Red]<?php endif; ?>[/COLOR]
 
Sorry for the huge ammount of code, but I highlighted what I have thrown in there to display the last 5 from each category, but display the latest metal detector reviews from each review category "if in_category();".

Thanks for the help in advance, Jer
 
In all reality I could create another footer file, like "footer2.php" and just include it in the single file that I use for the reviews section, but I would rather do it dynamically without adding another file to the template, if it is in fact possible.

Jer
 
Good grief thats a lot of code. From glancing at your snippets, it seems you run the last 5 posts from the same category on every page. To make your page lighter code it in reverse. Meaning call action for the special page first.

Unfortunately from your code I don't know which category is the one you want special and i don't know if you just want a different category or different CSS this should allow you to customize both.

Code:
<ul class="latest-footer">
<?php $original_post = $post;
$cat = get_the_category();
$cat = $cat[0];
$posts = get_posts("category=" . $cat->cat_ID . "&numberposts=5");
if( $posts ) : $post = $original_post;
setup_postdata($post);
?>
<?php if ( !is_category('special category listed by number') ): ?>
<li><strong>More in <?php the_category(', ') ?></strong></li>
<li> <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?> </a></li> 
<?php else ;?>
<li><strong><?php the_category(', ') ?> is special to me!</strong></li>
<li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></li> 
<?php endforeach; ?>
<?php endif; ?>
</ul>

Unfortunately I don't have testing sites up where I can run this code. It's an old variation of something I picked up long ago. I hope it works for you at the very least gets you to look at it differently.
 
I just glanced through it but you might want to follow through with elseif in there.

Code:
<?php 
if ( in_category('4') ) {
           RUN THIS CODE FOR 4
    } elseif ( in_category('5') ) {
           RUN THIS CODE FOR 5
    } elseif ( in_category('6') ) {
          RUN THIS CODE FOR 6
    } 

?>
 
Ill show you what I am doing, its from what I have run through my simple mind the only way to really achieve this...

Project Metal Detecting, Metal Detecting, Metal Detecting Finds and Metal Detecting Tips, scroll down to the footer....... That is the site wide footer and each category has to be called out and displayed as a seperate list to style, now there is one area of the site, "Detector Reviews" that instead of displaying the last five from each category, Id like to display the last 5 detector reviews from each sub category under detector reviews......

Seeings that I suck at PHP I am going to try pulling it off using a meathod Ive tryed before, that may be generic and painful but works...

Ill make a .php that holds the content to each different footer, then display them using the code above.
Code:
<?php 
if ( is_category() ) {
 include ('/sitewide_footer.php');
    } elseif ( in_category('7') ) {
 include ('/reviews_footer.php');
      }

?>

That should work and also clean up my footer.php file a bit....

Jer
 
Your getting it. Hopefully can come back with a better code for you. But another really simple method i used to use until i would be able to learn the right code or find a plugin to do what I want is to make a category specific template. So if Detector Reviews is category 23 make a template category-23.php and put the code you want to do in there. Also if you want this to carry over to you single view of category 23 make a template called single-23.php. It's always my short-term quick fix. heck sometimes it's long term because it works so well Wish you the best
 
I use a different file for single.php in the reviews section, though it uses the same footer.php, anyway you just gave me an idea, I wont change the footer links at all (the latest 5 from each), but work latest reviews from each into single-cat#.php somehow......
 
Status
Not open for further replies.