WordPress custom query pagination - wordpress

I have a WordPress site, where on the main page I list the content from more categories.
My question is, is there a plugin where I can paginate the results from a category? I mean something like $this->plugin_paginate('category_id'); or smth?
Best Regards,

If you're using the standard Wordpress loop, even with a query_posts for a category, pagination is automatic with the usual posts_nav_link. Are you trying to paginate for more than one query and more than one category on the same page?
Edit 11/20: I use this in several different places on one page to show the latest post in a category:
<?php
$my_query = new WP_Query('category_name=mycategory&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
That link then goes to a category page that paginates for that category: Category Templates « WordPress Codex
I don't know how to paginate different categories on the same page. Must be possible. Maybe ask in the Wordpress forums.

This sounds like something that a simple, well-formed query_posts() call can do. I doubt that you'll even need to rely on a plugin. :)
I'm going to assume that you're familiar with the query_posts() function, so let's go ahead and use this example as a base:
// let's get the first 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
Now, to get the 11th to 20th posts from category 3 (that is, the NEXT 10 posts), we'll want to use the [offset] parameter of query_posts():
// let's get the next 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3&offset=10');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
For most purposes, that should be sufficient. However, you did mention that you plan on paginating category post lists from the main page alone? I'm assuming that you mean that you have multiple category post lists on your main page, and all these are paginated independently.
With something like that, it looks like you'll have to work a bit with Javascript to do the work for you, along with what I illustrated above.

I believe you could do something like this:
<?php
if(isset($_GET['paged'])){
$page = $_GET['paged']-1;
}else{
$page = 0;
}
$postsPerPage = 5;
$theOffset = $page*$postsPerPage;
?>
<?php query_posts(array('posts_per_page' => $postsPerPage, 'cat' => CATEGORIES HERE, 'offset' => $theOffset)); ?>

Hope this will help you :)
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'paged' => $page,
);
query_posts($args);?>
?>

Related

How can I add some posts in the homepage and have a blog page as well?

I am building my own - custom - theme in WP.
In my index.php, I have many static contents ( Don't know how to convert them into dynamic fields in WP yet, but I will learn this later ).
The issue is that I want to display a number of posts in the homepage 2 posts and below them a link to redirect the user to a blog page which displays the whole posts.
I searched a lot and I found a solution which is not doing what I want, it was about setting a static front-page and set the blog page as the Posts page under settings then reading.
After doing so, the whole static content - that was in my index.php doesn't appear on the homepage anymore and the homepage becomes an empty page and the whole content went to the blog page, which made me CRAZY!!
All I want to do; is to keep my static content and 2 of my most recent posts in the homepage and create a blog page which contains the whole posts.
Hope to find a solution for this Without Plugins.
Add This Snippet Code into your front-page.php or wherever you want to show 2 posts recently published.And Also a Blog Page Link to navigate the user to all blog posts..
$the_query = new WP_Query( array( 'post_type' => 'post',
'posts_per_page' => 2,
'orderby' => 'publish_date',
'order' => 'DESC'
)
);
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_excerpt();
the_author();
// add whatever you want
endwhile;
wp_reset_postdata();
echo 'See All Blog Posts Here : <a href='.site_url("blog").'>All Blog Posts</a>';
else :
esc_html_e( 'Sorry, no posts yet published.' );
endif;
?>
This is going to be a detailed answer to my problem to help anyone who might face the same problem, it will cover everything in steps for a WordPress newbie, like me! :D
As mentioned in my Question, my problem is that I want to display 2 posts in my index.php and make a separate blog page contains the whole posts.
The Most Detailed Solution:
1) Go to your index.php and use the normal posts loop:
<?php
// The Loop
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
2) From WP Dashboard, go to Settings then Reading and change the Blog pages show at most value to 2 posts Or whatever number of posts you want.
Now, we've done the first step and show only a specific number of posts in our index.php, let's continue the steps to display the whole posts in our blog page.
3) Create a PHP Page called page-blog.php.
Notice: if you don't have a page.php you have to create it, for more information about how to create a custom PHP Page, I recommend #Adam's answer here.
4) Go to your WP Dashboard and create a new page called blog.
5) In your page-blog.php you need to add this snippet:
// Posts Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 6, 'paged' => $paged );
query_posts($args);
// The Loop
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} else {
echo "Sorry, There is no posts to be displayed";
} // end if
?>
Notice: We used the same loop as we did before in index.php, only the new 2 lines of codes which above the loop is the new different thing here in our page-blog.php page
6) I set the posts_per_page to 6 because it is my blog page and I want to display more posts there. Set its value to whatever number you want, it depends on you
7) BOOM, it works and you did what you wanted, just like me!!
This problem seemed VERY COMPLICATED for me, but when I searched and read more information about this, I found that it is a VERY EASY to solve problem, and I hope this answer help you if you faced it!

