Reputation: 2260
I have the wp-navi plug in working smoothly on any category that uses the global reading config (post per page "3"), but i have one category that i only need to show 1 per page, setting this value manually outside the loop destroys my paged links, it generates a lot of pages (instead of two, since i only have two posts) and they take to the home ( i guess is some sort of 404), i've tried with as many solutions i could find in stackoverflow and google, and no luck yet, i've been using wp for a couple weeks so im not sure if im doing something really stupid here, so please help me out.
Here is the code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//query 1 post
query_posts("posts_per_page=1&paged=$paged");
?>
<?php while(have_posts()) : the_post(); ?>
<div class="entry">
<div class="single_entry">
<p>
<?php the_content(); ?>
<div class="clear"></div>
</p>
<?=get_social(); ?>
<div class="clear"></div>
</div>
</div>
<?php endwhile; ?>
<?php if(function_exists('wp_pagenavi')) { ?>
<div id="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php } ?>
Thanks in advance =).
EDIT:
I found something interesting, if i set the &paged=2
it actually shows the second page, and the page-navi says "page 2 of 2" but only if the url stays like this "mysite.com/category/" if i add "/page/2/" to the end, it redirects me to the broken home page. So even if the pagenavi works, the "/page/#/" is breaking it, of course the get_query_var('paged')
or get_query_var('page')
will return its default "1" since i'm not using the "/page/" structure, maybe i could do a fix by adding a ?p=# when i click in each number of the pagenavi, not quite sure how to do that in the plugin file, so i hope you guys could help me out based on what i'm saying now, if not, well i'll try to see how i fix this the ugly way. Thanks in advance again.
Upvotes: 0
Views: 8735
Reputation: 2136
I also encountered this weird problem of pagination.I dont know if its because of the latest version of wp but I tried different solutions and only this tweak worked for me. Change your get_query_var('paged') to get_query_var('page'). Try this
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1; //notice this
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=1&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="entry">
<div class="single_entry">
<p>
<?php the_content(); ?>
<div class="clear"></div>
</p>
<?=get_social(); ?>
<div class="clear"></div>
</div><!-- fin expert -->
</div><!-- fin entry -->
<?php endwhile; ?>
<?php if(function_exists('wp_pagenavi')) { ?>
<div id="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php } ?>
<?php $wp_query = null; $wp_query = $temp; ?>
NOTE: I also noticed that you dont have closing bracket in your if(function_exists('wp_pagenavi')) { statement. Correct this as well.
Upvotes: 0
Reputation: 364
You should use WP_Query class to build query. And use this object as variable in wp_pagenavi().
So your code should look like.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query( $args );
$the_query->query('showposts=1&paged='.$paged);
?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="entry">
<div class="single_entry">
<p>
<?php the_content(); ?>
<div class="clear"></div>
</p>
<?=get_social(); ?>
<div class="clear"></div>
</div><!-- fin expert -->
</div><!-- fin entry -->
<?php endwhile; ?>
<?php if(function_exists('wp_pagenavi')) { ?>
<div id="pagination">
<?php wp_pagenavi(array( 'query' => $the_query )); ?>
</div>
<?php } ?>
Upvotes: 0
Reputation: 2260
Ok i fixed this issue using js/jq i would rather find a fix using the wp codex but everything i've tried has been useless and i need it to solve this no matter how, besides i'm not sure how these permalinks work internally, and editing the pluging was kinda disastrous, adding a "&p=2" to the url was making the same damage as the "/page/2" so i created a script with jq to remove the links from the pagenavi, get their target page, adding an onclick to each one and passing the target page number as an argument, to later replace a hidden value on a form and sending the form, then retrieving the page number and adding it to the query_posts, it works like a charm, but i wish i knew how to do a proper fix in wp, or if it is a bug i think i must submit a ticket. Thanks for your help, if anyone have more ideas are welcome, if somebody have the same issue feel free to use this fix.
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" id="change_page">
<input name="target_page" type="hidden" value="1" id="target_page" />
</form>
<script type="text/javascript">
function go_to_page(target_page){
var page = target_page;
$("#target_page").val(page);
$('#change_page').submit()
}
$(".wp-pagenavi").find("a").each(function(){
var page = $(this).attr("href");
page = page.slice(-3);
page = page.replace("/","");
page = page.replace("/","");
// The if below, only apllies to the first link, since it doesn't use "/page/#" structure.
if(isNaN(page)) { page = 1; }
$(this).attr("href","javascript:void(0)");
$(this).attr("OnClick","go_to_page("+page+")");
console.log(page);
});
</script>
And on top of the page i use this:
if(isset($_POST["target_page"])) {
$page = $_POST["target_page"];
} else {
$page = 1;
}
Upvotes: 1
Reputation: 5041
Try this to get one post from one category, and keeping it paged:
query_posts( "category_name=cat_slug_here&posts_per_page=1&paged=$paged");
Upvotes: 0