PHP loop, ik kom er niet uit :(
Ik heb een script op mijn voorpagina lopen. Het zit als volgt in elkaar:
- Ik heb 2 hoofdcategorieen ("van mij" & "van anderen")
- Ik heb vijf subcategorieën
Ik wil dus op de voorpagina twee kolommen met ieder twee postings hebben voor beide hoofdcategorieën. Daarvoor heb ik twee loops:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!-- 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>
<?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>
En verder op de tweede kolom:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!-- 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; ?>
<?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; ?>
Nu komt het dikwijls voor dat de 1e kolom maar één ipv twee artikelen laat zien. Maar soms wanneer ik dan weer een nieuwe artikel toevoeg, laat ie wel weer twee artikelen zien :/ hoe kan het dat kolom 1 af en toe niet goed werkt en maar 1 artikel / posting laat zien ipv 2? Wat doe ik verkeerd???
Groet,
Olav
Gewijzigd op 24/01/2014 21:36:09 door Olav D
Er zijn nog geen reacties op dit bericht.