Wordpress category

This is code from my wordpress homepage template. There are 3 same code, like this one bellow. The question is how can I put different categories to show different news when I post. At the moment code is same on my homepage and it show only category named World, it's the first category created. How can I setup the rest of code with different categories.
Currently it's like this : http://zaslike.com/files/os8hvxg38oendfwcvrz6.png
I need to put other categories instead same three.
http://justpaste.it/e0nh - Here is the code of homepage template.
I believe you are asking for this:
<!-- Your Query -->
<?php // The Query
query_posts( array ( 'category_name' => 'change-to-category-name', 'posts_per_page' => 1 ) ); ?>
<?php // The Loop
while ( have_posts() ) : the_post();?>
// YOUR HTML GOES HERE
<?php endwhile; ?>
<?php // Reset Query
wp_reset_query(); ?> // DON'T FORGET THIS
Use this code at the parts of the site that display your categories change the categories name to the ones you want to display and you are good to go.
Good luck welcome to SO.

Wordpress - Two loops on author.php?

Normally if I was doing more than one loop on a page, aside from the main wordpress loop, I'd just use wp_query, however when the author.php template is being used I can't see how I could use that, since I'd have to pass some args.
Pulling posts from categories or by date etc is all easy enough with wp_query, even getting posts by author ID can be done, but it needs to be generic, ie get posts from the current authors page.
Now, using the same loop as my category page I can easily generate posts on the author.php, but I need a second loop and I just can't figure out how to do it.
First loop would be pulling one random post and it's featured image, second would be getting the archive of that authors posts.
Any ideas?
Does this give you a list of the author's posts? If so, it should be easy to modify the arguments for WP_Query to get exactly what you want.
See here for all the options: http://codex.wordpress.org/Class_Reference/WP_Query
<?php
$new_loop = new WP_Query( array(
'post_type' => 'post',
'author' => get_the_author_meta('ID')
) );
?>
<?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; else: ?>
Sorry, nothing was found.
<?php endif; ?>
<?php wp_reset_query(); ?>

how do I make a list of my postings on Wordpress 3.4.2. and theme Twenty Ten

I've tried to find a solution to allow me to make an "Index" of postings I have published (349 posts, 27 pages, 28 categories and 50 tags).
Is there a simple solution allowing me to display, as a page or post, an "index" of ALL posts, pages, etc. with WordPress?
An "Index Page" where readers and/or subscribers can click on a post/page and be taken to that particular posting.
I've tried various plugins - (Index Press), (Table of Contents Plus) to mention just a couple but, unless I'm doing something wrong, nothing seems to work for me.
<?php wp_get_archives('type=alpha'); ?>
This will list all posts in alphabetical order.
More info can be found in the Codex here: wp_get_archives
WP Query is your best friend.
This will give you a list of all posts and pages in the form of links. If you have any additional post types, add the slug to the post_type array.
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => array('post', 'page')
);
$q = new WP_Query($args);
if($q->have_posts()) : while($q->have_posts()) : $q->the_post();
?>
<?php the_title(); ?><br/>
<?php
endwhile;endif;
?>

How do you display a random post from your database of content in a wordpress 3.0 theme?

I am developing a webpage at http://www.knowledgenation.us and currently I have roughly 500 posts on the page. I believe that is far too many posts to expect someone to read through but I do believe that my page has return value. I want people to return to the webpage on a regular basis and always get something new from the site.
That being said, I would like to post three random posts from my database to the body of the web page that is the theme that I have. I would also like to know how to make that code modular so that I can reuse it for a new incarnation of this website that is going to pull in content from RSS feeds from two websites that friends of mine are developing.
That all being said, bottom line, how do you post random posts to a website, what would the code look like and please be kind in explaining because I am quite new to PHP programming and would not understand most of what the code is about. I just recently got a http://www.lynda.com account and am going to be learning all about PHP but for right now I understand little.
I thank you in advance for helping me with this.
When you query your posts, you can pass on query attributes such as a category, included/excluded post ids, limits and offsets, etc.. You can also define how your results will be ordered — by which field and into which direction (ASC/DESC).
The order_by parameter can be a regular field names like title or date, and also rand as in random to fetch random posts.
Here's an example to use outside the loop, fetching five random posts:
<ul>
<?php
$args = array('numberposts' => 5, 'orderby' => 'rand');
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
And another example for a regular loop:
<?php
$args = array('numberposts' => 5, 'orderby' => 'rand');
query_posts($args);
while (have_posts()) : the_post();
the_content('Read the full post »');
// And so forth…
endwhile;
?>
Hope you get the picture…

Resources