Wordpress Posts Page - loop is not showing latest post - php

I am relatively new to php.
I have a loop in place for my Wordpress posts page - The posts have to alternate between left and right alignments.
I have this working by assigning an even or odd class to each post, however now the latest post does not display on the posts page.
For example, if I have say 5 posts; 4 of the posts will display and the latest post will remain hidden until I make a new post - the previously hidden post will then join the others and the new "latest post" will remain hidden.
I can't figure out why my loop is skipping over the first post, I have already tried adding rewind_posts(); however this created an infinite loop of the same post.
Any help is much appreciated!
<?php
$postcount=1;
while(have_posts()) :
if( ($postcount % 2) == 0 ) $post_class = ' even';
else $post_class = ' odd';
?>
<div class="row">
<div id="stories-box-alt" class="stories-column-circle-main"
style="background-color:transparent;">
<div id="circle-shape" class="post <?php echo $post_class; ?>">
<?php the_post(); ?>
<img src="<?php the_field('post_preview_image'); ?>" class="curve">
<h2><?php the_title(); ?></h2>
<h3><span class="featured-title"><?php the_field('post_category'); ?> .
</span></h3>
<p><?php the_field('post_preview'); ?><br><a href="<?php the_permalink();
?>">read more...</a></p>
</div>
</div>
</div>
<?php $postcount++;
endwhile; ?>

<?php
$postcount=1;
while(have_posts()) :
?>
<div class="row">
<div id="stories-box-alt" class="stories-column-circle-main"
style="background-color:transparent;">
<div id="circle-shape" class="post <?php if(($postcount % 2) == 0){ ?> even <?php } else{ echo " odd"; }?>">
<?php the_post(); ?>
<img src="<?php the_field('post_preview_image'); ?>" class="curve">
<h2><?php the_title(); ?></h2>
<h3><span class="featured-title"><?php the_field('post_category'); ?> .
</span></h3>
<p><?php the_field('post_preview'); ?><br><a href="<?php the_permalink();
?>">read more...</a></p>
</div>
</div>
</div>
<?php $postcount++;
endwhile; ?>
OR
<?php echo $postcount % 2 == 0 ? ' even ': ' odd '; ?>

Please try to use the_post() first.
<?php
$postcount=1;
while(have_posts()) :
the_post();
if( ($postcount % 2) == 0 ) $post_class = ' even';
else $post_class = ' odd';
?>
<div class="row">
<div id="stories-box-alt" class="stories-column-circle-main"
style="background-color:transparent;">
<div id="circle-shape" class="post <?php echo $post_class; ?>">
<img src="<?php the_field('post_preview_image'); ?>" class="curve">
<h2><?php the_title(); ?></h2>
<h3><span class="featured-title"><?php the_field('post_category'); ?> .
</span></h3>
<p><?php the_field('post_preview'); ?><br><a href="<?php the_permalink();
?>">read more...</a></p>
</div>
</div>
</div>
<?php $postcount++;
endwhile; ?>

Basically there is a basic loop in wordpress to make what you want to do : https://wpchannel.com/wordpress/tutoriels-wordpress/afficher-articles-recents-site-wordpress/
You can modify this one with your own properties but this is usually loop used.

Related

Wrap every 2 posts on a row

I'm using Bootstrap, I have a list of posts and I want to wrap every 2 posts on a row. Each post is wrapped on a <div col>. You can see live here.
I tried with this but it wrap the row each one post:
<?php
$num = 0;
// Check if there are any posts to display
if ( have_posts() ) : ?>
<?php
// The Loop
while ( have_posts() ) : the_post();
if($num%2) {
echo "<div class='row' style='margin-bottom: 2.3em;''>";
}
?>
<div class="col-xs-12 col-sm-6 col-md-6">
<h2 class="category-encabezado">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Enlace a <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
<small>Hace
<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ''; ?>
</small>
<div class="entry">
<p>
<?php the_excerpt(); ?>
</p>
<?php
$my_shortcode = get_field('audio-field');
echo do_shortcode( $my_shortcode );
?>
</div>
</div>
<?php
if($num %2) {
echo '</div>';
}
$num++
?>
<?php endwhile; // End Loop
?>
</div>
<?php
You have to put div.row Out of the Loop while

