I need every three divs of class "loopcontent" to be wrapped in a new div of class "threeacross". I'm new to WordPress and really not a PHP guy yet, but I'm learning as I go.
Here's my loop:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div class="loopcontent">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else {
// the current post lacks a thumbnail
}
?>
<h1><?php the_title(); ?></h1>
<?php the_excerpt(); ?>
</div>
<?php endwhile; else : ?>
<div class="loopcontent">
<h1>Page Not Found</h1>
<p>Looks like the page you're looking for isn't here anymore. Try browsing the categories, archives, or using the search box below.</p>
<?php include(TEMPLATEPATH.'/searhform.php'); ?>
</div>
<?php endif; ?>
Something like this should work. This keeps a running count and places your div wraps every three posts. Let me know if this helps. If not, let me know what it's outputting, and I can tweak it:
<?php
if(have_posts()) : for($count=0;have_posts();$count++) : the_post();
$open = !($count%3) ? '<div class="threeacross">' : ''; //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="loopcontent">
<?php
if(has_post_thumbnail()) the_post_thumbnail();
else{
// the current post lacks a thumbnail
}
?>
<h1><?php the_title(); ?></h1>
<?php the_excerpt(); ?>
</div>
<?php endfor; else : ?>
<div class="loopcontent">
<h1>Page Not Found</h1>
<p>Looks like the page you're looking for isn't here anymore. Try browsing the categories, archives, or using the search box below.</p>
<?php include(TEMPLATEPATH.'/searhform.php'); ?>
</div>
<?php endif; ?>
<?php echo $count ? '</div>' : ''; //Close the last wrapper if post count is greater than 0 ?>
Related
I am using get_template_part('content'); in single.php. The the_post_thumbnail('large-thumbnail'); function shows the thumbnail image perfectly before. But it does not show with is_single() function in content.php.
content.php code:
<?php
if(is_single()){
the_post_thumbnail('large-thumbnail');
the_content();
}else{
the_post_thumbnail('small-thumbnail');
echo get_the_excerpt();
?>
Read moreĀ»
<?php } ?>
single.php:
<article>
<div class="single_post_content">
<h5><?php the_title(); ?></h5>
<?php
get_template_part('content');
?>
</div>
</article>
I think you forget to write wp loop in your single.php file. follow this code,
<?php while ( have_posts() ) : the_post();
get_template_part('content');
endwhile; wp_reset_postdata(); ?>
the_post_thumbnail() do not work out of wp loop, So call your get_template_part() function between the loop. May be now its will solve your problem.
I have a blog(offline making) and I have Made a wordpress loop it checks if the First post it it uses different style and rest of other posts uses different.
Problem is: It works fine but when I go to next page(2) it shows again, I only want to apply for only First latest post. Not for First post on every page.
Code:
<?php
while ( have_posts() ) : the_post();?>
<?php $count++; ?>
<?php if ($count == 1 && is_home()) :
//First post different style goes here
<?php else : ?>
//Rest Other post different style goes here
<?php endif; ?>
<?php
endwhile;
?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="page_content">
<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
You should use get_query_var('paged'). It will return the current page number, starting from 0.
So you should add
<?php if ($count == 1 && is_home() && get_query_var('paged') == 0 ) : ?>
//First post different style goes here
<?php else : ?>
//Rest Other post different style goes here
<?php endif; ?>
Add this to your function.php to change the number of posts
add_filter( 'pre_get_posts', 'custom_pre_get_posts' );
function custom_pre_get_posts($query) {
if ( is_home() && get_query_var( 'paged' ) > 0 ) {
$query->set('posts_per_page', 12);
}
return $query;
}
how can i create a grid like image? for show last posts
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php $latest_post = get_posts( 'numberposts=4' ); ?>
<?php foreach( $latest_post as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?><br />
<?php the_content(); ?>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
One option is to use PHP to swap around the position of the image based on whether or not the increment is odd or even, for instance:
$i = 0;
while ( have_posts() ) : the_post();
if ($i % 2 == 0 ) {
// Display two columns with image on left
}
else {
// Display two columns with image on right
}
$i++;
endwhile;
If you're building your theme from scratch I'd suggest using a grid framework to handle your columns, otherwise see if the theme you're using has a grid framework already.
Edit:
There are also ways of doing this without actually having to change the markup of the page. For instance:
Swapping columns (left / right) on alternate rows
In this case you'd be able to generate your post markup without the if statement and just use CSS to swap the position of the image/video.
You can use the masonry technology or you can try this plugin
In your code above you fired two WordPress content loops. I am not sure why you had to fire two loops, although they both will work. First will print the recent posts depending on the number of Posts Per Page you selected via Settings->Reading tab from your WordPress Dashboard and the second one will again print the top 4 recent posts. I am using the first loop to tell you how you can create the attached image like grid.
Below is the PHP/HTML modifications that you have to make:
<?php $count = 1; if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="one-half <?php if( $count++ % 2 == 0 ): echo 'last-col'; endif; ?>">
<?php
// the function below will print the featured image attached to the post
the_post_thumbnail( 'your-featured-image-sizename' ); ?>
</div>
<!-- one-half -->
<div class="one-half">
<span class="post_stamp"><?php the_time( 'j m Y' ); ?></span>
<span class="post_cat"><?php the_category(', '); // This will print News if you filled the post under News Category ?></span>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); // Insteading of printing the whole content this function will print exceprt only ?>
</div>
<!-- one-half -->
<div class="clearfix"><!-- --></div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
You would have to put the below given links to your style file:
<style>
.one-half{width: 50%; float: left;}
.last-col{float: right;}
.clearfix{clear: both; overflow: hidden;}
</style>
After making the above changes your posts will be displayed as the attached image. Good Luck (y)
I am using a wordpress theme, Pitch Pro, and it has a built in slider. My issue is that sometimes the slider will flash the wrong image for a second. I noticed that it does not do it normally but only if I go from a page such as support to the home page. I have tried changed and setting the order. Changing the max amount of slides, currently 6, but nothing seems to fix it.
The site currently is jrummy16.com/test if anyone has any idea on a fix.
Not sure if this could relate to it anyway but the server I am using has issues and to fix it hostgator told me to add define( 'CONCATENATE_SCRIPTS', false ); to my config.php file. It fixed the issue but I do not recall the slider having this issue before adding that.
I have no idea on how to even start troubleshooting this. So any help would be greatly appreciated.
EDIT
All of the files were found inside the Theme, Pitch Pro.
I opened up my home.php file and I find this at the top for slider.
<?php
$slider_template = apply_filters('pitch_slider_template', array('slider', null));
get_template_part( $slider_template[0], $slider_template[1] );
?>
I then went and opened up slider.php in the same folder. It has this code,
<?php
$slides = new WP_Query(array(
'numberposts' => siteorigin_setting('slider_max_slides'),
'nopaging' => true,
'post_type' => 'slide',
'orderby' => 'menu_order',
'order' => 'ASC'
));
if($slides->have_posts()){
?>
<div id="slider">
<div class="container">
<div class="slides nivoSlider">
<?php while ($slides->have_posts()) : $slides->the_post(); if(has_post_thumbnail()) : ?>
<?php if(get_post_meta(get_the_ID(), 'slide_destination', true)) : $destination = get_post_meta(get_the_ID(), 'slide_destination', true) ?>
<?php echo '<a href="'.esc_url(get_permalink($destination)).'" title="'.esc_attr(get_the_title($destination)).'">' ?>
<?php elseif(get_post_meta(get_the_ID(), 'slide_destination_url', true)) : $destination = get_post_meta(get_the_ID(), 'slide_destination_url', true) ?>
<?php echo '<a href="'.esc_url($destination).'">' ?>
<?php endif; ?>
<?php echo get_the_post_thumbnail(get_the_ID(), 'slide') ?>
<?php if(!empty($destination)) echo '</a>' ?>
<?php endif; endwhile; ?>
</div>
<?php $slides->rewind_posts(); ?>
<div class="indicators-wrapper">
<ul class="indicators">
<?php while ($slides->have_posts()) : $slides->the_post(); if(has_post_thumbnail()) : ?>
<li class="indicator <?php if($slides->current_post == 0) echo 'active' ?> indicator-group-<?php echo $slides->post_count ?>">
<div class="indicator-container">
<div class="pointer"></div>
<h4><?php the_title() ?></h4>
<?php the_excerpt() ?>
</div>
</li>
<?php endif; endwhile; ?>
</ul>
</div>
</div>
</div>
<?php
wp_reset_postdata();
}
I am not sure if this will help or not but I hope that it does.
I think the reason is that before slider loads completely, the page will show last image in the list of slides and in your case it is this one:
<img width="705" height="344" src="http://jrummy16.com/test/wp-content/uploads/BA-slider.jpg" class="attachment-slide wp-post-image" alt="BA-slider">
Here is a similar problem and it has few of the solutions which you may use it as well.
UPDATE:
I found this article which suggests to modify your CSS. I would paste the code here but the article uses image to show CSS code:)
I have a page with many items pulled from posts.
I have it set up to only display 10 posts at a time, but my previous/next button isn't actually displaying the next or previous posts - it justs keeps displaying the same posts.
Here's the function I wrote:
function add_shop() {
if (is_page('shop') || is_page('7')) { ?>
<div id="content">
<div class="post_box">
<div id="column_1">
<div id="shop_wrapper">
<?php query_posts('tag=shop&orderby=title&order=ASC&posts_per_page=10');
if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="shop_item"> <img src="<?php getCustomField('shop_thumbnail_image'); ?>" alt='photo of <?php getCustomField('title'); ?>' class="shop_thumb" />
<div class="shop_content">
<h4><a href="<?php getCustomField('link_to_project_page'); ?>">
<?php getCustomField('title'); ?>
</a></h4>
<?php getCustomField('duration'); ?>
<?php getCustomField('paypal_code'); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php posts_nav_link(); ?>
</div>
<?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
</div>
</div>
<div id="sidebars">
<div id="sidebar_1" class="sidebar">
<ul class="sidebar_list">
<li class="widget">
<div class="widget_box">
<?php dynamic_sidebar(5); ?>
</div>
</li>
</ul>
</div>
</div>
<?php } }
Based on the above help, I just just got it working with this:
<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($page == 1) {
query_posts('tag=shop&orderby=title&order=ASC&posts_per_page=10&paged=$page');
} else {
$numposts = get_option('posts_per_page');
// work out offset
$offset = (($page -1) * $numposts); // i.e. page 2 - 1 = 1 * 10 (10 for the number of posts)
query_posts("tag=shop&orderby=title&order=ASC&offset=$offset&paged=$page&showposts=$numposts");
}
if (have_posts()) : ?>
You're manually calling query_posts(), which will overwrite any variables related to fetching posts that wordpress tries to send itself. If you want to keep the query string that it's already sending, you should concatenate to it instead of replacing it:
query_posts($query_string.'&tag=shop&orderby=title&order=ASC&posts_per_page=10');
Alternatively, if you only want to include the "page" variable, include it with $paged:
query_posts('tag=shop&orderby=title&order=ASC&posts_per_page=10&paged='.$paged);