Jezthomp Posted October 20, 2009 Share Posted October 20, 2009 I have been using this code to show div.example with 6 li columns inside it, each li is a post with its info inside. Once it gets to the 6th li it closes the div.example and starts a new one. This to enable me to divide them with a dotted line. However, when it gets to the 4th row it prints an empty div.example because there are no li's to go in it, i just need it to stop once it gets to 24, now its stopping at 24 but printing one final div.example... Really i need it to stop when there is no more li's to show, which it does however its printing this extra div.example and i dont know why. :'( You can see it here the big gap at the bottom is another div.example its showing with nothing inside it! (only part of the whole page code as its the only bit that is not working correctly and isn't connected to anything else.) <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; $postcnt = 0; query_posts('cat=3,4,5,6'); if (have_posts()) { ?> <div class="example"> <ul class="content_cols"> <?php while ( have_posts() ) : the_post(); $postcnt++; $data = get_post_meta( $post->ID, 'key', true );?> <li> column here </li> <?php if($postcnt % 2 == 0) { ?> <?php if($postcnt % 6 == 0) { ?> </ul> <div class="line"></div> <div class="clearFix"><!--x--></div> </div> <div class="example"> <?php } ?> <ul class="content_cols"> <?php } ?> <?php endwhile; ?> </ul> <div class="clearFix"><!--x--></div> </div> <?php } ?> The forum doesn't seem to be showing the first postcnt code properly, its the same as the one below just with 2 rather than 6 Any ideas? Obviously very annoying as dont want that final div.example there at all as nothing is in it!! Many thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/ Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 i think that the issue is that you create a new div.example after every six items in the list. As you have exactly 6 items in the last row, you are again creating a new div.example and closing it here </ul> <div class="clearFix"><!--x--></div> </div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940304 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 Yeah that is correct, is there anyway to tell it that if there is no more list items to publish it just closes and finishes its job. I couldn't figure it out i must have moved things around so many times with no luck I'm not the greatest with these things anyway, i've pieced it together from bits and bobs and got lucky really. Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940306 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 can you clarify for me what the ?php if($postcnt % 2 == 0) { ?> part does? Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940317 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 i'm not entirely sure, as i said i've pieced it together from tutorials and reading things, it probably works without it, i wasn't sure though. Basically, as each row has 6 columns of li's in it i thought it might be needed for a multiple of 2, i.e 6. Its all working well, until you get to the end and as you say it prints the extra empty div.example which is obviously not what i'm after as no posts are in it... If i added a post it would look fine, but need it to be dynamic to the available content and not hope its never an even 24 as you'll get a big space Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940321 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 fair enough, last question and i should have something workable for you. Do you have the total number of posts or a function that can return it? Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940328 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 No not really, i dont think. If there has to be a set amount of posts then i'll have to grin and bear it, i was hoping that it could work without needing it as the post count might be more or less than it is now... <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; $postcnt = 0; query_posts('cat=3,4,5,6'); if (have_posts()) { ?> <div class="example"> <ul class="content_cols"> <?php while ( have_posts() ) : the_post(); $postcnt++; $data = get_post_meta( $post->ID, 'key', true );?> <li> li col here </li> <?php if($postcnt % 2 == 0) { ?> <?php if($postcnt % 6 == 0) { ?> </ul> <div class="line"></div> <div class="clearFix"><!--x--></div> </div> <div class="example"> <?php } ?> <ul class="content_cols"> <?php } ?> <?php endwhile; ?> </ul> <div class="clearFix"><!--x--></div> </div> <?php } ?> The most important thing is the final div.example doesn't display the <div class="line"></div> hence the bottom of the rows, the line appears in all the above rows as its separator. Its why i didn't include it in the end part of the code the final div.example. Many thanks for you help Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940332 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 this should work if you have a way of counting how many posts there are: <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; $postcnt = 0; query_posts('cat=3,4,5,6'); if (have_posts()) { echo '<div class="example">'; echo '<ul class="content_cols">'; while ( have_posts() ) : the_post(); $postcnt++; $data = get_post_meta( $post->ID, 'key', true ); echo '<li>column here</li>'; //if the post was the 6th if($postcnt % 6 ==0) { //close the list, add the line,clear the float and close the div echo '</ul>'; echo '<div class="line"></div>'; echo '<div class="clearFix"><!--x--></div>'; echo '</div>'; //if the post was not the last if($postcnt != $lastPost) { //lastPost contains the total number of posts //create a new example div and list echo '<div class="example">'; echo '<ul class="content_cols">'; } } endwhile; ?> </ul> <div class="clearFix"><!--x--></div> </div> <?php } ?> do you have any code for query_posts('cat=3,4,5,6'); have_posts() the_post() im sure that there is a way we can get the number of post without having a set figure Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940340 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 Unfortunately no, it just needs to show all the posts from category 3,4,5 and 6. Thank you for your code but its still repeating the empty div.example at the bottom Also the li column has to have the following code in it, which wouldnt work either.. <li class="cat<?php $category = get_the_category(); echo $category[0]->cat_ID;?>"> <a href="<?php the_permalink() ?>" title="View more details of <?php the_title(); ?>"><img src="http://jezthompson.co.uk/ransom_note/wp-content/uploads/<?php echo $data[ 'event_image' ]; ?>" alt = "work image"/></a> <h3><?php the_title(); ?></h3> <p><?php echo $data[ 'event_date' ]; ?></p> <p><?php echo $data[ 'event_location' ]; ?></p> <p><?php echo $data[ 'event_time' ]; ?></p> <p><a href="<?php the_permalink() ?>" title="View more details of <?php the_title(); ?>">Read More</a></p> <div class="clearFix"><!--x--></div> </li> So my final one looks like this.. <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; $postcnt = 0; query_posts('cat=3,4,5,6'); if (have_posts()) { ?> <div class="example"> <ul class="content_cols"> <?php while ( have_posts() ) : the_post(); $postcnt++; $data = get_post_meta( $post->ID, 'key', true );?> <li class="cat<?php $category = get_the_category(); echo $category[0]->cat_ID;?>"> <a href="<?php the_permalink() ?>" title="View more details of <?php the_title(); ?>"><img src="http://jezthompson.co.uk/ransom_note/wp-content/uploads/<?php echo $data[ 'event_image' ]; ?>" alt = "work image"/></a> <h3><?php the_title(); ?></h3> <p><?php echo $data[ 'event_date' ]; ?></p> <p><?php echo $data[ 'event_location' ]; ?></p> <p><?php echo $data[ 'event_time' ]; ?></p> <p><a href="<?php the_permalink() ?>" title="View more details of <?php the_title(); ?>">Read More</a></p> <div class="clearFix"><!--x--></div> </li> <?php if($postcnt % 2 == 0) { ?> <?php if($postcnt % 6 == 0) { ?> </ul> <div class="line"></div> <div class="clearFix"><!--x--></div> </div> <div class="example"> <?php } ?> <ul class="content_cols"> <?php } ?> <?php endwhile; ?> </ul> <div class="clearFix"><!--x--></div> </div> <?php } ?> Sorry i didn't think it would have any baring on things... Still strange how your code shows the empty div.example also, perhaps it just wont work at all Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940344 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 no that is to be expected (my code showing the empty div) as i havent defined the $lastPost variable. if i set that then there will be no more empty div put this $lastPost=24; here $lastPost=24; $page = (get_query_var('paged')) ? get_query_var('paged') : 1; and there should be no more empty div that is why i asked if there was any way to count the posts - if there was we could assign it to the $lastPost variable and problem solved. Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940350 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 just been wading thru some wordpress docs try this and see what happens <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; $postcnt = 0; query_posts('cat=3,4,5,6'); //hopefully this will get the number of posts for this query $lastPost=$wp_query->post_count; if (have_posts()) { echo '<div class="example">'; echo '<ul class="content_cols">'; while ( have_posts() ) : the_post(); $postcnt++; $data = get_post_meta( $post->ID, 'key', true ); echo '<li>column here</li>'; //if the post was the 6th if($postcnt % 6 ==0) { //close the list, add the line,clear the float and close the div echo '</ul>'; echo '<div class="line"></div>'; echo '<div class="clearFix"><!--x--></div>'; echo '</div>'; //if the post was not the last if($postcnt != $lastPost) { //lastPost contains the total number of posts //create a new example div and list echo '<div class="example">'; echo '<ul class="content_cols">'; } } endwhile; ?> </ul> <div class="clearFix"><!--x--></div> </div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940360 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 That works apart from the line appears in the final div.example. I'll also need this in the li tags.. <li class="cat<?php $category = get_the_category(); echo $category[0]->cat_ID;?>"> <a href="<?php the_permalink() ?>" title="View more details of <?php the_title(); ?>"><img src="wp-content/uploads/<?php echo $data[ 'event_image' ]; ?>" alt = "work image"/></a> <h3><?php the_title(); ?></h3> <p><?php echo $data[ 'event_date' ]; ?></p> <p><?php echo $data[ 'event_location' ]; ?></p> <p><?php echo $data[ 'event_time' ]; ?></p> <p><a href="<?php the_permalink() ?>" title="View more details of <?php the_title(); ?>">Read More</a></p> <div class="clearFix"><!--x--></div> </li> Which the above doesn't like.. Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940363 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 yeah sorry, i really struggle with all the <?php ?> tags in the html - find it hard to read - so i reformatted it a bit. this should work with the <li> content <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; $postcnt = 0; query_posts('cat=3,4,5,6'); $lastPost=$wp_query->post_count; if (have_posts()) { echo '<div class="example">'; echo '<ul class="content_cols">'; while ( have_posts() ) : the_post(); $postcnt++; $data = get_post_meta( $post->ID, 'key', true ); $category = get_the_category(); echo'<li class="cat'.$category[0]->cat_ID.'">'; echo '<a href="'.the_permalink().'" title="View more details of '.the_title().'"><img src="wp-content/uploads/'; echo $data[ 'event_image' ].'" alt = "work image"/></a>'; echo '<h3>'.the_title().'</h3>'; echo '<p>'.$data[ 'event_date' ].'</p>'; echo '<p>'.$data[ 'event_location' ].'</p>'; echo '<p>.'$data[ 'event_time' ].'</p>'; echo '<p><a href="'.the_permalink().'" title="View more details of '.the_title().'">Read More</a></p>'; echo '<div class="clearFix"><!--x--></div>'; echo '</li>'; //if the post was the 6th if($postcnt % 6 ==0) { //close the list, add the line,clear the float and close the div echo '</ul>'; echo '<div class="line"></div>'; echo '<div class="clearFix"><!--x--></div>'; echo '</div>'; //if the post was not the last if($postcnt != $lastPost) { //lastPost contains the total number of posts //create a new example div and list echo '<div class="example">'; echo '<ul class="content_cols">'; } } endwhile; ?> </ul> <div class="clearFix"><!--x--></div> </div> <?php } ?> WAIT WAIT WAIT - sorry found a bug - try it now Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940366 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 Thanks again, Alt. Getting an a little error though... Quote Parse error: syntax error, unexpected '.' in.........etc etc Which is this line.. echo '<a href="'.the_permalink().'" title="View more details of '.the_title();.'"><img src="wp-content/uploads/'; echo $data[ 'event_image' ].'" alt = "work image"/></a>'; Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940368 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 yeah sorry my bad missed a semi-colon :-[ here you go - this should fix the line appearing at the bottom 2 <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; $postcnt = 0; query_posts('cat=3,4,5,6'); $lastPost=$wp_query->post_count; if (have_posts()) { echo '<div class="example">'; echo '<ul class="content_cols">'; while ( have_posts() ) : the_post(); $postcnt++; $data = get_post_meta( $post->ID, 'key', true ); $category = get_the_category(); echo'<li class="cat'.$category[0]->cat_ID.'">'; echo '<a href="'.the_permalink().'" title="View more details of '.the_title().'"><img src="wp-content/uploads/'; echo $data[ 'event_image' ].'" alt = "work image"/></a>'; echo '<h3>'.the_title().'</h3>'; echo '<p>'.$data[ 'event_date' ].'</p>'; echo '<p>'.$data[ 'event_location' ].'</p>'; echo '<p>.'$data[ 'event_time' ].'</p>'; echo '<p><a href="'.the_permalink().'" title="View more details of '.the_title().'">Read More</a></p>'; echo '<div class="clearFix"><!--x--></div>'; echo '</li>'; //if the post was the 6th if($postcnt % 6 ==0) { //close the list, add the line,clear the float and close the div echo '</ul>'; if($postcnt != $lastPost) { //lastPost contains the total number of posts //only display the line div if this is not the last line of posts echo '<div class="line"></div>'; } echo '<div class="clearFix"><!--x--></div>'; echo '</div>'; //if the post was not the last if($postcnt != $lastPost) { //lastPost contains the total number of posts //create a new example div and list echo '<div class="example">'; echo '<ul class="content_cols">'; } } endwhile; ?> </ul> <div class="clearFix"><!--x--></div> </div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940370 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 Thanks again Feel its so close another rubbish error though.. Quote Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in etc etc etc etc on line 161 Which is this like i think.. echo '<p>.'$data[ 'event_time' ].'</p>'; Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940371 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 my sincere apologies, thats what you get for not checking your code properly!!! change that line to this echo '<p>'.$data[ 'event_time' ].'</p>'; Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940375 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 ok so it looks like this line of code has no bearing at all $lastPost=$wp_query->post_count; not that im suprised - i know nothing about wordpress for now i guess i would change that line to $lastPost=24; which is the number of posts that you have now but i would recommend maybe asking in a wordpress forum (or here) about how to get the number of posts returned by a query Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940376 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 Thanks Alt that is working great, its just the content inside the li's which is going abit wonky. The permalink's are working as they should, the image and the read more work as links on their own, however above each one it is printing the full link in text as well, literally printing the http:// bit... Also in a similar way the h3 text is appearing but above out of its h3 holder for some reason.. Any idea? Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940378 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 do you have a url so i can have a look? Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940382 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 Yes of course sorry... Your code page.. How it currently is, which is fine apart from the big white empty div.example at the bottom as discussed. Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940386 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 no thats ok. i have a feeling the problem is my wordpress ignorance. it would seem that the permalink and image function calls cannot be put in an echo statement like i have done. so maybe try this in place of the existing <li> code echo'<li class="cat'.$category[0]->cat_ID.'">'; echo '<a href="'; the_permalink(); echo '" title="View more details of '; the_title(); echo '"><img src="wp-content/uploads/'; echo $data[ 'event_image' ].'" alt = "work image"/></a>'; echo '<h3>'; the_title(); echo '</h3>'; echo '<p>'.$data[ 'event_date' ].'</p>'; echo '<p>'.$data[ 'event_location' ].'</p>'; echo '<p>'.$data[ 'event_time' ].'</p>'; echo '<p><a href="'; the_permalink(); echo '" title="View more details of '; the_title(); echo '">Read More</a></p>'; echo '<div class="clearFix"><!--x--></div>'; echo '</li>'; Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940391 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 Perfect!!! Brilliant, thank you so much You're a legend! Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940393 Share on other sites More sharing options...
Alt_F4 Posted October 20, 2009 Share Posted October 20, 2009 i wouldn't go that far! no problems. can i ask one question are you using this bit of code $lastPost=$wp_query->post_count; and also (i dont want to sound condescending here) the last thing i want to do is come in and stomp on your work and leave you thinking HUH?!? so does what i have done make sense to you? Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940395 Share on other sites More sharing options...
Jezthomp Posted October 20, 2009 Author Share Posted October 20, 2009 Yeah i'm using that bit of code. From my experience with WP that should take any posts from the selected cats and print them however many, not limiting it to any fixed amount, which is what i am after. What you have done certainly makes sense, whether i could do it again on my own would be another matter, i'll go through it step by step, the comments certainly help also. Many thanks again Quote Link to comment https://forums.phpfreaks.com/topic/178323-solved-php-multiples-of-2-show-postsnot-working-wordpress/#findComment-940421 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.