ACF loop within category loop - Wordpress

I cannot get my posts to display on a category page.
It finds if the posts are within and category and displays content fine.. But the loop I am using does not bring in the posts.. Any help would be great.
<?php if (is_category(array(4, 5, 6))) { ?>
<?php if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="case-study-box">
<a href="<?php the_permalink(); ?>">
<div class="post-img ripple-me" style="background-image: url(<?php the_field('thumbnail_image'); ?>)">
</div>
</a>
<div class="post-intro">
<span><?php the_category(' '); ?></span>
<?php the_title('<h4>', '</h4>'); ?>
Read more
</div>
</div>
<!--Close .case-study-box -->
<?php endwhile; else: ?>
<?php endif; ?>
<?php } else { ?>

wordpress wrap every 3 posts in a div after the very first post in a loop

I want to add every 3 posts in my index.php file in a row div but I would like to exclude the first post so that I can style the first post differently.
I currently have the loop working for every 3 posts but I would like to exclude the first post and style the first post differently to make it a featured post
below is my code
<div class="content post-main__content clearfix" id="post-<?php the_ID(); ?>" >
<?php
if(have_posts()) : for($count=0;have_posts();$count++) : the_post();
$open = !($count%3) ? '<div class="post-main__row clearfix">' : ''; //Create open wrapper if count is divisible by 3
$close = !($count%3) && $count ? '</div>' : ''; //Close the previous wrapper if count is divisible by 3 and greater than 0
echo $close.$open;
?>
<div class="post-main__cell">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Click to go to <?php the_title(); ?>">
<div class="post-main__cell-top">
<?php the_post_thumbnail(array(330, 167,)); ?>
<h3 class="content__category-name <?php foreach((get_the_category()) as $category) { echo $category->cat_name. ''; } ?>">
<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>
</h3>
</div>
<div class="post-main__cell-bottom">
<h2 class="archive-header"><?php the_title() ?></h2>
<p class="paragraph--time"><?php posted_on(); ?></p>
</div>
</a>
</div>
<?php endfor; else : ?>
<?php echo $count ? '</div>' : ''; //Close the last wrapper if post count is greater than 0 ?>
There is a great answer here for creating a loop that separates out the first post from the rest: https://wordpress.stackexchange.com/questions/101096/how-to-show-one-post-different-from-the-rest
Then I'd use a grid framework or some simple css to style the remaining containers, personally I love getskeleton.com but this part is up to you using the blog loop template.
I worked it like this
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 12, 'paged' => $paged );
query_posts($args); ?>
<?php if (have_posts()) : ?>
<?php $postcount = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $postcount++; ?>
<?php if ($postcount == 1 && $paged == 1) : // if this is the first post & first page ?>
<div class="post-main__row clearfix">
<div class="post-main__cell post-main__cell-large">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Click to go to <?php the_title(); ?>">
<div class="post-main__cell-left clearfix">
<?php the_post_thumbnail(array(691, 317,)); ?>
<h3 class="content__category-name <?php foreach((get_the_category()) as $category) { echo $category->cat_name. ''; } ?>">
<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>
</h3>
</div>
<div class="post-main__cell-right">
<h2 class="archive-header"><?php the_title() ?></h2>
<p class="paragraph--time"><?php the_time('l, F jS, Y') ?></p>
</div>
</a>
</div>
</div>
<?php
if(have_posts()) : for($count=0;have_posts();$count++) : the_post();
$open = !($count%3) ? '<div class="post-main__row clearfix">' : ''; //Create open wrapper if count is divisible by 3
$close = !($count%3) && $count ? '</div>' : ''; //Close the previous wrapper if count is divisible by 3 and greater than 0
echo $close.$open;
?>
<div class="post-main__cell">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Click to go to <?php the_title(); ?>">
<div class="post-main__cell-top">
<?php the_post_thumbnail(array(330, 167,)); ?>
<h3 class="content__category-name <?php foreach((get_the_category()) as $category) { echo $category->cat_name. ''; } ?>">
<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>
</h3>
</div>
<div class="post-main__cell-bottom">
<h2 class="archive-header"><?php the_title() ?></h2>
<p class="paragraph--time"><?php the_time('l, F jS, Y') ?></p>
</div>
</a>
</div>
<?php endfor; else : ?>
<?php endif; ?>
<?php echo $count ? '</div>' : ''; //Close the last wrapper if post count is greater than 0 ?>
<?php endif; ?>
<?php endwhile; ?>

