Hot answers tagged

15

// get current page we are on. If not set we can assume we are on page 1. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // are we on page one? if(1 == $paged) { //true }


11

if you only want to know that you are on the first page of a paginated page, try: if( !is_paged() ) { //first page of pagination } http://codex.wordpress.org/Function_Reference/is_paged


7

The solution is to not alter the main query in the template. The default query happens before the template is loaded, so querying in the template overwrites that original query, which is a waste of resources. See the example in codex under pre_get_posts for the correct way to alter the default query without pagination issues.


6

should it be.. $paged = (get_query_var('page')) ? get_query_var('page') : 1; WP_Query in codex: Pagination Note: You should set get_query_var( 'page' ); if you want your query to work with pagination. Since Wordpress 3.0.2, you do get_query_var( 'page' ) instead of get_query_var( 'paged' ). The pagination parameter 'paged' for WP_Query() remains the ...


5

twentyeleven_content_nav() uses the main query object, $wp_query. You'll need to use the $wp_query variable, rather than $unfiltered_query, then wp_reset_query() to restore the original $wp_query (which it'll find in $wp_the_query, something you should probably avoid touching directly). As long as you're careful to restore the original query, you're in good ...


5

This is quite an interesting question (which I have upvoted, specially for your approach and research). The big curveball here is the first page of the query: You cannot set the query to return 0 posts on the first page By moving the page content of every page up by one page, you will loose the last page as the query will still only have the same amount of ...


4

You shouldn't need to do any special queries for this. Here is one way to accomplish it /** * conditional check ensures special class only shows on top post on first page. * if you want top post on page 2, etc. to have special class, just set $first_post to true */ if( (int) get_query_var( 'paged' ) > 1 ){ $first_post = false; } else { $...


4

For multi-page posts: The $page global variable returns the current page of a multi-page post. The $numpages global variable returns the total number of pages in a multi-page post. For paginated archive index pages: The $paged global variable returns the current page number of a paginated archive index. To use any of these variables, simply globalize ...


4

Here you go: <?php if ( is_paged() ) echo 'some text'; See http://codex.wordpress.org/Conditional_Tags#A_Paged_Page


4

The rewrite API won't redirect - it just "maps" URL structures to query strings - you'll need to do this yourself. First things first, fix that rewrite rule: 'episode/([^/]+)/transcript'; // Matches anything that follows transcript, not what we want! 'episode/([^/]+)/transcript/?$'; // Matches only "episode/name/transcript", optionally with a trailing ...


3

I was looking for a simple way to determine whether or not to use the posts_nav_link() function and all solutions I found online were either too complex or unreliable. For example, many people suggested using the $paged global variable, but I found that this variable returned the same value for the first page, even when the first page was the only page! So, ...


3

Because you are using <?php $query = new WP_Query ( $args ); ?> the wp_query global isn't changed (used by next_posts_link). So you can change the new WP_Query call to just query_posts( $args ); (and change your loop without the $query->). Or you can overwrite the <?php global wp_query; $wp_query = new WP_Query ( ); $wp_query->query($args); ?...


3

The same, with built-in function: <?php if ( ! get_query_var('paged')) : ?> { your code here } <?php endif; ?>


3

the argument for WP_Query is paged, but the query var is page, no 'd' on the end. 'paged' => get_query_var( 'paged' ) should be: 'paged' => get_query_var( 'page' )


3

I hope you understand what query variables is. If not, here is it in short. The main query uses WP_Query to set itself up. In the main query, WP_Query uses public query variables to construct the main query according to the page being requested, and paged and page are two of them. To see all the public query variables, paste this in your header and check on ...


2

$wp_query->current_post holds information about current post in loop, not global set of posts. Numering posts across multiple pages is not available natively (as far as I know) and probably would be to unreliable (for example what if number of posts per page changes is customized to be uneven?). For specific implementation building custom numbering, ...


2

