I am having trouble with wordpress post page with pagination. I have installed a pagination plugin (WP-PageNavi), and I have being able to print all my posts so far, but pagination just shows one page (the present one). How can I make pagination work properly?
<?php
// args query
$args = array(
'post_type' => 'post',
'order' => 'DESC'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
<p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
// reset query
wp_reset_postdata();
?>
<?php include($path.'libs/pagination.php'); ?>
Here is my pagination.php file:
<div class="pagination">
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
</div>
I guess you have put wp_pagenavi(); function to libs/pagination.php.
So, replace it with
wp_pagenavi(array( 'query' => $recent_posts ));
So, final code should look like,
<?php
// args query
$args = array(
'post_type' => 'post',
'order' => 'DESC'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
<p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
echo '<div class="pagination">';
if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $recent_posts )); }
echo '</div>';
wp_reset_postdata();
?>
I found the problem in my code:
I was not adding anything for the pagination in my query, so it was returning all my posts.
So I added a pagination logic to my custom query and it did work.
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => 5,
'paged' => $paged
);
// custom query
$recent_posts = new WP_Query($custom_args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
<p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
echo '<div class="pagination">';
if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $recent_posts )); }
echo '</div>';
wp_reset_postdata();
?>
Related
I have been trying to customize my site but I have met a problem... As I have stated in the title, what shall I add in order to make it possible? I will like the make the category with the latest post move to the first. I have tried for 5 hours and still failed to do it. Please teach me how to fix it.
<?php
//Get the desired categories and order by ID
$cat_args = array(
'orderby' => 'id'
);
//For each category show a random post
$categories = get_categories($cat_args);
foreach ($categories as $category) {
?>
<?php
$post_args = array(
'numberposts' => 1,
'category' => $category->term_id,
);
$posts = get_posts($post_args);
foreach ($posts as $post) {
?>
<article <?php post_class('post-list animated fadeIn'); ?> role="article">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<figure class="eyecatch<?php if (!has_post_thumbnail()) : ?> noimg<?php endif; ?>">
<?php the_post_thumbnail('home-thum'); ?>
<?php archivecatname(); ?>
</figure>
<section class="entry-content cf">
<h1 class="h2 entry-title"><?php the_title(); ?></h1>
<div class="byline entry-meta vcard">
<?php if (get_option('post_options_authordisplay', 'author_off') == 'author_on') : ?><span class="writer name author"><?php echo get_avatar(get_the_author_meta('ID'), 30); ?><span class="fn"><?php the_author(); ?></span></span><?php endif; ?>
</div>
<div class="description"><?php the_excerpt(); ?></div>
</section>
</a>
</article>
<?php get_template_part('loop'); ?>
<?php
}
}
?>
Query Arguments
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Running the Query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
I'm trying to add pagination to my [shortcut]. This is my code:
<?php }
//LatestCar Function
function carforyou_LatestCar($atts){
ob_start();?>
<div class="row">
<?php
extract( shortcode_atts(array('show' =>''), $atts ));
extract( shortcode_atts(array('brand' =>''), $atts ));
$loop = new WP_Query( array(
'post_type' => 'auto',
'auto-brand' => $brand,
'posts_per_page'=>$show,
'offset' => 0
));// $count = $loop->found_posts; echo $count;
while ($loop->have_posts()) : $loop->the_post();
global $post; ?>
<div class="col-list-3">
<div class="featured-car-list">
<div class="featured-car-img">
<a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
<?php if(has_post_thumbnail()):
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif;
?>
</a>
<?php carforyou_AutoType(); ?>
<div class="compare_item">
<div class="checkbox">
<button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
</div>
</div>
</div>
<div class="featured-car-content">
<h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
<div class="price_info">
<?php if(!empty($post->DREAM_auto_price)): ?>
<p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
<?php endif; ?>
<div class="car-location">
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?>
<span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
<?php endif; ?>
</div>
</div>
<ul>
<?php carforyou_featuredList(); ?>
</ul>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
<?php }
Above code using [latestcars show="10" brand="ford"] displays 10 cars made by ford. I want to add pagination, so I have added folllwing modification:
<?php }
//LatestCar Function
function carforyou_LatestCar($atts){
ob_start();?>
<div class="row">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
extract( shortcode_atts(array('show' =>''), $atts ));
extract( shortcode_atts(array('brand' =>''), $atts ));
$loop = new WP_Query( array(
'post_type' => 'auto',
'auto-brand' => $brand,
'paged' =>$paged,
'posts_per_page'=>$show,
'offset' => 0
));
// $count = $loop->found_posts; echo $count;
while ($loop->have_posts()) : $loop->the_post();
global $paged, $post;
?>
<div class="col-list-3">
<div class="featured-car-list">
<div class="featured-car-img">
<a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
<?php if(has_post_thumbnail()):
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif;
?>
</a>
<?php carforyou_AutoType(); ?>
<div class="compare_item">
<div class="checkbox">
<button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
</div>
</div>
</div>
<div class="featured-car-content">
<h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
<div class="price_info">
<?php if(!empty($post->DREAM_auto_price)): ?>
<p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
<?php endif; ?>
<div class="car-location">
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?>
<span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
<?php endif; ?>
</div>
</div>
<ul>
<?php carforyou_featuredList(); ?>
</ul>
</div>
</div>
</div>
<?php endwhile;
carforyou_pagination();
else:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
extract( shortcode_atts(array('show' =>''), $atts ));
extract( shortcode_atts(array('brand' =>''), $atts ));
$loop = new WP_Query( array(
'post_type' => 'auto',
'auto-brand' => $brand,
'paged' =>$paged,
'posts_per_page'=>$show,
'offset' => 0
));
while ($loop->have_posts()) : $loop->the_post();
?>
<div class="col-list-3">
<div class="featured-car-list">
<div class="featured-car-img">
<a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
<?php if(has_post_thumbnail()):
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif;
?>
</a>
<?php carforyou_AutoType(); ?>
<div class="compare_item">
<div class="checkbox">
<button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
</div>
</div>
</div>
<div class="featured-car-content">
<h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
<div class="price_info">
<?php if(!empty($post->DREAM_auto_price)): ?>
<p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
<?php endif; ?>
<div class="car-location">
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?>
<span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
<?php endif; ?>
</div>
</div>
<ul>
<?php carforyou_featuredList(); ?>
</ul>
</div>
</div>
</div>
<?php endwhile;
carforyou_pagination();
endif;
}
?>
Above solution returns following error code which I can't trace:
PHP Parse error: syntax error, unexpected
'else' (T_ELSE) in
/var/www/clients/clientxxx/webxxx/web/wp-content/themes/xxxxxx/functions/basic-functions.php
on line 439
Line 439 is "else:"
<?php endwhile;
carforyou_pagination();
else:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
(...)
Any advice? Thanks
Strip out the HTML tags and it becomes pretty clear that your if...else control structure is not correct. The else in question has no corresponding if structure before it.
while ($loop->have_posts()) : $loop->the_post();
global $paged, $post;?>
<?php if(has_post_thumbnail()): //OPEN IF ONE
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif; //CLOSE IF ONE
?>
<?php carforyou_AutoType(); ?>
<?php if(!empty($post->DREAM_auto_price)): ?> //OPEN IF TWO
<?php endif; ?> //CLOSE IF TWO
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?> //OPEN IF THREE
<?php endif; ?> //CLOSE IF THREE
<?php carforyou_featuredList(); ?>
<?php endwhile;
carforyou_pagination();
else: //SO WHERE IS IF FOUR???
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
extract( shortcode_atts(array('show' =>''), $atts ));
...
How to add post order to this function? I would like to display most recent post from newest to oldest:
<?php
//Establish first post check variable
$first_post = true;
query_posts('category_name=blog&showposts=3');
while (have_posts()) : the_post(); ?>
<li>
<a href="<?php echo get_permalink($post->ID); ?>">
<p class="news_title"><?php $title = get_the_title(); echo wp_trim_words( $title , '4', $more = null ); ?></p></a>
<?php if($first_post) { ?>
<div class="post_skrot"><?php echo wp_trim_words( get_the_content(), $num_words = 8, $more = '... <a class="button_more" href="'. get_permalink($post->ID) . '">show more >> </a>' ); ?></div>
<?php } else { ?>
<div class="post_skrot"><a class="button_more" href="'.get_permalink($post->ID).'">show more>> </a></div>
<?php } ?>
</li>
<?php
//Change value of $first_post
$first_post = false;
endwhile;
wp_reset_query(); ?>
I try add
&orderby=date&order=ASC
but it doesn't work.
Whole code:
<div id="sliders-2-3">
<div class="elementy-oferty">
<div class="slider-4">
<div class="border-video">
<div class="content-slider-4">
<div class="blog_top_title">Blog</div>
<ul>
<?php
//Establish first post check variable
$first_post = true;
$args = array(
'posts_per_page' => 3,
'cat' => 317,
'orderby' => 'DESC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog_post_sekcja">
<a href="<?php echo get_permalink($post->ID); ?>">
<p class="news_title"><?php $title = get_the_title(); echo wp_trim_words( $title , '4', $more = null ); ?></p></a>
<?php if($first_post) { ?>
<div class="post_skrot"><?php echo wp_trim_words( get_the_content(), $num_words = 30, $more = '... <a class="button_more" href="'. get_permalink($post->ID) . '">pokaż więcej » </a>' ); ?></div>
<?php } else { ?>
<div class="post_skrot"><a class="button_more" href="<?php echo get_permalink($post->ID); ?>">pokaż więcej » </a></div>
<?php } ?>
</div>
<?php
//Change value of $first_post
$first_post = false;
endwhile;
wp_reset_query(); ?>
</ul>
<div class="blog_more">
Więcej wpisów</div>
</div>
</div>
</div>
I've got: Parse error: syntax error, unexpected end of file...on line 145. 145 is my last line in code and on this line I have only <?php get_footer(); ?>
I think it must be something with endwhile; or another syntax.
Your $args array is off a little bit. Should be:
<div id="sliders-2-3">
<div class="elementy-oferty">
<div class="slider-4">
<div class="border-video">
<div class="content-slider-4">
<div class="blog_top_title">Blog</div>
<ul>
<?php
//Establish first post check variable
$first_post = true;
$args = array(
'posts_per_page' => 3,
'cat' => 317,
'orderby' => 'date',
'order' => 'DESC' // or 'ASC like in your original code.'
);
$the_query = new WP_Query( $args );
if ( have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<div class="blog_post_sekcja">
<a href="<?php echo get_permalink($post->ID); ?>">
<p class="news_title"><?php $title = get_the_title(); echo wp_trim_words( $title , '4', $more = null ); ?></p></a>
<?php if ( $first_post ) : ?>
<div class="post_skrot"><?php echo wp_trim_words( get_the_content(), $num_words = 30, $more = '... <a class="button_more" href="'. get_permalink($post->ID) . '">pokaż więcej » </a>' ); ?></div>
<?php else : ?>
<div class="post_skrot"><a class="button_more" href="<?php echo get_permalink($post->ID); ?>">pokaż więcej » </a></div>
<?php endif; ?>
</div>
<?php
$first_post = false;
}
}
wp_reset_postdata();
?>
</ul>
<div class="blog_more">
Więcej wpisów</div>
</div>
</div>
</div>
this is my code
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
foreach($portfolio_categories as $portfolio_category)
echo '<li data-filter=".' .$portfolio_category->slug. '">' .$portfolio_category->name. '</li> ';
?>
this shows the filters created by the category's name inside $portfolio_categories
then, there is the creation of client's list
<ul id="list" class="portfolio_list clearfix responsive">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' ) );
?>
<?php if (have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php $terms = wp_get_object_terms($post->ID, 'portfolio_category'); ?>
<?php $pf_bimg = wp_get_attachment_url( get_post_thumbnail_id() );?>
<?php $pf_simg = aq_resize( $pf_bimg, 420, 450, true ); ?>
<li class="span3 list_item <?php foreach ($terms as $term) { echo $term->slug.' '; } ?>">
<div class="recent-item">
<figure>
<div class="touching medium">
<img src="<?php echo $pf_simg; ?>" alt="<?php the_title(); ?>" />
<!-- <i class="icon-search"></i>
<i class="icon-link"></i> -->
</div>
<figcaption class="item-description">
<h5><?php the_title(); ?></h5>
<span><?php $i = 0; foreach ($terms as $term) { if($i)echo " / "; echo $term->name; $i=1; } ?></span>
</figcaption>
</figure>
</div>
</li>
<?php endwhile; ?> <?php endif; ?>
<?php wp_reset_query();?>
</ul>
What i 'm trying to do is to display a different element for every different category .
I thought to insert an if condition before the tag , like this
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
if ($portfolio_category = 'category1'): ?>
<figure>1</figure>
<?php else: ?>
<figure>2</figure>
<?php endif
?>
Thank all for your time,
Dan
You need to store last category, otherwise you'll have <figure> tag before each row:
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
?>
<?php if ($portfolio_category != $prev_portfolio_category): ?>
<?php if ($portfolio_category = 'category1'): ?>
<figure>1</figure>
<?php else: ?>
<figure>2</figure>
<?php endif; ?>
<?php endif; ?>
<?php
$prev_portfolio_category = $portfolio_category;
?>
I'm trying to style the first post differently than the rest. It's for a "featured" section on index.php
<?php
$count = 0;
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$count++;
if ( $count == 1 ) : ?>
<div class="big">
<div class="details">
<a class="cat" href="#">nike</a>
<p><?php the_title(); ?></p>
<ul class="stats">
<li class="icon-calendar"><?php the_time('m.d.Y') ?></li>
<li class="icon-eye"><?php if(function_exists('the_views')) { the_views(); } ?></li>
</ul>
</div>
<?php echo get_first_inserted_image(); ?>
</div>
<?php else : ?>
<div class="small">
<div class="holder">
<?php echo get_first_inserted_image(); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
the above code does not seem to work as nothing is showing up. I would also like to expand on this to show only 4 random posts from the last 7 days. Any guidance is appreciate it! Thanks!
this got it done
<?php
// WP_Query arguments
$args = array (
'orderby' => 'rand',
'showposts' => 5,
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);
$count = 0;
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$count++;
if ( $count == 1 ) : ?>
<div class="big">
<div class="details">
<a class="cat" href="#">nike</a>
<p><?php the_title(); ?></p>
<ul class="stats">
<li class="icon-calendar"><?php the_time('m.d.Y') ?></li>
<li class="icon-eye"><?php if(function_exists('the_views')) { the_views(); } ?></li>
</ul>
</div>
<?php echo get_first_inserted_image(); ?>
</div>
<?php else : ?>
<div class="small">
<div class="holder">
<?php echo get_first_inserted_image(); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>