Print category of post Wordpress

I have try all the functions related to print categories that the codex provide, but i havent found any way that works for me.
Im trying to print the categories slug for put it inside a class.
I want to print the category of the actual post in div with the class proyect, so then i can use it to filter with isotope.
<!-- feature posts -->
<div id="container">
<?php $the_query = new WP_Query('showposts=5&orderby=post_date&order=DESC'); ?>
<?php while ($the_query->have_posts()) : $the_query->the_post();
$id = get_the_ID(); ?>
<div class="proyect <?php wp_get_post_categories($id); ?>">
<div class="view view-tenth">
<a style="display:block;" href="<?php the_permalink(); ?>">
<article> <?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail('', array("class" => "")); } ?></article>
</a>
<div class="mask">
<h2><?php echo substr(strip_tags(get_the_title()),0,35); ?></h2></a>
<p class="fecha-post"><?php the_time('F j, Y'); ?></p>
<?php echo substr(strip_tags(get_the_content()),0,100); ?>
<a class="info" href="<?php the_permalink(); ?>">Ver más...</a>
</div>
</div>
</div>
<?php endwhile;?>
</div>
<!-- #feature post -->
From http://wordpress.org/support/topic/getting-category-slug-from-posts-in-the-loop:
<li class="<?php foreach(get_the_category() as $category) { echo $category->slug . ' ';} ?>">
You should be able to use:
$cats = wp_get_post_categories($post->ID);
This will be an array of the categories associated with this post. Then you can loop through them and do whatever you need.

How do I remove the most recent post from the sidebar in Wordpress?

On both the front page and the blog page - the sidebar shows the most recent post, which I find doesn't look very good duplicated against the same post expanded on the main page.
This is my code for the sidebar:
<div class="blog-sidebar">
<?php query_posts('showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="blog-sidebar-feature">
<?php if ( has_post_thumbnail() ) { ?>
<div class="blog-sidebar-image"><?php the_post_thumbnail('medium'); ?></div>
<?php
}
?>
<div class="blog-sidebar-content">
<p class="date"><?php the_time('F j, Y') ?></p>
<h3 <strong><?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?></strong></h3>
<h2 <p><a href="<?php the_permalink() ?>" rel="bookmark" title=""><?php the_title();
?></a></p></h2><?php echo get_excerpt(166); ?>
</div>
</div>
<?php endwhile;?>
<br />
<?php wp_pagenavi(); ?>
</div>
and the relevant code for how the blog appears on the home page:
<div class="blog-sidebar">
<div class="blog-sidebar-feature">
<?php query_posts('orderby=date&order=DESC&showposts=2'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div class="blog-sidebar-image"><?php the_post_thumbnail('medium'); ?></div>
<?php
}
?>
<div class="blog-sidebar-content">
<p class="date"><?php the_time('F j, Y') ?></p>
<h3 <strong><?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?></strong></h3>
<h2 <p><a href="<?php the_permalink() ?>"
rel="bookmark" title=""><?php the_title(); ?></a></p></h2><?php echo get_excerpt(166); ?>
</div>
<?php endwhile;?>
</div>
</div>
<div id="connect">
<?php query_posts('page_id=1');
while (have_posts()): the_post();
the_content();
endwhile;
wp_reset_query(); ?>
</div>
Is there any way to remove only the most recent post from the sidebar when it appears in full on the main container? Thanks in advance for any help.
UPDATE V2
So you do want recent posts, just not the post currently showing in the main content.
UPDATE V3:
This should work now. I had to change arguments of query_posts to array to make it work.
Try it now:
<?
global $wp_query;
$skip_posts=array();
if (is_single()) //only exclude posts when single post is shown
$skip_posts[]=$wp_query->post->ID;
?>
<?php query_posts( array( 'showposts'=>5,'post__not_in'=>$skip_posts)); ?>
<?php query_posts('posts_per_page=5&offset=1'); ?>
Thanks to 850010 for all the help, I went back and had a look at the offset rule and the 'array' wasn't needed. Deceptively simple.

Categories