• 0

[wordpress] Strange problem with "handmade" script.


Question

Hi,
 
I want to display recent four posts from selected category in simple grid. I wrote something like this:
<div id="cat-grid">
<?php    
$catArray = array (3,4,107,113,168,102,167,111,85,5,110,11,109,77,106,135,108,149,136,105,103,91,76,9,112,10,8,127,137,-129);
foreach ($catArray as $kategoria) {
// Aromaty-i-przyprawy
$the_query = new WP_Query( 'cat='.$kategoria.'&posts_per_page=4' );
 
// The Loop
if ( $the_query->have_posts() ) {
        echo '<div class="kategoria">
            <div class="cat-header">
<h2>'. get_the_category_by_ID($kategoria).'</h2><span><a href="' . get_category_link($kategoria) . '">zobacz</a></span>
</div>';
echo '<div class="recent-posts">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<a href="'. get_permalink() .'"><div class="recent-post">';
echo '<div class="kontent">';
echo ''. the_post_thumbnail() .'';
echo '<h3>' . get_the_title() . '</h3>';
//echo '<p>Opublikowano: ' . get_the_time('F jS, Y') . '<span>'. get_comments_number('0', '1', '%') .'</span></p>';
echo '</div>';
echo '</div></a>';
                
}
echo '</div></div>';
} else { 
//no posts found
}
 
}
?>
</div>
 
What is strange ? At the end I got one extra line recent posts from all categories.
Can anyone tell me how to fix it ? Thanks.

post-345878-0-85038700-1437337681.jpg

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Hey MaPa,

Just for future reference, make sure to use indentation; that way, we can see which block of code is running which section of the script. At the moment, I'm struggling to see which bit is doing what, so, I'll show you an example WP Query to do what you're desiring:

<?php

$args = (
    'posts_per_page' => '4',
    'cat' => '1,2,3,4,5'
    )

// The Query with arguments super-imposed
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
	echo '<div class="recent-posts">';
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		echo '<li>' . get_the_title() . '</li>';
	}
	echo '</div>';
} else {
	// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

That's not the full code of what you're wanting to do, i.e I haven't put in your custom CSS classes, however, you should be able just to copy those echo's over into that and display as needed.

If you do want me to write the full script, to how you want it, just reply back and I can do that for you (it's just a little late here right now).

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.