Guest Posted June 15, 2012 Share Posted June 15, 2012 Hy guys. I would be very grateful if you can help me on this. This is what I have: function ansimuz_novicke_items($v){ // get config $nPosts = $v['ansimuz_front_tab_number']; $nPosts = ($nPosts == '') ? 4 : $nPosts; $args = array( 'post_type' => 'novicke', 'numberposts' => $nPosts, 'tax_query' => array( array( 'taxonomy' => 'tip_novicke', 'field' => 'slug', 'terms' => 'obvestilo-podpora-agentu', 'operator' => 'NOT IN' ) ) // end array ); $myposts = get_posts($args); global $post; foreach($myposts as $post) : setup_postdata($post); ?> <div class="one-fourth-novicke"> <h6 class="line-divider-novicke"><?php the_title() ?></h6> <span class="sub">Objavljeno <?php the_time('j. F Y'); ?></span> <p><?php the_content() ?></p> </div> <?php endforeach; ?> <?php } And I need to modify it so that it will stack posts in packs of 4 wrapped inside the <div></div> For an instant: <div> post1 post2 post3 post4 </div> <div> post5 post6 post7 post8 </div> ... and so on. Do you have any suggestion how to do that? Thank you in advance ! Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 15, 2012 Share Posted June 15, 2012 I see some problems in that code: It is generally not advised to use "global". Since you are defining $post in the foreach loop, it really makes no sense why you are setting it as global. The only thing I can assume is that the other functions that are called [the_title(), the_time() and the_content()] are using the current value of $post. But, that's a pretty poor implementation. You should instead pass the appropriate value of the $post array within those functions. E.g. the_title($post['some_index']) Also, why do you have a three-dimensional array, $args, where the 2nd level only contains only element - another array. Seems you shoudl only need a two dimensional array. As to your problem, I would use array_chuck to split the $myposts array into the sections matching the number you want. Then do a nested foreach loop. The outer loop is for each group of four and the inner loop is for the individual records. $post_data = array_chunk($myposts, 4); foreach($post_data as $myposts) { echo "<div class='one-fourth-novicke'>\n"; foreach($myposts as $post) { setup_postdata($post); echo " <h6 class='line-divider-novicke'>" . the_title() . "</h6>\n"; echo " <span class='sub'>Objavljeno " . the_time('j. F Y') . "</span>\n"; echo " <p>" . the_content() . "</p>\n"; } echo "</div>\n"; } Quote Link to comment Share on other sites More sharing options...
Guest Posted June 15, 2012 Share Posted June 15, 2012 Hy Psycho! Thank you for your time and constructive critics and a lot of help! To be honest I'm really bad with php... more of a VBA guy . Your code did most of the trick. I modified it a little in to: function ansimuz_novicke_items($v){ // get config $nPosts = $v['ansimuz_front_tab_number']; $nPosts = ($nPosts == '') ? 4 : $nPosts; $args = array( 'post_type' => 'novicke', 'numberposts' => $nPosts, 'tax_query' => array( array( 'taxonomy' => 'tip_novicke', 'field' => 'slug', 'terms' => 'obvestilo-podpora-agentu', 'operator' => 'NOT IN' ) ) // end array ); $myposts = get_posts($args); $post_data = array_chunk($myposts, 4); foreach($post_data as $myposts) { echo "<div>\n"; global $post; foreach($myposts as $post) { setup_postdata($post); echo "<div class='one-fourth-novicke'>\n"; echo "<h6 class='line-divider-novicke'>" . the_title() . "</h6>\n"; echo "<span class='sub'>Objavljeno " . the_time('j. F Y') . "</span>\n"; echo "<p>" . the_content() . "</p>\n"; echo "</div>\n"; } echo "</div>\n"; } } I tried without global $post; but then wordpress sends some strange resoults. I don't know why... possibly there are some more issues with this same global $post; sintax in some other part of my code and so I get wrong resoults. Strangely I get correct the_content() but for the the_title() and the_time() I get only time and title from the latest post available... But I will not take any more time of yours on this. Must first examine whole code for global $post; issues... What I would LIKE TO ASK YOU is this: When I run this code the content of function does not get where it should. It gets before the html tags. For an instance, this line: echo "<span class='sub'>Objavljeno " . the_time('j. F Y') . "</span>\n"; Produces this results: " 9. Junij 2012 " <span class="sub">Objavljeno </span> In this exact order. But it should be like: <span class="sub">Objavljeno 9. Junij 2012</span> Do you have any idea what could be wrong? Quote Link to comment Share on other sites More sharing options...
Guest Posted June 15, 2012 Share Posted June 15, 2012 OK, class dismissed ! Solution is to use , instead of . with functions. So correct code is: echo "<div class='one-fourth-novicke'>\n"; echo "<h6 class='line-divider-novicke'>" ,the_title(), "</h6>\n"; echo "<span class='sub'>Objavljeno " ,the_time('j. F Y'), "</span>\n"; echo "<p>" ,the_content(), "</p>\n"; echo "</div>\n"; Thank you Psycho for your help and guidance! Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 15, 2012 Share Posted June 15, 2012 Solution is to use , instead of . with functions. Ok, the problem is that those functions are echoing content to the page rather than returning a value. You should typically not build functions that output content and instead they should return the content. That a general programming standard - not a PHP thing. Using periods, as I did in an echo concatenates strings and would have worked if the functions were returning a value. The reasons the commas work is that the echo statement is executing on each value independently. But, it is not doing what you think it is doing - even if it is working For example, this line: echo "<span class='sub'>Objavljeno " ,the_time('j. F Y'), "</span>\n"; The echo is first outputting "<span class='sub'>Objavljeno " Then it executes the_time('j. F Y') and tries to echo the result. But, the function is actually echoing something and the result is a null string - so the echo is doing nothing on that. Lastly the echo is outputting "</span>\n" not to be too critical, but that is really sloppy, IMHO. But getting it working is the first battle. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.