I'm doing a blog with Wordpress and the post pagination doesn't appear. The post list is shown correctly, but there is only 5 post (I have 8) and pagination isn't working.
<?php
if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="all-post">
<?php
$args = array('category' => '');
$myposts = get_posts($args);
foreach ($myposts as $post) : setup_postdata($post); ?>
<div class="post">
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="post-details">
<div class="post-meta">
<span class="date"><?php the_time('j M, Y'); ?></span>
<!-- Categories -->
<span class="categories">
<?php
$category_ids = get_all_category_ids();
?><?php
$args = array('orderby' => 'slug', 'parent' => 0);
$categories = get_categories($args);
foreach ($categories as $category)
{
echo '<a href="' . get_category_link(
$category->term_id
) . '" rel="bookmark" class="category">' . $category->name . '' . $category->description . '</a>';
} ?>
</span>
</div>
<h2 class="post-title">
<?php the_title(); ?>
</h2>
Read More
</div>
</div>
<?php endforeach;
wp_reset_postdata(); ?>
</div>
<?php endwhile; ?>
<?php next_posts_link('Older posts'); ?>
<?php previous_posts_link('Newer posts'); ?>
<?php else: ?>
<?php endif; ?>
You maybe didn't set any posts_per_page value and the default is 5. If you want to modifiy it for the whole website blog/archive pages, modify the posts_per_page value in the Reading Settings page.
In a $args array, modify as folow:
$args = array(
'posts_per_page'=> -1, // unlimited
'category' => ''
);
$posts = get_posts($args);
I'm prudent with my answer (that why I give 2 methods to achieve this), because I don't know with your given code, if the main loop $args is modify.
I hope this could nevertheless help you.
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();
<div class="small-12 columns">
<ul class="category-filters">
<?php
$args = array(
'parent' => 0,
'exclude' => '1, 9'
);
$cats = get_categories($args);
foreach($cats as $cat) {
$output =
'<li>
<input class="checkbox" type="checkbox" value="' . $cat->cat_name . '">'
. $cat->cat_name .
'</li>';
echo $output;
}
?>
</ul>
<h4>Meet our Coaches</h4>
<?php
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));
if ( $wpb_all_query->have_posts() ) : ?>
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div class="callout horizontal word-wrap"
data-category="
<?php
$cats=get_the_category();
echo $cats[0]->cat_name;
?>">
<?php the_post_thumbnail() ?>
<h5><?php the_title(); ?></h5>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
I have the above php. It creates a list of checkboxes and then creates a list on callouts looping through all of the categories in wordpress.
I would like to know how I could modify this code so that it never creates and checkboxes or callouts for the category 'coaching'.
In addition to this question it would be great to know if there is a better way to get my desired outcome through something that wordpress has to offer that I have missed.
I'm trying to loop only custom post types using the WP loop, but only shows the ones I give by ID.
This is my "normal" loop right now:
<?php $args = array(
'post_type' => 'referenties', 'posts_per_page' => 5, 'order' => 'DESC',
); ?>
<?php
$number = 0;
query_posts($args);
if(have_posts()):
?>
<!-- /Carousel script -->
<div class="container">
<div class="carousel-loop">
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<?php while(have_posts()): the_post(); ?>
<li data-target="#myCarousel" data-slide-to="<?php echo $number++; ?>"></li>
<?php endwhile; ?>
</ol>
<div class="controle-buttons">
<a class="carousel-control left" href="#myCarousel" data-slide="prev"><i class="fa fa-chevron-circle-left"></i></a>
<a class="carousel-control right" href="#myCarousel" data-slide="next"><i class="fa fa-chevron-circle-right"></i></a>
</div>
<!-- Carousel items -->
<div class="carousel-inner">
<?php while(have_posts()): the_post(); ?>
<!-- Carousel nav -->
<div class="item">
<div class="col-sm-2">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php the_post_thumbnail(array(150,150)); // Declare pixel size you need inside the array ?>
</div>
<div class="col-sm-4">
<h4><?php the_title(); ?></h4>
<?php $bedrijf = get_field('naam_bedrijf'); ?>
<?php $feest = get_field('feest'); ?>
<?php $link = get_field('mylink'); ?>
<?php echo '<p>Bedrijfsnaam: ' . $bedrijf . '</p>'; ?>
<?php $post_object = get_field('mylink');
if( $post_object ): $post = $post_object; setup_postdata( $post ); ?>
<p>Feest type: <a style="color:#ff6600" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endif; ?>
</div><?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<div class="col-sm-4 col-sm-offset-1">
<h4>Opmerking</h4>
<p><?php echo custom_field_excerpt_longer(); ?></p>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php echo '<p>' . wp_review_show_total() . '</p>'; ?>
</div>
<?php endif; ?>
</div>
<?php endwhile; ?>
But I only want to show post ID's: 2706, 2462, 2514, 2511 and 2505.
The loop is shown in a carousel, this works fine. But I just want the ID's to show and not all posts added.
Use something like this:
<?php
$args = array(
'post_type' => 'referenties',
'post__in' => array(2706, 2462, 2514, 2511, 2505),
'order' => 'DESC',
);
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//post content output goes here
}
// Restore original Post Data
wp_reset_postdata();
} else {
// no posts found
}
The post__in () argument uses array with desired post ids to retrieve.
Don't use query_posts for custom queries. Too much that can go wrong.
Hope this helps :)
I've been building a WordPress template based on inuitcss to get a responsive page.
In this template, I'm attempting to get the WP posts from a certain category, and divide them over a predefined amount of columns. To achieve this I'm counting the amount of posts from category and compute the necessary data for the posts to split up evenly.
I've been using this (last on the page) example and updated several deprecated functions.
The issue I am facing at the moment is that all columns are loading the same posts, and they are only loading the last two instead of all available posts in the category.
The code below is what I am using at the moment, the result can be found at http://www.alikivanderkruijs.com/wp (click on 'Projects').
I've been sitting on this for a while and can't seem to get it right. I hope someone can help me out!
<?php
$proj_posts = get_posts('cat=15');
$count_proj_posts = count($proj_posts);
$split_proj_posts = round($count_proj_posts/2);
?>
<div class="gw">
<div id="menuwrap">
<h3 class="work">PROJECTS</h3>
<div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php query_posts('cat=15&showposts=' . $split_proj_posts . ''); ?>
<?php $posts = get_posts('posts_per_page=' . $split_proj_posts . '&offset=0'); foreach ($posts as $post) : the_post(); ?>
<?php static $proj_count1 = 0; if ($proj_count1 == $split_proj_posts) { break; } else { ?>
<div <?php post_class(); ?>>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<?php $proj_count1++; } ?>
<?php endforeach; ?>
</div>
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php rewind_posts(); ?>
<?php query_posts('cat=15&showposts=' . $split_proj_posts . ''); ?>
<?php $posts = get_posts('posts_per_page=' . $split_proj_posts . '&offset=' . $split_proj_posts . ''); foreach ($posts as $post) : the_post(); ?>
<?php static $proj_count2 = 0; if ($proj_count2 == $split_proj_posts) { break; } else { ?>
<div <?php post_class(); ?>>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<?php $proj_count2++; } ?>
<?php endforeach; ?>
</div>
</div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
</div>
You are overcomplicating it, simply get all the posts and count up until half, then echo out the closing and opening tags to create a new row:
<div class="gw">
<div id="menuwrap">
<h3 class="work">PROJECTS</h3>
<div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php
$args = array(
'numberposts' => -1,
'posts_per_page' => '',
'offset' => 0,
'category' => '15',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true);
$posts_array = get_posts($args);
$split_at = round(count($posts_array)/2)-1;
$count = 0;
foreach ($posts_array as $post_object):setup_postdata($post);
?>
<div <?php post_class(); ?>>
<h4><a href="<?php the_permalink() ?>" rel="bookmark"
title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h4>
<?php the_content(); ?>
</div>
<?php
//IF we are halfway through, close 1st row and
start a new one
if ($count == $split_at) {
?>
</div>
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php
}
$count++; ?>
<?
endforeach;
?>
</div>
</div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
</div>
</div>
I have left the full args array, incase it is usefull to others, but you only need to use the cat argument in your case.
EDIT ok, setup_postdata is flawed, just checked the docs and it doesnt work as expected. The suggested workaround is to set the global post object like so:
setup_postdata( $GLOBALS['post'] =& $post_object )
Change that section should fix that issue. The count problem is still weird though.
I have created two loops with pagination (first loop loops through CAT'S category and second loops through DOG'S category), but now I am stuck:(
The problem: After I click "Next entry" on my site (CAT'S category) it goes to second entry in that category BUT it also goes to my DOG'S category second entry (I don't want THAT!! ). It also happens vice versa...
What I like to do is this: I click on "Next Entry" on my CAT'S category and it goes only to next post in THAT category (CAT'S) but NOT to second post in my DOG'S category, or another way around: I click on "Next Entry" on my DOG'S category and it goes only to next post in THAT category (DOG'S) but NOT to second post in my CAT'S category .
Can someone help me please? I have asked for help on
wordpress.stackexchange.com a while ago but I didn't get any answer so I am asking question here.
Index php looks like this:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<div id="blog">
<?php
$args = array(
'category_name' => 'cats'
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_query($args . '&paged=' . $paged . '&cat=-3');
while( $the_query -> have_posts()) : $the_query -> the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_postdata();?>
<div class="navigation">
<div style="float:left;" class="alignleft"><?php previous_posts_link('« Previous Entries') ?></div>
<div style="float:right;" class="alignright"><?php next_posts_link('Next Entries »',$the_query->max_num_pages) ?></div>
</div>
</div>
<div id="blogs">
<?php
$args = array(
'category_name' => 'dogs'
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_query($args . '&paged=' . $paged . '&cat=-10');
while( $the_query -> have_posts()) : $the_query -> the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_postdata();?>
<div class="navigation">
<div style="float:left;" class="alignleft"><?php previous_posts_link('« Previous Entries') ?></div>
<div style="float:right;" class="alignright"><?php next_posts_link('Next Entries »',$the_query->max_num_pages) ?></div>
</div>
</div>
<?php get_footer(); ?>
You need 2 different paging values so add some new ones and rewrite rules which look for them (they're really just to make the urls neater looking). The rewrite rules and the pagination link format mean you can page through one category while the other category page doesn't change.
In functions.php:
function add_new_rules()
{
// new 'paged' variables
global $wp;
$wp->add_query_var('paged_cats');
$wp->add_query_var('paged_dogs');
// rewrite rules
add_rewrite_rule('page/cats/(\d+)/dogs/(\d+)', 'index.php?paged_cats=$matches[1]&paged_dogs=$matches[2]', 'top');
add_rewrite_rule('page/cats/(\d+)/dogs/?$', 'index.php?paged_cats=$matches[1]&paged_dogs=1', 'top');
if( !array_key_exists('page/cats/(\d+)/dogs/(\d+)', (array)get_option('rewrite_rules')) )
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
add_filter('init', 'add_new_rules');
Check for the new query vars in index.php and use them for each WP_Query and the related pagination links.
<div id="blog">
<?php
$paged_cats = (get_query_var('paged_cats')) ? get_query_var('paged_cats') : 1;
$paged_dogs = (get_query_var('paged_dogs')) ? get_query_var('paged_dogs') : 1;
$cats = new WP_query(array(
'category_name' => 'cats',
'paged' => $paged_cats,
'posts_per_page' => 1
));
while( $cats->have_posts() ) : $cats->the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php if ( $cats->max_num_pages > 1 ) : ?>
<div class="navigation">
<?php
echo paginate_links(array(
'base' => home_url("page/cats/%#%/dogs/{$paged_dogs}"),
'format' => '%#%',
'current' => $paged_cats,
'total' => $cats->max_num_pages,
));
?>
</div>
<?php endif; ?>
</div>
<hr>
<div id="blogs">
<?php
$dogs = new WP_query(array(
'category_name' => 'dogs',
'paged' => $paged_dogs,
'posts_per_page' => 1
));
while( $dogs->have_posts() ) : $dogs->the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_postdata();?>
<?php if ( $dogs->max_num_pages > 1 ) : ?>
<div class="navigation">
<?php
echo paginate_links(array(
'base' => home_url("page/cats/{$paged_cats}/dogs/%_%"),
'format' => '%#%',
'current' => $paged_dogs,
'total' => $dogs->max_num_pages,
));
?>
</div>
<?php endif; ?>
</div>
Your problem is this:
On pagination wordpress sends page id or count to get the next enteries and the name of the variable that goes in postback is same for both lists. When it goes to the server both lists get a request to go to next page by looking at the post variables.
The solution mentioned by jho1086 is to create paged variable as a custom variable for both lists and assign it. this would then send a different variable for each list and you can move next or previous as you wish.
You need to do both add a paged variable and add it to your pagination as well. In jho1086's solutuin see $args1 and $pag_args1 both have reference to $paged2 to make this happen.
When you select page 2 for cats it should send a catpage=2 to the server
When you select page 2 for Docs it should send a dogpage=2 to the server
If you can solve this for pagination links and pass the args then you can do following
When server gets the list of cats use catpage as a paging param
When server gets the list of dogs use dogpage as a paging param
This is in theory and will surely work. You can test variables going in and out of the request via firebug and comeback with what other issues you have but the reason your loops go to page 2 for clicking on one is that both loops are paging on the same post variable.
I find answer here https://wordpress.stackexchange.com/questions/47259/multiple-wp-query-loops-with-pagination, the choice answer is working with me. It use format.
<!-- Cats -->
<div class="animals">
<?php
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
// Custom Loop with Pagination 1
// http://codex.wordpress.org/Class_Reference/WP_Query#Usage
$args1 = array(
'paged' => $paged1,
'posts_per_page' => 2,
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post();
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';
endwhile;
// http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $query1->max_num_pages,
'add_args' => array( 'paged2' => $paged2 )
);
echo paginate_links( $pag_args1 );
?>
</div>
<!-- Dogs -->
<div class="animals">
<?php
// Custom Loop with Pagination 2
$args2 = array(
'paged' => $paged2,
'posts_per_page' => 2,
);
$query2 = new WP_Query( $args2 );
while ( $query2->have_posts() ) : $query2->the_post();
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';
endwhile;
$pag_args2 = array(
'format' => '?paged2=%#%',
'current' => $paged2,
'total' => $query2->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args2 );
?>
</div>
And since it is generating a un-clean url you can add a rel="nofollow" for SEO purposes. Here is the instruction how to do add rel="nofollow"
the functions next_post_link() and previous_post_link() has a third argument called 'in_same_cat' - You will need to set it to TRUE.
Read the codex page (click on the functions name in the answer .
From codex :
<?php next_post_link('%link', 'Next post in category', TRUE); ?>
They even have a fourth argument 'excluded_categories' which you can also use for achieving the same thing, or combining both to get even more sophisticated results.