Find oldest post — WordPress - php

Thanks to some answers on here, I've managed to distinguish my posts into the latest post and all the rest. However, it needs to be the oldest post. Here is my current loop:
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<!-- first post -->
<?php $c++;
if( $c == 1) :?>
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="down">a</div>
</div>
</div>
<?php else :?>
<!-- second post -->
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="up">b</div>
</div>
</div>
<?php endif; ?>`
I saw somewhere that you can use a while loop to target the last post from a post.length. However, I am unsure how to implement this.

Yes, you're right. Use count.
Suppose the total posts is 5 for $total_posts = count($posts);.
You'll have to check your $counter for total - 1 as the array is $posts[0], $posts[1], $posts[2], $posts[3], $posts[4].
<?php
if ( have_posts() ) :
$counter = 0;
$total_posts = count($posts) - 1;
while ( have_posts() ) :
the_post();
if( $counter == $total_posts ) {
?>
<div class="container"><?php // CUSTOM CODE FOR LAST ARTICLE ?></div>
<?php
} else {
?>
<div class="container"><?php // CUSTOM CODE FOR THE REST OF ARTICLES ?></div>
<?php
}
$counter++;
endwhile;
endif;
Note that you only need to open <?php and close ?> when changing from PHP to HTML, using them at every line is very confusing.

Related

How to display wordpress first post different from the others?

First part of index.php, which currently have big post display contains these entries:
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="p-heading"><h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>
and the first part of archive.php contains these entries:
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content-a">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="imgdiv"><img src="<?php echo catch_that_image() ?>" width="250"></div>
<div class="p-heading"><h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_excerpt(); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <img src="http://www.virmodrosti.com/wp-content/uploads/comments.png" alt="Komentarji" width="26" height="12"><?php $totalcomments = get_comments_number(); echo $totalcomments; ?></div>
</div>
<?php endwhile; ?>
I use diffrent style div id="content" for big post, div id="content-a" for smaller post display, 3 in a row.
Now, I would like that only the latest post would be display in big format, as defined by #content in css, and the rest just as they are on archive.php with #content-a. How can I do that?
My site main index page is http://www.virmodrosti.com/ and archive is here http://www.virmodrosti.com/zdravje/
Kindly let me know, thank you.
The absolute easiest way to do this would be to use a ::first-child pseudo-selector in CSS.
If that is not an option, you can add a counter to your while loop and use that to add a class to your items.
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php
$counter = 0;
if (have_posts()) :
?>
<?php while (have_posts()) : the_post(); ?>
<div class="post count-<?php echo $counter; ?>">
<div class="p-heading"><h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php
$counter++;
endwhile; ?>
There are two problems with your css selectors.
1 - You can't use div id='content-a' to style multiple posts because id is unique. You must use class=content-a.
2 - There's no id='content' inside your posts loop if (have_posts()) :
while (have_posts()) : the_post();. The only id=content is outside the loop. It will be applied to all posts no matter what.
The fix is to use a class that is inside the loop. In your code the best one would be post that is the higher div's class.
Then you need to use the while (have_posts()) loop to flag the first post...
index.php
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php
$first_post = true;
while (have_posts()) : the_post(); ?>
<div class="<?php
if ($first_post){
$first_post = false;
echo 'post-first';
}else{
echo 'post';
}
?>">
<div class="p-heading"><h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>
In archive.php I don't see why you are creating a new WP_Query object after if (have_posts()). But since it's not part of the question nor the problem I'll leave it that way...
archive.php
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php
$query = new WP_Query(array('posts_per_page'=> 2,));
$first_post = true;
while ($query->have_posts()) : $query->the_post(); ?>
<div class="<?php
if ($first_post){
$first_post = false;
echo 'post-first';
}else{
echo 'post';
}
?>"> <div class="p-heading"><h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>

using an if statement inside a while with wordpress

Hi guys I'm using a while loop to display my posts and while I have posts I want to display the odd and even post numbers differently. I currently have the following code:
<div class="container">
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
</div>
<?php query_posts('post_type=portfolio&post_status=publish&posts_per_page=8&paged='. get_query_var('paged'));
if( have_posts() ):
echo '<div>';
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
$i = 0;
if($i % 2 == 0)
{?>
<div class="class1">
html content 1 to go here
</div>
<?php
$i++;
}else{
?>
<div class="class2">
html content 2 to go here
</div>
<?php $i++; } ?>
<?php endif; ?>
<?php endwhile; ?>
<div class="container">
<div class="navigation">
<span class="newer"><?php previous_posts_link(__('« See Previous Case Studies','example')) ?></span>
<span class="older"><?php next_posts_link(__('See Next Case Studies »','example')) ?></span>
</div>
</div> <!-- /.navigation -->
<?php else: ?>
<div id="post-404" class="noposts">
<p><?php _e('None found.','example'); ?></p>
</div><!-- /#post-404 -->
<?php endif; wp_reset_query(); ?>
I haven't used php for a while, so I'm unsure the best way to do the above. I'm currently getting a few errors relating to syntax such as unexpected end of file or unexpected } on line...
Can anyone figure out where I am going wrong?
Reverse Position
<?php endwhile; ?>
<?php endif; ?>
First While statement should get complete than if.

PHP - Adding divs to a foreach loop every 4 times, working with $post_object

I am building a wordpress page (based on quark, using ACF) showing info from other posts using foreach(get_field('exhibitions') as $post_object)
I want the 'col grid_3_of_12-divs' containing info from other posts to group up in divs, with four in each div. A similar problem as the author of this question had: Is there an easy way to do 4 at a time in a php foreach loop
I have tried with array_chunk, but can't seem to make it without messing with the retrieving of info from the other posts.
Can someone help me with this?
I am a self taught php beginner, so please excuse me if my code is stupid..
<?php get_header(); ?>
<div id="primary" class="site-content row clearfix" role="main">
<div class="col grid_12_of_12">
<?php while ( have_posts() ) : the_post(); ?>
<?php foreach(get_field('exhibitions') as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div class="col grid_3_of_12">
<h3 class="exhibition title"> <?php echo $post_object->short_title?> </h3>
<?php $attachment_id = $post_object->thumbnail;
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); ?>
<img src="<?php echo $image_attributes[0]; ?>">
<p class="exhibition short desc"> <?php echo $post_object->short_description?> </p>
</div>
</a>
<?php endforeach; ?>
<?php content_nav( 'nav-below' ); ?>
<?php endwhile; ?>
</div>
</div>
Add a counter. Then when the counter is at the right spot, add a close or open div.
<?php get_header(); ?>
<div id="primary" class="site-content row clearfix" role="main">
<div class="col grid_12_of_12">
<?php while ( have_posts() ) : the_post(); ?>
<?php $counter = 0; /* ADD THIS */ ?>
<?php foreach(get_field('exhibitions') as $post_object): ?>
<?php if ($counter % 4 == 0): /* ADD THIS */ ?>
<div class="group-of-4-posts-wrapper">
<?php endif; ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div class="col grid_3_of_12">
<h3 class="exhibition title"> <?php echo $post_object->short_title?> </h3>
<?php $attachment_id = $post_object->thumbnail;
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); ?>
<img src="<?php echo $image_attributes[0]; ?>">
<p class="exhibition short desc"> <?php echo $post_object->short_description?> </p>
</div>
</a>
<?php if ($counter % 4 == 3): /* ADD THIS */ ?>
</div>
<?php endif; ?>
<?php $counter++ ?>
<?php endforeach; ?>
<?php
// this closes the div if there is not a number of posts that is evenly
// divisable by 4, like 11 posts. with 11 posts, the last post would have
// ($counter % 4 == 3) equal to false, because $counter % 4 would = 2
// adding this, closes the div, if it was not already closed
if ($counter % 4 != 0): /* ADD THIS. */
?>
</div>
<?php endif; ?>
<?php content_nav( 'nav-below' ); ?>
<?php endwhile; ?>
</div>
</div>

Sticky Post appears twice in homepage loop. Is there a way to make it appear only once?

So in WordPress, if you run the regular loop, Sticky Posts appear twice. Once at the beginning (as desired) and then once in the chronological order among regular posts. Is there a way to make a post sticky and not appear the second time?
Here's the code from the index.php file.
<?php get_header(); ?>
<div id="content">
<div class="sidebar">
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('Default') ) : ?>
<?php endif; ?>
</div>
<div class="textContainer">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<h1><?php the_title(); ?></h1>
<div class="featuredImage">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'main-thumb' ); } ?>
</div>
<div class="readMore">
<?php // the content of the post
the_content('Read More'); ?>
</div>
<div class="comments">
<?php _e('', 'surplus'); ?> <?php comments_popup_link('{0 Comments}', '{1 Comment}', '{% Comments}'); ?>
</div>
</div>
<?php endwhile; endif; ?>
<div class="nav3">
<?php posts_nav_link(); ?>
</div>
<?php get_footer(); ?>
Thank you!
Liz

How to combine is_category() and query_posts() in an if..else statement?

I have a few snippets of code where I want to check if the category is correct and if post actually exist, at the moment I have an if_category() function that then queries my required category but I think these are clashing so none of my posts for the category are being displayed, can anyone advise how I can fix this code?
PHP
<?php if(is_category('Rainwater Harvesting Product')) { ?>
<!-- Related Projects -->
<?php query_posts('category_name=rainwater-harvesting-project&showposts=5'); ?>
<?php if (have_posts()) { ?>
<div class="aside-mod related-projects">
<div class="curved-ctn">
<h2 class="module-header">Related Projects</h2>
<ul class="project-list clearfix">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="project">
<a href="<?php the_permalink();?>" class="project">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
} ?>
<h3><?php the_title(); ?></h3>
<!-- <i class="icon-project"></i> -->
</a>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>
</div>
<?php } ?>
<?php } ?>
<?php wp_reset_query(); ?>
It does not work because in your
<?php query_posts('category_name=Greywater Recycling Project&showposts=5'); ?>
you are giving to category_name, the Name instead of the category slug, the query_posts parameter, category_name takes only the slug. eg: it must be
<?php query_posts('category_name=greywater-recycling-project&showposts=5'); ?>
More info here: http://codex.wordpress.org/Function_Reference/query_posts

Categories