wp_pagenavi wont work using custom post per page - wordpress

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.

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;
}

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");

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 } ?>

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.

Related

Wordpress the_content / post is empty

I'm building my own template for Wordpress and I'm bumping into an issue when displaying the content of a post.
I have already built one page template for the home page and it works fine. The loop outputs what I want to display. Now I'm building the template to display an article but the loop doesn't return anything.
Here is the code of the page template:
<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header();
?>
<div class="wrapper">
<div class="sidebar">
<?PHP get_sidebar(); ?>
</div>
<div class="main">
<div class="section group">
<div class="col col12-12">
<span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</span>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
This results in displaying the "sorry, no posts matched your criteria" message when i click on one of the links on the homepage. The strange thing is that the page really exists (it has ID=26 as you'll see here below):
<?php
$post = get_post(26);
$title = $post->post_title;
echo $title;
?>
This works and displays the expected title. I have tried get_the_ID(); to get the post ID but it returns an empty variable.
There is probably something missing in my template but I can't figure out what.
Any idea?
Thanks
I have found the issue and it had nothing to do with the template itself.
I discovered what's wrong by using one of the standard Wordpress themes (twentyfifteen) where every post led me to a 404 even if I clicked on a post from the Admin UI. I swapped back permalink structure to the ?p=123 option and there everything working. Definitely a permalink structure problem.
The problem was coming from the polylang plug-in. I'm using a network of wordpress site for which I have made this plug-in available. I did not need this plug-in for this particular site but somehow it needed to be active anyway. So I activated polylang and configured it to have only one language and now it works.
Got new gray hair in the process but if that can help anyone...
Thanks for your help!
Laurent
I don't think, that this template can output any posts like this. For WordPress
/*
Template Name: PAGE
*/
indicates: this is a page-template which you can asign to the content of a page but not to a post.
When you want to show a specific post on such a page you will have to:
Asign the template to the page in WP-backend
Add a new query to the template to show the post
Like:
<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header();
?>
// The new Query
$args = array (
'p' => '26',
);
// The Query
$query = new WP_Query( $args );
<div class="wrapper">
<div class="sidebar">
<?PHP get_sidebar(); ?>
</div>
<div class="main">
<div class="section group">
<div class="col col12-12">
<span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</span>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
But as this is very complicated I would recommend to:
delete the /* Template Name: PAGE */ from the template
save it as single.php or more specific as single-26.php
link to the desired post in your wp-menu

Wordpress Conditional page types such as is_home,is_front_page, is_single not working

I have been trying to use several conditional statements using is_home/is_front_page or is_single and they are not working in my theme. Below is an example on such code:
<?php if(is_front_page() || is_home() ) : ?>
<?php if(function_exists('show_flexslider_rotator'))
echo show_flexslider_rotator('homepage_fexslider' ); ?>
This loads my Flexslider, but when I go to page 2, the slider remains on the page.
I have used these statements successfully in other themes.
Do I need to add support for this feature in my functions.php?
How can I troubleshoot this?
My home.php code is as follows:
<?php get_header(); ?>
<?php if(is_front_page() || is_home() ) : ?>
<?php if(function_exists('show_flexslider_rotator')) echo show_flexslider_rotator( 'homepage' ); ?>
<?php endif; ?>
<div class="row below-banner">
<div id="site-content" class="span8">
<div class="row">
<div class="span8">
<div class="widget-header">
<h2>What's new</h2>
</div>
</div>
</div>
<div class="row">
<div class="posts span8">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') :1; query_posts('cat=-35&paged=' . $paged);?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part( 'entry' ); ?>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php else : ?>
<h2>No Posts Found</h2>
<?php endif; wp_reset_query(); ?>
</div>
</div>
<?php get_footer(); ?>
We would need more info/code in order to give a definitive answer, but my best guess is that you need to add a wp_reset_query() somewhere.
A quick test: add wp_reset_query() immediately before the conditional statements and see if that resolves the issue. If so, then you need to find the offending query and make sure it is reset at the appropriate time.
The issue with wordpress conditionals like is_single is that they don't work outside a functions in functions.php
So the solution is to place the conditional within the function and call that function using add_action probably.
The code here is_single not working in functions.php explained how it won't work outside the function and how should it be instead

custom wordpress theme echoes gallery short code

So i'm quite a beginner, trying to make a custom theme. In a page i want to have a gallery. Uploaded images, made a gallery all fine.
When I view the page it outputs only the shortcode:
[gallery orderby="post_date"]
my page.php file basically has:
<?php $content = get_page( $page_id ) ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<?php echo "<h1>".$content->post_title."</h1><br>" ; ?>
<?php echo $content->post_content ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I really don't understand how to get this to show correctly, any pointers would be greatly appreciated. Cheers, Matt
get_page returns the raw page data. There are a few ways to do what you want:
BAD WAY:
<?php $content = get_page( $page_id ) ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<?php echo "<h1>".$content->post_title."</h1><br>" ; ?>
<?php echo do_shortcode($content->post_content); ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
do_shortcode() renders all registered shortcode that's found within a given string. In this case, your page's content will have all shortcode rendered before being written to the document. I say this is the "bad" way, only because it doesn't follow the usual Wordpress format. Which leads us to the:
BETTER WAY:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<h1><?php the_title(); ?></h1><br>
<?php the_content(); ?>
</div>
</div>
<?php endwhile;endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is what's called "The Loop". It is pretty much the standard for all Wordpress Themes in retrieving all Post or Page Data, as well as running queries against the database.
I would suggest getting to know it, as well as running Wordpress queries to modify the loop using WP Query. This is getting into a more complex area of Wordpress, but it will help you in the longrun with figuring out how to gather up all of the posts and pages you want to retrieve in your theme that aren't provided by Wordpress' globals.
Good luck.

wp_get_recent_posts stopped working

I want to return the 2 newest posts when in single post mode, but exclude the current post. And I did it. The problem is, it just stopped working. It didn't change the code and it stopped on the server as well as on my localhost.
Here's the code:
<section id='recent_posts'>
<header class='recent_header'>
Recent posts
</header>
<?php
$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");
foreach( $recent_posts as $recent ) { ?>
<article class='single_recent'>
<header>
<?php echo $recent["post_title"]; ?>
</header>
<p>
<?php echo get_excerpt_by_id($recent["ID"]); ?>
</p>
</article>
<?php } ?>
</section>
Does anyone have an explanation ?
I tried removing the argument, still nothing. It returns an empty array.
Any suggestions which other function should I use to achieve the same effect ?
EDIT:
<?php
get_header();
get_sidebar();
?>
<?php the_post() ?>
<article class='post-single'>
<header class='post_header'>
<h1><?php the_title(); ?></h1>
<div class='post_header_bottom'>
<strong class='post_category'><?php echo get_the_category_list(', '); ?></strong>
<strong class='post_author'><span class='symbol'>U</span> by <?php the_author(); ?></strong>
</div>
</header>
<?php if (has_post_thumbnail()) : ?>
<figure class='post_single_image'>
<?php the_post_thumbnail(); ?>
<figcaption>No Will No Skill</figcaption>
</figure>
<?php endif; ?>
<div class='post_perex'>
<?php the_content(); ?>
</div>
<footer class='post_footer'>
<div class='post_footer_top'>
<div class='post_tags'>
<?php the_tags('', '', ''); ?>
</div>
<div class='post_time'>
<time datetime='<?php the_time('Y-m-d'); ?>' pubdate>
<span class='symbol'>P </span>
<?php relative_post_the_date(); ?>
</time>
</div>
</div>
<div class='post_share'>
<div class='share_show'>
<span class='symbol'>f</span> Like
|
<span class='symbol'>g</span> +1
|
<span class='symbol'>t</span> Tweet
<?php
if(function_exists('display_social4i'))
echo display_social4i("large","align-left");
?>
</div>
</div>
</footer>
</article>
<?php comments_template(); ?>
<section id='recent_posts'>
<header class='recent_header'>
Recent posts
</header>
<?php
global $post;
$id = $post->ID;
$qargs = array(
'post__not_in'=> array($id),
'posts_per_page' => 2
);
$recent_posts = new WP_Query($qargs);
if ($recent_posts->have_posts()) echo 'yes'; else echo 'nope';
if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>
<article class='single_recent'>
<header>
<?php the_title(); ?>
</header>
<p>
<?php the_excerpt(); ?>
</p>
</article>
<?php endwhile;endif; ?>
</section>
<div class='space'></div>
</div>
<?php
get_footer();
?>
You're missing your loop, or at least an global instance of the current post object. While I prefer to use the loop myself, you can get away with using the latter.
Change:
$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");
To:
global $post;
$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");
UPDATE:
The loop can be used in a single view to grab information from the default query_posts object. Even though it's just one post, the loop is most often used to populate the_content and other information.
You're correct that the ID should be irrelevant in this instance, since wp_get_recent_posts() should still return some results without any arguments (unless, of course, you're dealing with custom post types).
An alternate solution would be to try getting your recent posts using WP_Query:
<section id='recent_posts'>
<header class='recent_header'>
Recent posts
</header>
<?php
global $post;
$id = $post->ID;
$qargs = array(
'post__not_in'=> array($id),
'posts_per_page' => 2
);
$recent_posts = new WP_Query($qargs);
if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>
<article class='single_recent'>
<header>
<?php the_title(); ?>
</header>
<p>
<?php the_excerpt(); ?>
</p>
</article>
<?php endwhile;endif; ?>
</section>
WP_Query will default to the standard 'orderby'=>'date' and 'order'=>'DESC' parameters, so those don't need to be explicitly stated (though, you can add them if you like).
As mentioned above and in my comment, I'm unsure if you want just the most recent posts, or if you're trying to query recent custom post types. If the recent posts are of type 'post' and that's what you want, then that is the default for WP_Query's 'post_type' parameter as well as wp_get_recent_posts.
Otherwise, you will need to explicitly state the 'post_type' parameter, so that $qargs looks like:
$qargs = array(
'post__not_in'=> array($id),
'posts_per_page' => 2,
'post_type' => array('post', 'custom_post_type_slug', ...)
);
Just to check, you can also set 'post_type' to 'all' if you want to ensure SOMETHING is returned. If nothing is returned by either WP_Query or wp_get_recent_posts with 'post_type' set to 'all', then the issue is much larger, and I'll need to see all of the code in your single.php template.
Hope this helps.
NEW UPDATE:
Try commenting out the block of code altogether and substitute for:
wp_get_archives(array('type'=>'postbypost'));
If that doesn't work, then I'm fresh out of ideas. Something may have happened on your filehost's end that could possibly explain something happening out of nothing. Check and see if they have any announcements about this kind of activity. I know that many filehosts are in the process of replacing PHP4 and older versions of MySQL, which can probably cause some unforseen issues.
I would try creating a new subdomain with a clean, separate installation of Wordpress and a copy of your theme. Once that's set, just create a new post and see if the same problem occurs.
If so, then comment out chunks of code (possibly start with commenting out from get_sidebar through your first <article>...</article> block) in your single.php file and work out any errors that might pop up. If the block of code we worked on suddenly begins to populate, then post the block of code that was preventing it from happening and I'll try to work with you further (that is, if you're still having trouble).
Otherwise, if the clean installation fixes your issues, then it's probably some kind of discrepancy in your wp_options table. I would start merging tables over (first wp_posts, then wp_postmeta, then wp_terms....) until the problem pops up again.
Unfortunately, I think exhaustive testing might be in order to get this anomaly straightened out. Sorry I don't have a pure code solution. This is a pretty strange issue you're going through. Keep me posted, I'll do what I can to help.

