WordPress pagination doens't work on home - php

I created a WordPress template file with latest posts and pagination. But when I set this page as the home page, the pagination wont work and it'll display the first page. As a standalone page it does work. Strange! Any thoughts about this?
<?php get_header(); ?>
<div id="primary" class="content-area clr">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article class="homepage-wrap clr">
<?php
/**
Post Content
**/ ?>
<?php if ( get_the_content() !== '' ) { ?>
<div id="homepage-content" class="entry clr">
<?php the_content(); ?>
</div><!-- .entry-content -->
<?php } ?>
<?php
/**
Features
**/
$wpex_query = new WP_Query(
array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'features',
'posts_per_page' => '-1',
'no_found_rows' => true,
)
);
if ( $wpex_query->posts ) { ?>
<div id="homepage-features" class="clr">
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<?php get_template_part( 'content-features', get_post_format() ); ?>
<?php if ( $wpex_count == '4' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-features -->
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php
/**
Portfolio
**/
$display_count = get_theme_mod('wpex_home_portfolio_count', '8');
$wpex_query = new WP_Query(
array(
'post_type' => 'portfolio',
'posts_per_page' => $display_count,
'no_found_rows' => true,
'tax_query' => wpex_home_portfolio_taxonomy(),
)
);
if ( $wpex_query->posts && '0' != $display_count ) { ?>
<div id="homepage-portfolio" class="clr">
<h2 class="heading"><span><?php _e( 'Recent Work', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<?php get_template_part( 'content-portfolio', get_post_format() ); ?>
<?php if ( $wpex_count == '4' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-portfolio -->
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php
$args = array(
'paged' => (get_query_var('paged') ? get_query_var('paged') : 1),
'posts_per_page' => 4
);
query_posts($args);
?>
<div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $wpex_count++; ?>
<article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
<?php
// Display post thumbnail
if ( has_post_thumbnail() ) { ?>
<div class="recent-blog-entry-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
</a>
</div><!-- .recent-blog-entry-thumbnail -->
<?php } ?>
<header>
<h3 class="recent-blog-entry-title"><?php the_title(); ?></h3>
<ul class="post-meta clr">
<li class="meta-date"><?php _e('Posted on','wpex'); ?> <span class="meta-date-text"><?php echo get_the_date(); ?></span> </li>
</ul>
</header>
<div class="recent-blog-entry-content entry clr">
<?php wpex_excerpt( 18, false ); ?>
</div><!-- .recent-blog-entry-content -->
</article><!-- .recent-blog -->
<?php if ( $wpex_count == '3' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endwhile; ?>
<?php wp_pagenavi();?>
</div><!-- #homepage-portfolio -->
<?php wp_reset_query(); ?>
<?php endif; ?>
</article><!-- #post -->
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>

I might have found an answer for this myself, apparently it's different when you use pagination on the frontpage. I used this and it seems to be working!:
if( is_front_page() ){
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
} else {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
}

Related

only displays certain category

I'm building my own website just for fun and for learning.
I want to display only the post with a certain category # my homepage.
I've tried a number of things but they don't seem to work
at my home.php
this is what te code looks like.
get_header(); ?>
<?php $blog_layout = get_theme_mod('blog_layout', 'small-images'); ?>
<?php if ( ($blog_layout == 'masonry') || ($blog_layout == 'fullwidth') ) {
$layout = 'fullwidth';
} else {
$layout = '';
} ?>
<?php if ( $blog_layout == 'masonry' ) {
$masonry = 'home-masonry';
} else {
$masonry = '';
} ?>
<div id="primary" class="content-area <?php echo $layout; ?>">
<main id="main" class="site-main" role="main">
<span class="diensten">ONZE DIENSTEN..</span>
<div class="diensten-content">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<div class="home-wrapper <?php echo $masonry; ?>">
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ( $blog_layout == 'large-images' || $blog_layout == 'masonry' ) {
get_template_part( 'content', 'large' );
} else {
get_template_part( 'content', get_post_format() );
}
?>
<?php endwhile; ?>
</div>
<?php moesia_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!--diensten-content-->
<
</main><!-- #main -->
</div><!-- #primary -->
And at content.php
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb">
<a href="<?php the_permalink(); ?>" class="H1-posts" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('moesia-thumb'); ?>
</a>
</div>
<?php endif; ?>
<?php if (has_post_thumbnail()) : ?>
<?php $has_thumb = ""; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="post-content <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
<?php if ( 'post' == get_post_type() ) : ?>
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php if ( (get_theme_mod('full_content') == 1) && is_home() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
</div><!-- .entry-content -->
I've tried this..`
<?php
//refine your query to the category you desire either a slug(example below) or category id
$args = array(
'category_name' => 'my_category_slug',
);
//create the query using the arguments
$query = new WP_Query($args);
?>
//create the loop to show the posts
<?php if($query->have_posts()): ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
did not work. How do I fix this?`If It displays only the posts with a certain class if also good.
Thanks..

Editing PHP grid page for WordPress site

I would like to Edit the Grid Artist page to only have 12 artists and land on the producers section instead of all artist.
Here' s the link of website page : http://cascaderecords.fr/roster/
PHP code:
<?php
if (ci_setting('artists_isotope') == 'enabled' AND !wp_is_mobile() AND !is_tax() ):
?>
<ul class="filters-nav group">
<li><?php _e('All Artists', 'ci_theme'); ?></li>
<?php
$args = array('hide_empty' => 1);
$cats = get_terms('artist-category', $args);
?>
<?php foreach ( $cats as $cat ): ?>
<li><?php echo $cat->name; ?></li>
<?php endforeach; ?>
</ul>
<?php
endif; // is isotope enabled
global $paged;
if ( ci_setting('artists_isotope') == 'enabled' AND !wp_is_mobile() AND !is_tax() ) {
$args = array(
'post_type' => 'cpt_artists',
'posts_per_page' => -1
);
} else {
$args = array(
'post_type' => 'cpt_artists',
'posts_per_page' => ci_setting('artists_per_page'),
'paged' => $paged
);
}
$the_query = !empty($the_query) ? $the_query : new WP_Query($args);
?>
<article class="row">
<ul class="col-md-12 items-list-grid filter-container <?php echo $filter; ?>">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $cats = wp_get_object_terms($post->ID, 'artist-category'); ?>
<li class="<?php foreach ( $cats as $cat ) : echo $cat->slug.' '; endforeach; echo $grid; ?>">
<article id="artist-<?php the_ID(); ?>" <?php post_class('row'); ?>>
<div class="col-md-12">
<?php if ( has_post_thumbnail() ) : ?>
<figure>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('ci_thumb_square'); ?>
</a>
</figure>
<?php endif; ?>
<h2><?php the_title(); ?></h2>
</div><!-- /col-md-12 -->
</article><!-- /row -->
</li><!-- /col-md-4 -->
<?php endwhile; wp_reset_postdata(); ?>
</ul>
Try change:
'posts_per_page' => -1
to:
'posts_per_page' => 12
Not sure what "ci_setting('artists_per_page')" is in below variable, need more info:
'posts_per_page' => ci_setting('artists_per_page'),

Use image as a Wordpress conditional

I'm trying to use a image as a conditional to show different post types. I have one post with images and another one without images. So I built a If else statement and a while loop that calls the post. At the end I have a page number code. But the loop is infinite, I'm not finding where to close it. Some one can help me?
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="texto-longo">
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
<div class="row-2 w-row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'cat' => '3',
);
$wp_query = new WP_Query( $args );
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="_w-image container-novidades">
<?php if( get_field('imagem_do_evento') ): ?>
<div class="w-row">
<div class="column w-col w-col-4"><img class="<?php the_field('imagem_do_evento');?>">
</div>
<div class="colomn-text-novidades w-col w-col-8">
<h1 class="txt-blue"><?php the_title();?></h1>
<h3 class="txt-blue"><?php the_field('imagem_do_evento');?></h3>
<p><?php get_the_date('d/m/Y'); ?></p>
<p><?php the_field('imagem_do_evento');?></p>
</div>
</div>
</div>
<?php wp_reset_query(); else : ?>
<p>Ainda não temos novidades :(</p>
<?php endif; ?>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="navegacao-paginas">
<div class="paginacao twisted w-inline-block">
<div class="seta-text"><?php echo get_previous_posts_link( '⟶' ); // display newer posts link ?></div>
</div>
<div class="paginacao w-inline-block">
<div class="seta-text"><?php echo get_next_posts_link( '⟶', $wp_query->max_num_pages ); // display older posts link ?></div>
</div>
</nav>
<?php } ?>
</div>
<?php wp_reset_query(); else : ?>
<?php endif; ?>
Try to put the endwhile and edn if in the same row like this
<?php endwhile; wp_reset_query(); endif; ?>
I Figured it Out. It was a div opening outside the if statement. And I wasn't closing all if else properly. Check out the final code.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="texto-longo">
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
<div class="row-2 w-row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'cat' => '4',
);
$wp_query = new WP_Query( $args );
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php if( get_field('foto_novidades') ): ?>
<div class="_w-image container-novidades">
<div class="w-row">
<div class="column w-col w-col-4"><img class="img-novidades" src="<?php the_field('foto_novidades');?>">
</div>
<div class="colomn-text-novidades w-col w-col-8">
<h1 class="txt-blue"><?php the_title();?></h1>
<h3 class="txt-blue"><?php the_field('sub_titulo_novidades');?></h3>
<p><?php get_the_date('d/m/Y'); ?></p>
<p><?php the_field('texto_novidades');?></p>
</div>
</div>
</div>
<?php else:?>
<div class="container-novidades">
<h1 class="txt-blue"><?php the_title();?></h1>
<h3 class="txt-blue"><?php the_field('sub_titulo_novidades');?></h3>
<p><?php get_the_date('d/m/Y'); ?></p>
<p><?php the_field('texto_novidades');?></p>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="navegacao-paginas">
<div class="paginacao twisted w-inline-block">
<div class="seta-text"><?php echo get_previous_posts_link( '⟶' ); // display newer posts link ?></div>
</div>
<div class="paginacao w-inline-block">
<div class="seta-text"><?php echo get_next_posts_link( '⟶', $wp_query->max_num_pages ); // display older posts link ?></div>
</div>
</nav>
<?php } ?>
</div>
<?php wp_reset_query(); else : ?>
<p>Ainda não temos novidades :(</p>
<?php endif; ?>

Add pagination to this Wordpress Home

I Guys! I'm working on a pagination for this theme homepage. The original code is:
<?php
/**
Blog
**/
$display_count = get_theme_mod('wpex_home_blog_count', '30');
$wpex_query = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => $display_count,
'no_found_rows' => true,
)
);
if ( $wpex_query->posts && '0' != $display_count ) { ?>
<div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
<?php
// Display post thumbnail
if ( has_post_thumbnail() ) { ?>
<div class="recent-blog-entry-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
</a>
</div><!-- .recent-blog-entry-thumbnail -->
<?php } ?>
<header>
<h3 class="recent-blog-entry-title"><?php the_title(); ?></h3>
<?php
// Display post meta details
wpex_post_meta() ;?>
</header>
<div class="recent-blog-entry-content entry clr">
<?php wpex_excerpt( 18, false ); ?>
</div><!-- .recent-blog-entry-content -->
</article><!-- .recent-blog -->
<?php if ( $wpex_count == '3' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-portfolio -->
<?php } ?>
What I did is, I changed it to
<div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query('showposts=4' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php $wpex_count++; ?>
<article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
<?php
// Display post thumbnail
if ( has_post_thumbnail() ) { ?>
<div class="recent-blog-entry-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
</a>
</div><!-- .recent-blog-entry-thumbnail -->
<?php } ?>
<header>
<h3 class="recent-blog-entry-title"><?php the_title(); ?></h3>
<ul class="post-meta clr">
<li class="meta-date">
<?php _e('Posted on','wpex'); ?>
<span class="meta-date-text"><?php echo get_the_date(); ?></span>
</li>
</ul>
</header>
<div class="recent-blog-entry-content entry clr">
<?php wpex_excerpt( 18, false ); ?>
</div><!-- .recent-blog-entry-content -->
</article><!-- .recent-blog -->
<?php if ( $wpex_count == '3' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endwhile; ?>
</div>
<?php wp_pagenavi();?>
<?php wp_reset_postdata(); ?>
</article>
This seems to work on the first page, but when i go to page 2, it gives an copy after copy after copy of the whole page, like an infinate page one. That's not how it should work.
Any ideas what goes wrong here? Does this make any sense to you?
EDIT:
I tried the following. Right now the first page looks good, but when i press the second page on the pagination, it outputs the same as the first.
<?php
$args = array(
'paged' => (get_query_var('paged') ? get_query_var('paged') : 1),
'posts_per_page' => 4
);
query_posts($args);
?>
<div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $wpex_count++; ?>
<article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
<?php
// Display post thumbnail
if ( has_post_thumbnail() ) { ?>
<div class="recent-blog-entry-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
</a>
</div><!-- .recent-blog-entry-thumbnail -->
<?php } ?>
<header>
<h3 class="recent-blog-entry-title"><?php the_title(); ?></h3>
<ul class="post-meta clr">
<li class="meta-date"><?php _e('Posted on','wpex'); ?> <span class="meta-date-text"><?php echo get_the_date(); ?></span> </li>
</ul>
</header>
<div class="recent-blog-entry-content entry clr">
<?php wpex_excerpt( 18, false ); ?>
</div><!-- .recent-blog-entry-content -->
</article><!-- .recent-blog -->
<?php if ( $wpex_count == '3' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endwhile; ?>
<?php wp_pagenavi();?>
</div><!-- #homepage-portfolio -->
<?php endif; ?>
<?php wp_reset_query(); ?>
</article><!-- #post -->
<?php endwhile; ?>
Whole original code:
<div id="primary" class="content-area clr">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article class="homepage-wrap clr">
<?php
/**
Post Content
**/ ?>
<?php if ( get_the_content() !== '' ) { ?>
<div id="homepage-content" class="entry clr">
<?php the_content(); ?>
</div><!-- .entry-content -->
<?php } ?>
<?php
/**
Features
**/
$wpex_query = new WP_Query(
array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'features',
'posts_per_page' => '-1',
'no_found_rows' => true,
)
);
if ( $wpex_query->posts ) { ?>
<div id="homepage-features" class="clr">
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<?php get_template_part( 'content-features', get_post_format() ); ?>
<?php if ( $wpex_count == '4' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-features -->
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php
/**
Portfolio
**/
$display_count = get_theme_mod('wpex_home_portfolio_count', '8');
$wpex_query = new WP_Query(
array(
'post_type' => 'portfolio',
'posts_per_page' => $display_count,
'no_found_rows' => true,
'tax_query' => wpex_home_portfolio_taxonomy(),
)
);
if ( $wpex_query->posts && '0' != $display_count ) { ?>
<div id="homepage-portfolio" class="clr">
<h2 class="heading"><span><?php _e( 'Recent Work', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<?php get_template_part( 'content-portfolio', get_post_format() ); ?>
<?php if ( $wpex_count == '4' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-portfolio -->
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php
/**
Blog
**/
$display_count = get_theme_mod('wpex_home_blog_count', '30');
$wpex_query = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => $display_count,
'no_found_rows' => true,
)
);
if ( $wpex_query->posts && '0' != $display_count ) { ?>
<div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
<?php
// Display post thumbnail
if ( has_post_thumbnail() ) { ?>
<div class="recent-blog-entry-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
</a>
</div><!-- .recent-blog-entry-thumbnail -->
<?php } ?>
<header>
<h3 class="recent-blog-entry-title"><?php the_title(); ?></h3>
<?php
// Display post meta details
wpex_post_meta() ;?>
</header>
<div class="recent-blog-entry-content entry clr">
<?php wpex_excerpt( 18, false ); ?>
</div><!-- .recent-blog-entry-content -->
</article><!-- .recent-blog -->
<?php if ( $wpex_count == '3' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-portfolio -->
<?php } ?>
<?php wp_reset_postdata(); ?>
</article><!-- #post -->
<?php comments_template(); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
Try declaring $paged outside, and call this in the query_posts
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 4
);
query_posts($args . '&paged=' . $paged);
I might have found an answer for this myself, apparently it's different when you use pagination on the frontpage. I used this and it seems to be working!:
if( is_front_page() ){
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
} else {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
}

if have posts display div else display no posts statment

I have no idea why this code is not working. I am trying to create a conditional statement where if this custom post type has posts then display customer-section div. if post does not exist then print no post statement. I did everything I thought I am suppose to but I must be doing something silly wrong because I can still see the customer-section div even though there are no posts.
here is what I have:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summery">
<?php the_excerpt(); ?>
</div>
<footer>
<?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
</footer>
</div>
<hr />
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
What you are currently doing is checking if your initial have_posts() (the default query) condition is true and if this is a page template it is always true. Actually (for page templates) you don't need that check at all, since WordPress will return 404 if the page is not found.
You need a check for posts for your custom query:
<?php the_post(); ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) );
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summery">
<?php the_excerpt(); ?>
</div>
<footer>
<?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
</footer>
</div>
<hr />
</div>
<?php endwhile;
else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
wp_reset_query();
?>
</div>
</div>
I believe the issue is your syntax with the two loops.
You're missing the if statement before the while condition
You're trying to load your $loop loop inside the standard page or post loop.
Try this instead:
<?php
$case_studies = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) );
if ( $case_studies->have_posts() ) : ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php while ( $case_studies->have_posts() ) : $case_studies->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<footer>
<?php if (get_the_tags()) { ?>
<p><?php the_tags(); ?></p>
<?php } ?>
</footer>
</div>
<hr />
</div>
<?php endwhile; ?>
</div>
</div>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; wp_reset_query(); ?>
this is what worked for me. You don't need to end the while loop if it's inside the WP_Query. All you have to do is close the if statement for the main div you want to show, add the while for the repeating section and if it doesn't work, I'd try deleting the conditions you have set on your footer.
<?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) ); if($loop->have_posts()): ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summery">
<?php the_excerpt(); ?>
</div>
<footer>
<?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
</footer>
</div>
<hr />
</div>
<?php } wp_reset_postdata(); ?>
</div>
</div>
<?php endif; ?>

Categories