I have some posts displaying from a specific category but need them to have a little different styling on every second post.
This code I have displays all posts in a list with the same styling.
How do I edit this code and make it so I can edit the styling of ONLY every second post?
As it stands there's text on the left and an image on the right - the second posts just need to switch sides - it's simple styling but I'm unsure how to break the posts up to edit #case-left & #case-right are the two divs that need to switch.
Thanks in advance.
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
?>
<div class="serve-inner-split">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<div id="case-right" class="serve-grey">
<?php
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
</div>
</div>
<?php endforeach;
}
}
?>
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
$counter = 1;
$class = '';
foreach ($posts as $post):
setup_postdata($post);
if($counter%2 == 0) {
$class = "even-no";
} else {
$class = '';
}
?>
<div class="serve-inner-split <?php echo $class; ?>">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<div id="case-right" class="serve-grey">
<?php
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
</div>
</div>
<?php $counter++; ?>
<?php endforeach;
}
}
?>
Ok, so when editing your question I saw that you want to style it so that the divs switch places. You can do that like this in php
<?php // PAGE LINK/TITLE
if (is_page()){}
$i = 0;
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
$i++;
if ($i % 2 == 0):?>
<div class="serve-inner-split">
<div id="case-split">
<?php if ( has_post_thumbnail() ):?>
<div id="case-right" class="serve-grey">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="serve-inner-split">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<?php if ( has_post_thumbnail() ):?>
<div id="case-right" class="serve-grey">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endif;
?>
<?php endforeach;
wp_reset_postdata();
} ?>
But in my opinion, that's kinda unnecessary. You can also do it like this:
<?php // PAGE LINK/TITLE
if (is_page()){}
$i = 0;
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
$class = '';
$i++;
if ($i % 2 == 0){
$class = 'right_image'
}
?>
<div class="serve-inner-split <?php echo $class; ?>">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<?php if ( has_post_thumbnail() ):?>
<div id="case-right" class="serve-grey">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach;
wp_reset_postdata();
} ?>
And then float the image the other way (or use absolute positioning or whatever suits you), with the '.right_image' class in the css.
Related
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
I'm trying to load all Wordpress posts into three different Divs as equal,
like this
<div class="row">
<div class="col-4">
POSTS HERE 1/3
</div>
<div class="col-4">
POSTS HERE 1/3
</div>
<div class="col-4">
POSTS HERE 1/3
</div>
</div>
I've already done something similar on other pages, but the difference is I'm only loading 6 posts there, so it's easier,
but now I wanna list all posts in the page into 3 columns,
this is the code I've used for 6 posts:
<?php
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<?php query_posts('showposts=2'); ?>
<div class="col-sm-4">
<div class="article_list">
<?php $posts = get_posts('numberposts=2&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "2") { break; } else { ?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php $count1++; } ?>
<?php endforeach; ?>
</div>
</div>
<?php query_posts('showposts=2'); ?>
<div class="col-sm-4">
<div class="article_list">
<?php $posts = get_posts('numberposts=2&offset=2'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "2") { break; } else { ?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php $count2++; } ?>
<?php endforeach; ?>
</div>
</div>
<?php query_posts('showposts=2'); ?>
<div class="col-sm-4">
<div class="article_list">
<?php $posts = get_posts('numberposts=2&offset=4'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == "2") { break; } else { ?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php $count3++; } ?>
<?php endforeach; ?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Try the below code. you will get post list in below format.
<?php
$loop_counter = $innerBreak = 1;
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6));
if($wpb_all_query->have_posts()):
while($wpb_all_query->have_posts()) :
$wpb_all_query->the_post();
if($innerBreak == 1){
?>
<!-- when complete listing of 3 post open the new div -->
<div class="col-sm-4">
<?php
}
?>
<div class="article_list">
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
</div>
<?php
//when complete listing of 3 post closed previously div.
if($loop_counter%3==0){ echo '</div>'; $innerBreak = 1;}else{$innerBreak = 0;}
$loop_counter++; endwhile;
else:
echo "<div>No Results Found</div>";
endif;
?>
Thanks to Shivendra Singh
after some small tweaks, here is the code to equally split all posts into three columns
<?php
$loop_counter = $innerBreak = 1;
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));
if($wpb_all_query->have_posts()):
while($wpb_all_query->have_posts()) :
$wpb_all_query->the_post();
if($innerBreak == 1){
?>
<?php $posts_count = round((($wpb_all_query->post_count) / 3)+0.5); ?>
<!-- when complete listing of 3 post open the new div -->
<div class="col-sm-4">
<div class="article_list" data-parallax='{"y" : -150, "distance": 2000, "smoothness": 10}'>
<?php
}
?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php
//when complete listing of 3 post closed previously div.
if($loop_counter%$posts_count==0){ echo '</div></div>'; $innerBreak = 1;}else{$innerBreak = 0;}
$loop_counter++; endwhile;
else:
echo "<div>No Results Found</div>";
endif;
?>
In line $wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6)); ?> you use 'posts_per_page'=>6.
Try to: 'posts_per_page'=>-1 it's load all posts.
Then try instead of your code:
<!-- If you use bootstrap -->
<div class="container">
<div class="row">
<?php
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<?php while( $wpb_all_query->have_posts() ){ $wpb_all_query->the_post(); ?>
<div class="col-md-4">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); // or the_content(); or any ?>
</div>
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
</div>
I need to have a different layout for the first post in every page, but don't know where to start. The code is the following:
<?php get_header(); ?>
<div class="row">
<section class="small-12 columns grid-style">
<?php $i = 0; $counter = range(0, 200, 3); ?>
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array('offset'=> 0, 'paged'=>$paged);
$all_posts = new WP_Query($args);
if (have_posts()) : while($all_posts->have_posts()) : $all_posts->the_post();?>
<?php if ($i % 3 == 0) { echo '<div class="row journal" data-equal=".post">'; } ?
<!--Post -->
<article class="grid-style post" id="post-<?php the_ID(); ?>"
<div class="post-img">
<?php get_template_part( 'inc/postformats/grid-style' ); ?>
</div>
<div class="post-box">
<div class="post-title">
<h2><?php echo ShortenText(get_the_title(), 50); ?></h2>
</div>
<aside class="post_categories">
<?php the_category(', '); ?>
</aside>
<div class="post-content">
<?php echo ShortenText(get_the_excerpt(), 170); ?>
</div>
</div>
</article>
<!--/Post -->
<?php if (in_array($i + 1, $counter)){ echo '</div>'; } ?>
<?php $i++; endwhile; ?>
<div class="small-12 columns">
<?php theme_pagination($all_posts->max_num_pages, 1, true); ?>
</div>
<?php else : ?>
<p><?php _e( 'Please add posts from your WordPress admin page.', THB_THEME_NAME ); ?></p>
<?php endif; ?>
</section>
</div>
<?php get_footer(); ?>
I need to use a different image size, so I need to output everything inside < ! - - Post - - > again for the first post only.
Are you trying to include div.row.journal to the first post?
If it's the case try doing this:
Replace <?php if ($i % 3 == 0) { echo '<div class="row journal" data-equal=".post">'; } ? by the following: (It'll echo it only on the first page, for the first post)
<?php
if ($all_posts->current_post == 0 && !is_paged()) {
echo '<div class="row journal" data-equal=".post">';
}
?>
Doing the same for the closing div by replacing <?php if (in_array($i + 1, $counter)){ echo '</div>'; } ?> by :
<?php
if ($all_posts->current_post == 0 && !is_paged()) {
echo '</div>';
}
?>
You have to define a counter and ask with if...else... inside the loop
$post_counter = 1;
while (start_loop):
if ($post_counter == 1){
echo "<div id='1'></div>";
else:
echo "<div id='2'></div>";
$post_counter += 1;
//end loop
I think that should help you.
This is the code I have now, I was able to output the HTML for the first post, and a different one for ALL the posts, but I am getting 2 first posts. One using the first layout and the second using the second layout
<?php
/*
Template Name: Blog - Grid Style
*/
?>
<?php get_header(); ?>
<div class="row">
<section class="small-12 columns grid-style">
<?php $i = 0; $counter = range(0, 200, 3); ?>
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array('offset'=> 0, 'paged'=>$paged);
$all_posts = new WP_Query($args);
$post_counter = 0;
if (have_posts()) : while($all_posts->have_posts()) : $all_posts->the_post(); $post_counter++;?>
<?php if ($i % 3 == 0) { echo '<div class="row journal" data-equal=".post">'; } ?>
<?php if ($post_counter == 1){ ?>
<!--Layout Post 1-->
<div class="row">
<article class="grid-style post small-9 small-centered column" id="post-<?php the_ID(); ?>">
<div class="post-img">
<?php get_template_part( 'inc/postformats/grid-style' ); ?>
</div>
<div class="post-box">
<div class="post-title">
<h2><?php echo ShortenText(get_the_title(), 50); ?>1</h2>
</div>
<aside class="post_categories">
<?php the_category(', '); ?>
</aside>
<div class="post-content">
<?php echo ShortenText(get_the_excerpt(), 170); ?>
</div>
</div>
</article>
</div>
<!--/Layout Post 1-->
<?php } ?>
<!--Other Posts -->
<div class="row">
<article class="grid-style post small-9 small-centered column" id="post-<?php the_ID(); ?>">
<div class="post-img">
<?php get_template_part( 'inc/postformats/grid-style' ); ?>
</div>
<div class="post-box">
<div class="post-title">
<h2><?php echo ShortenText(get_the_title(), 50); ?></h2>
</div>
<aside class="post_categories">
<?php the_category(', '); ?>
</aside>
<div class="post-content">
<?php echo ShortenText(get_the_excerpt(), 170); ?>
</div>
</div>
</article>
</div>
<!--/Other Posts -->
<?php if (in_array($i + 1, $counter)){ echo '</div>'; } ?>
<?php $i++; endwhile; ?>
<div class="small-12 columns">
<?php theme_pagination($all_posts->max_num_pages, 1, true); ?>
</div>
<?php else : ?>
<p><?php _e( 'Please add posts from your WordPress admin page.', THB_THEME_NAME ); ?></p>
<?php endif; ?>
</section>
</div>
<?php get_footer(); ?>
I've created an archive page for a custom post type in Wordpress. I want to output data if certain fields match.
Within the first if statement, I want to check what product the user has signed up to (held within $product and match it against the current items custom field (held within $title).
All it's doing is seeing what the user signed up to then outputting the content (which is all the data below <?php if($match == "yes"){ ?>.
The problem I have is wp_reset_postdata() is killing the data held within $match. Is there a way around this? If I don't include the setting up of post data and don't include the reset then the rest of the page doesn't show the correct info.
I'm using Advanced Custom Fields relationship field (http://www.advancedcustomfields.com/resources/field-types/relationship/).
Any help would be much appreciated.
User signed up as: <?php $current_user = wp_get_current_user();
$board = $current_user->work;
$product = $current_user->product;
echo $product; ?>
<br />
<?php while (have_posts()) : the_post(); ?>
<?php $products = get_field('product');
if( $products ):
foreach( $products as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$title = $post->post_title;
echo $title;
if($product == $title) {
$match = "yes";
}
endforeach;
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
endif; ?>
<?php if($match == "yes"){ ?>
<div id="module-area" style="margin-top:0px!IMPORTANT;">
<div id="modules-top"></div>
<div id="modules-repeat" style="position:relative;padding-left:10px;padding-right:10px;width:625px!IMPORTANT;">
<div class="topic">
<p><?php the_title(); ?></p>
</div>
<div class="description">
<p><?php the_field('description') ?></p>
</div>
<div class="date">
<p style="text-align:center!IMPORTANT;"><?php the_modified_time('d.m.y'); ?></p>
</div>
<div class="action">
<a class="train-button" href="<?php echo the_permalink(); ?>"></a>
</div>
<div class="clear"></div>
</div>
<div style="margin-bottom:5px;" id="modules-bottom"></div>
</div>
<?php } ?>
<?php endwhile; ?>
User signed up as: <?php $current_user = wp_get_current_user();
$board = $current_user->work;
$product = $current_user->product;
?>
<?php $products = get_field('product');
if( $products ):
foreach( $products as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$title = $post->post_title;
if($product == $title) {
wp_reset_postdata(); ?>
<div id="module-area" style="margin-top:0px!IMPORTANT;">
<div id="modules-top"></div>
<div id="modules-repeat" style="position:relative;padding-left:10px;padding-right:10px;width:625px!IMPORTANT;">
<div class="topic">
<p><?php the_title(); ?></p>
</div>
<div class="description">
<p><?php the_field('description') ?></p>
</div>
<div class="date">
<p style="text-align:center!IMPORTANT;"><?php the_modified_time('d.m.y'); ?></p>
</div>
<div class="action">
<a class="train-button" href="<?php echo the_permalink(); ?>"></a>
</div>
<div class="clear"></div>
</div>
<div style="margin-bottom:5px;" id="modules-bottom"></div>
</div>
<?php
}
endforeach;
endif; ?>
wp_reset_postdata();
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.