Jump to content

[SOLVED] PHP Multiples of 2, Show posts...not working (wordpress)


Jezthomp

Recommended Posts

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 :)

Link to comment
Share on other sites

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
      } ?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

:lol: 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 :)

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
} 
?>

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Thanks again, Alt. :)

 

Getting an a little error though...

 

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>'; 

Link to comment
Share on other sites

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
} 
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?  :wtf:

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.