rogerr
Member
Posted 7 months ago #
I think this has something to do with me trying to mix strings and arrays....Help please to combine the $option into the array....
<?php
$categories = get_categories('child_of=20');
foreach ($categories as $cat) {
$option = '';
$option .= ''.$cat->cat_ID.', ';
echo $option;
// This currently outputs: 21, 22, I need to insert this for the array below like this: $display_categories = array(21, 22,);
}
// enter the IDs of the categories to display
$display_categories = array(21, 22,); // I tried entering array($option); here and only get the last category
foreach ($display_categories as $category) { ?>
<?php query_posts("showposts=1&orderby=rand&cat=$category"); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="cat-3content">
<div align="center"><a href="<?php echo get_category_link($category);?>"><?php echo the_excerpt(); ?></a>
</div>
<h6><a href="<?php echo get_category_link($category);?>"><?php single_cat_title(); ?></a></h6>
</div>
<?php endwhile; ?>
<?php } ?>
You can just set the array in your first foreach loop:
<?php
$display_categories = false;
$categories = get_categories( 'child_of=20' );
foreach ( $categories as $cat )
$display_categories[] = $cat->cat_ID;
if( $display_categories ) {
foreach ( $display_categories as $category ) {
query_posts( "showposts=1&orderby=rand&cat=$category" );
while ( have_posts() ) {
the_post(); ?>
<div class="cat-3content">
<div align="center"><a href="<?php echo get_category_link($category);?>"><?php echo the_excerpt(); ?></a></div>
<h6><a href="<?php echo get_category_link($category);?>"><?php single_cat_title(); ?></a></h6>
</div>
<?php
}
}
}
?>
rogerr
Member
Posted 7 months ago #
Thanks for the quick reply...you rock....BUT....
Small issue - The excerpt is showing the same for each category...and the h6 is not showing at all
you can have a look here
http://clients.popcornphotography.com.au/category/your-photos
Thanks in advance
I am confused... please describe what you are trying to accomplish.. The link you posted goes to a client galleries page... I am confused..
rogerr
Member
Posted 7 months ago #
Oh dear...sorry about that
try here... this is using your new code (with thanks)
http://clients.popcornphotography.com.au/category/events
I am using photoq which publishes a thumbnail as the_excerpt for each post.
I want to show a random thumbnail from, and the category title of each child category.
I have used the old code here to show how I would like it to look
http://clients.popcornphotography.com.au/category/your-photos
Cheers and once again Many Many Thanks
Hmm... look at here:
query_posts( "showposts=1&orderby=rand&cat=$category" );
while ( have_posts() ) {
See the part that says:
showposts=1
This will force the loop to only return one result. Your page is displaying 2 results which is most likely due to the foreach loop the encases the "WordPress Loop".
Try removing showposts=1 and let me know what happens.
rogerr
Member
Posted 7 months ago #
Hmmm...that didn't work...I'll go back to changing it manually when I put a new client category up..
Many thanks for your help