Wordpress previous post shortlink - wordpress

Hey guys so far I have this code that gives me the full link to my previous post
<?php $prevPost = get_previous_post(FALSE);
if($prevPost) {?>
<div class="prev1" onclick="javascript:_gaq.push(['_trackSocial', 'Nav', 'Prev-Thumb']);">
<?php previous_post_link('%link',"«", FALSE); ?>
</div>
<?php } ?>
But I want the shortlink instead of the full link
How can I accomplish that ?
Thank you in advance

Swap out your existing code with the following.
<?php
// Get the previous post.
$previous_post = get_previous_post();
// Check a post was found.
if ( $previous_post ) : ?>
<div class="prev1" onclick="javascript:_gaq.push(['_trackSocial', 'Nav', 'Prev-Thumb']);">
«
</div>
<?php endif; ?>

Related

Next / Prev post link not working wp

Ok so I am having issues with linking to next and previous posts...
Here is my code:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<div id="project-prev"> <?php previous_post_link('Prev'); ?> </div>
<div id="project-next"> <?php next_post_link('Next'); ?> </div>]
...
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php get_footer(); ?>
I have read places that next/prev posts requires a 'new WP_Query' query, but have had no such luck. There is no next/prev link rendered out on my site, using the above.
As always appreciate solutions and pointers.
Many thanks
Have you tried following (according to Wordpress codex)
<?php next_post_link('<strong>%link</strong>'); ?>
<?php previous_post_link('<strong>%link</strong>'); ?>
In your divs ... :) If you still encouter problems, then just try something like:
<?php echo get_previous_posts_link('Prev'); ?>
<?php echo get_next_posts_link('Next'); ?>
Should work.
EDIT:
<div id="project-prev"><?php previous_post_link('%link', 'PREV'); ?></div>
<div id="project-next"><?php next_post_link('%link', 'NEXT'); ?></div>
First try and get the next and previous posts.
<?php
$previous_post_url = get_permalink(get_adjacent_post(false, '', true));
$next_post_url = get_permalink(get_adjacent_post(false, '', false));
?>
And then create to <a> tags and echo out the URLs we set above.
<?php if ( $previous_post_url != get_the_permalink() ) : ?>
Previous Project
<?php endif; ?>
<?php if ( $next_post_url != get_the_permalink() ) : ?>
Next Project
<?php endif; ?>
One reason that could make these links not to show is having the posts set as draft. Only published posts will make the previous and next post links render.

wp_pagenavi wont work using custom post per page

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.

wordpress won't display posts

The problem I have is I cannot get Wordpress to show the posts where I need to.
I have already tried using two methods the first in the reading settings by selecting a page to display the posts which did not work. The second was a getpost plugin which displays the post using short code [get_posts] it didn't display any post just the short code and the plugin is active.
I want it to appear like this http://www.completesource.co.uk/category/ironkey-id-theft/ but on this page http://beta.completesource.co.uk/it-news/
I have been searching for this for a while any help would be greatly appreciated.
Thank you
EDIT: code
<?php
if (is_page() ) {
$category = get_post_meta($posts[0]->ID, 'category', true);
}
if ($category) {
$cat = get_cat_ID($category);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = -1; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'category__in' => array($cat),
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => $post_per_page,
'caller_get_posts' => $do_not_show_stickies
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_excerpt('Read the rest of this entry »'); ?>
Read More..
<BR><BR>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif;
$wp_query = $temp; //reset back to original query
} // if ($category)
?>
Personally I would write a page template for the page, then create a new page and use the template. Start reading about Page Templates and go from there.
Every Wordpress theme on the market will handle the display of posts very differently. It can very by page, shortcode, template, settings, etc.
We need to know what theme you are using in order to help you. In order to help yourself, investigate how to activate the blog by checking your themes document files or the theme developer or any help forums for the theme.

Wordpress Category Page Navigation

my problem is that I can navigate between Blog pages and posts but I cant navigate between the category pages.
My code looks like that:
<div class="previous-page">
<?php
if(is_single()){
previous_post_link( '%link', __( '<div title="%title">‹</div>') );
} else {
previous_posts_link( __('‹') );
}
?>
</div>
and I have the same code for the next_post/next_posts.
I thougt that the previous code also covers the category.
So what I am missing?
Greetings and Thanks
Chris
Try using this code which I currently use for all my themes:
This goes in your functions.php file:
function show_posts_nav() {
global $wp_query;
return ($wp_query->max_num_pages > 1);
}
Then in your template files use this to show the navigation:
<?php if (show_posts_nav()) : ?>
<div class='navigation'>
<?php next_posts_link('« Older Entries'); ?>
<?php previous_posts_link('Newer Entries »'); ?>
</div>
<?php endif; ?>

Wordpress Comment reply link does not appear

I am using custom code to print the comments but the problem is whatever i do, i cant print the comment reply link under any comment....
here is the code
<?php // Do not delete these lines
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');
if (!empty($post->post_password)) { // if there's a password
if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie
?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}
}
/* This variable is for alternating comment background */
/*$oddcomment = 'class="alt" ';*/
$oddcomment = 'alt';
?>
<!-- You can start editing here. -->
<?php if ($comments) : ?>
<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>
<ol class="commentlist">
<?php foreach ($comments as $comment) : ?>
<!--<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">-->
<li class="<?php echo $oddcomment; ?> <?php if ($comment->comment_author_email == get_the_author_email()) { echo 'author_comment'; } ?>" id="comment-<?php comment_ID() ?>">
<?php echo get_avatar( $comment, 32 ); ?>
<cite><?php comment_author_link() ?></cite> Says:
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?>
<br />
<small class="commentmetadata"><?php comment_date('F jS, Y') ?> at <?php comment_time() ?> <?php edit_comment_link('edit',' ',''); ?>
</small>
<?php comment_text() ?>
<div class="reply">
<?php comment_reply_link( array ( 'reply_text' => 'Reply this comment' ) );
?>
</div>
</li>
<?php
/* Changes every other comment to a different class */
/*$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';*/
$oddcomment = ( empty( $oddcomment ) ) ? 'alt' : '';
?>
<?php endforeach; /* end for each comment */ ?>
</ol>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
<!-- If comments are closed. -->
<p class="nocomments">Comments are closed.</p>
<?php endif; ?>
<?php endif; ?>
But when i use wp_list_comments functions i can see the reply link appear automatically i am using wordpress 3.2.1
It can be helpful to read the source for comment_reply_link (http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/comment-template.php#L1061), as there are multiple conditions that can lead to the link not appearing. Work through each one at a time, and you'll likely find your answer.
The one that tripped me up, is that the link will not appear if comments are closed. So it can be helpful to review the comment settings on the blog to make sure that providing replies is actually allowed on this post.
Try this:
comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth'])))
Thanks to the following codex-entry (http://codex.wordpress.org/Function_Reference/comment_reply_link), I managed to find the actual use of the comment_reply_link() on the following link: http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/comment-template.php#L1061
This leaves me with the following question - did you try adding either the second or the third parameter for the function? I think there might be something fishy going on under the hood, making the comment-link not know where to actually link.
Try removing the following snippet
comment_reply_link( array ( 'reply_text' => 'Reply this comment' ) );
and instead use the following one
comment_reply_link( array('reply_text' => 'Reply this comment'), comment_ID(), the_ID() );
Let me know if it works out for you!
Enable nested comments in Admin > Settings > Discussion:
Enable threaded (nested) comments levels deep

Resources