WordPress featured post and sharethis issue

im having some trouble with this featured post function that I made. the issue that im having is that when query('showposts=1'); is set the featured post is not picked up. however when I put query(''); the sharethis plugin doesn't work. can any one please help me on what I might be doing wrong.
<div id="block_feature">
<div id="featured_post" class="post">
<div class="post_inner">
<?php
$featured = new WP_Query();
$featured->query('showposts=1');
while($featured->have_posts()) : $featured->the_post();
//$wp_query->in_the_loop = true; // This line is added so that the_tags('') will work outside the regular loop.
$featured_ID = $post->ID; // We'll store this here so that we know to skip this post in the main loop
?>
<?php if(get_post_meta($post -> ID, 'feature', true)) { ?>
<?php if (get_post_meta($post->ID, 'large_preview', true)) { ?>
<div class="post_image">
<img src="<?php echo get_post_meta($post->ID,'large_preview',true);?>" width=150px; height=150px alt="Featured Post"/>
</div>
<?php } ?>
<div class="excerpt">
<h2><?php the_title(); ?></h2>
<small>on <?php the_time('M d'); ?> in <?php the_category(',');?> tagged <?php the_tags(''); ?></small>
<?php the_excerpt();?>
</div>
Read More
<?php } ?>
<?php endwhile; ?>
</div>
</div>
</div>
showposts is been deprecated since version 2.1, so it might be better to use posts_per_page instead. Not sure if it will make any difference, but you could also replace the first two lines of PHP with $featured = new WP_Query('showposts=1');
As to what is causing the problem I am not sure, your query looks fine to me and you didn't mention in what way Share This failed. I'm not familiar with the Share This plugin, but most such plugins add its content to the post using a filter function attached to the the_content filter. That said, it might just be that you are using the_excerpt() and not the_content().

Resources