You don't need the type variable and other bits of custom search query code, or custom functions like the other answers. In your searchbox, instead of submitting the form to / submit it to /my_post_type/ instead! WordPress will handle all the rest for you automagically, with no effort involved. What's more this means all the usual functions will work just ...


2

Not sure if you can use a complete different template but you can use different template parts using the global $page variable. Example: global $page; if ( $page == 2 ) { get_template_part( 'page', 'two' ); } else { get_template_part( 'page', 'default'); } This would load the template file page-two.php or page-default.php depending on ...


2

Use a pre_get_posts action in your functions.php with conditional tags, and remove the call to query_posts: function wpa62751_pre_get_posts( $query ) { if ( is_category( 'my-category' ) && is_main_query() ) $query->set( 'posts_per_page', 4 ); } add_action( 'pre_get_posts', 'wpa62751_pre_get_posts' );


2

you could change the one line to: $first_post = ( !is_paged() ) ? $posts[0]->ID : ''; or use a different approach: if ($wp_query->current_post == 0 && !is_paged() ) { echo '<div class="post top-post-special" id="post-' . get_the_ID() . '">'; } else { echo '<div class="post" id="post-' . get_the_ID() . '">'; }


2

So, all query conditionals (is_paged, is_singular, etc) look something like this: function is_paged() { global $wp_query; if ( ! isset( $wp_query ) ) { _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' ); return false; } return $...


2

Isn't it so that you have exactly 10 posts and you're showing them all on first page. $found_posts - The total number of posts found matching the current query parameters found_posts on your first page shows 10. All of these posts are shown on first page. So there are no posts to show on second page - therefore it's empty.


2

Also from codex.wordpress.com/Pagination (Under "Advanced Troubleshooting Steps" > "Removing query_posts from the main loop") regarding modifying the main query via the pre_get_posts action mentioned by Milo: [...] add the query for your home and category pages back in your theme's functions.php file: function my_post_queries( $query ) { // do not ...


2

You set offset param, so paged param is ignored ;) From Codex: offset (int) - number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged If you want to ignore most recent post in this query, you can achieve it in two ways. 1. Custom pagination. $ppp = get_option('posts_per_page'); // or any other ...


2

I had this exact problem. I just stumbled upon the solution! In case this helps you, in both my initial queries before the array merge I changed the posts_per_page to be -1 like 'posts_per_page' => -1 instead of 10. When I did this, the pagination links suddenly appeared and worked fine. I then added 'posts_per_page' => 10 to my final query after the ...


1

I use this solution in my functions.php /*------ order ------*/ session_start(); add_filter('posts_orderby', 'edit_posts_orderby'); function edit_posts_orderby($orderby_statement) { $seed = $_SESSION["sem"]; if (empty($seed)) { $seed = rand(); $_SESSION["sem"] = $seed; } $orderby_statement = 'RAND('.$...


1

You are doing a few different things wrong-- or at least convoluted and suspect-- in that code and I am sure that that is resulting in your query results getting out of sync with the pagination. You've got a couple of queries in there, including one made by query_posts which you should really never be using at all, plus some filters. Honestly, I didn't crawl ...


1

There are several ways of doing this. You could, for instance, incorporate a counter into your loop and treat the post data with respect to the counter's value. YOUR QUERY HERE $my_counter = 0; while ( have_posts() ) : the_post(); $my_counter++; if ( $my_counter <= 4 ) { SLIDER ACTION } else { REGULAR POST ACTION } endwhile;


1

No. Query conditionals are part of WordPress' object cache, and incur no additional resources when called.


1

Ok, so I got it working more or less correctly, although there is still a small problem left, please read on.. Here is what I've got: in fucntions.php: Rewrite rules add_action('init', 'my_rewrite_add_rewrites'); function my_rewrite_add_rewrites(){ add_rewrite_rule( '^services/([^/]+)/?$', 'index.php?post_type=service&order=ASC&...



Only top voted, non community-wiki answers of a minimum length are eligible