Jump to content

Need help with double foreach loop :(


olavdi

Recommended Posts

Dear all,

 

I have a script on my website's front page. Quickly summarized:

 

- I have two main categories ("Made by me" and "Made by others")

- I have five subcategories

 

I want to have on my frontpage two columns (one for main category #1, two for main category #2) containing each two postings. I use two loops:

<!-- Start the Loop. -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php static $count = 0;
if ($count == "2") { break; }
else { ?>
<?php if ( in_category('5') && !is_single() ) continue; ?>

 <!--Start Post-->

<div align="justify" style='float:left; width: 276px; margin: 0 0 0 20px;'>
<h6><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><div style="color: #000000;"><?php the_title(); ?></div></a></h6>
<div style="font-size: 80%; color: #999999; font-style: italic; font-weight: bold;">In: <?php
//exclude these from displaying
$exclude = array("4");

//set up an empty categorystring
$catagorystring = '';

//loop through the categories for this post
foreach((get_the_category()) as $category)
{
    //if not in the exclude array
    if (!in_array($category->cat_ID, $exclude))
    {
        //add category with link to categorystring
        $catagorystring .= ''.$category->name.', ';
    }
}

//strip off last comma (and space) and display
echo substr($catagorystring, 0, strrpos($catagorystring, ','));
?></div>

<?php
$my_excerpt = get_the_excerpt();
echo "<div style=\"color: #000000;\">";
echo $my_excerpt;
echo "</div>";
?>
        <?php wp_link_pages(array('before' => '' . __('Pages:', 'cloriato'), 'after' => '')); ?>          
<div class="frontpage_olav">               
<a class="read_more" href="<?php the_permalink(); ?>"></a></div>
</div>
 <!--End Post-->

<?php $count++; } ?>
<?php endwhile; ?>
 <!--End Loop-->
<?php endif; ?>

</div>

And for the second column:

<!-- Start the Loop. -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php static $count2 = 0;
if ($count2 == "2") { break; }
else { ?>
<?php if ( in_category('4') && !is_single() ) continue; ?>

 <!--Start Post-->

<div align="justify" style='float:left; width: 276px; margin: 0 0 0 17px;'>
<h6><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><div style="color: #000000;"><?php the_title(); ?></div></a></h6>
<div style="font-size: 80%; color: #999999; font-style: italic; font-weight: bold;">In: <?php
//exclude these from displaying
$exclude2 = array("5");

//set up an empty categorystring
$catagorystring2 = '';

//loop through the categories for this post
foreach((get_the_category()) as $category2)
{
    //if not in the exclude array
    if (!in_array($category2->cat_ID, $exclude2))
    {
        //add category with link to categorystring
        $catagorystring2 .= ''.$category2->name.', ';
    }
}

//strip off last comma (and space) and display
echo substr($catagorystring2, 0, strrpos($catagorystring2, ','));
?></div>

<?php
$my_excerpt2 = get_the_excerpt();
echo "<div style=\"color: #000000;\">";
echo $my_excerpt2;
echo "</div>";
?>
        <?php wp_link_pages(array('before' => '' . __('Pages:', 'cloriato'), 'after' => '')); ?>          
<div class="frontpage_olav">               
<a class="read_more" href="<?php the_permalink(); ?>"></a></div>
</div>
 <!--End Post-->

<?php $count2++; } ?>
<?php endwhile; ?>
 <!--End Loop-->
<?php endif; ?>

The whole problem is now that often the first column only shows one posting instead of two. Sometimes, however, it does show two postings. What am I doing wrong? Why doesn't it show two postings as it's supposed to do? How come my first column doesn't function properly?

 

Thanks a lot for all your help!

 

Olav

Link to comment
https://forums.phpfreaks.com/topic/285676-need-help-with-double-foreach-loop/
Share on other sites

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.