next_posts_link() in the same category - php

I was wondering if it's possible to limit next_posts_link() and previous_posts_link() to a specific category in Wordpress.
I've got a website where I display one post every page. Clicking on next and previous arrow, it goes to next or previous post. However I would like to detect if the next or previous post is in the same category BEFORE loading it and remove prev/next arrow in case the post is not in the same category.
Here my partial code:
<?php if(in_category( '1' )) {
echo '<section class="next_nav">';
next_post_link('%link', '<img src="http://www.mywebsite.it/wp-content/uploads/2014/11/next_post.png">', TRUE);
echo '</section>';
echo '<section class="previous_nav">';
previous_post_link('%link', '<img src="http://www.mywebsite.it/wp-content/uploads/2014/11/previous_post.png">', TRUE);
echo '</section>';
}
else {
echo '';
}
?>
When I click on next button, I get the next post but it's not in the same category. I was wondering if it was possible to use get_next_post() or get_adjacent_post() together with the previous function.
How can I get rid of it ?

Related

Wordpress post Loop not working as expected

I'm using a theme that uses the blog page to display all of its content on the blog page rather than an excerpt and then when i click into the post i want to show the whole content.
I'm using the following code:
$postId = get_the_ID();
$ex = the_excerpt();
if($postId == 19){
echo $ex;
}
else{
echo $content;
}
The blog page is located at post =19
I would expect only the excerpt to show on the blog page and the content to show on the post page. However both show. Also it doesnt matter if i change the number 19 in my if statement as the same happens. Can anyone see where i am going wrong?
edit made changes, screen shots:
You need to use the_excerpt(); inside the condition means where you want to display the excerpt but in case you want to get value of excerpt but does not want to display it directly then you have to use get_the_excerpt(); so you need to change
$ex = the_excerpt();
to
$ex = get_the_excerpt();
And same is the case with the_content()
Hope it helps you.
functions such as the_excerpt() are only available inside the loop or after you call the function the_post()
You may want to display only the except in your index.php
while (has_posts()) {
the_post();
the_excerpt();
}
In your single.php you may want to display the whole content
if(has_posts()) {
the_post();
the_content();
}
Use $postId = get_queried_object_id(); instead of $postId = get_the_ID();

How do you create a "next page" link in Wordpress?

I'm trying to add a button to my site which links to the next page of posts. I'm looking for a way to add an HTML link to the button so it actually works.
http://codex.wordpress.org/Template_Tags/wp_link_pages
I believe that explains what I want, but I'm not having any luck trying to get those snippets to work.
My button looks like:
Next Page
How can I link the button to the next page function in Wordpress?
EDIT:
My problem is I can't figure out how to associate this code with a button.
There are two 'built-in' ways of doing this in Wordpress:
1) If you need access to the link as a PHP variable, try Using get_next_posts_link
<?php $nextLink = get_next_posts_link( $label , $max_pages ); ?>
<?php $previousLink = get_previous_posts_link( $label ); ?>
From here the links are stored in variables and you can do whatever you'd like with them.
2) Otherwise use these:
<?php next_posts_link( $label , $max_pages ); ?>
<?php previous_posts_link( $label ); ?>
Where $label is the name of the link ( "Next Page" in your case ) and $max_pages is the max number of pages (if you want a limit), that the link shows up on.
If you want to style these, without having to enclose them inside another DIV, use Wordpress Filters
function apply_my_next_link_style ( ){
return 'class="button"';
}
apply_filters( 'next_posts_link_attributes', 'apply_my_next_link_style' )
3) If you need even more control you can try this, from the source code for the above functions:
if ( !is_single() ) {
echo '<a href="' . next_posts( 0, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', "Next Page") . '</a>';
}
Also, I would suggest asking these types of questions over at wordpress.stackexchange.com
Is necesary Snippets , visit this and this solve
<?php next_posts_link('Older Posts'); ?>
<?php previous_posts_link('Newer Posts'); ?>

Next & previous links: Not showing a link if there isn't a next page

I've followed the documentation here (at the bottom) to create next and back buttons at the bottom of my page.
It seems to work fine until I get to the last page where the link just redirects me back to the first page. Is there a way to say if there isn't a next page to not show the link? I assumed thats what the if statement was supposed to do!!
<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}
$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<?php if (!empty($prevID)) { ?>
<a class="back" href="<?php echo get_permalink($prevID); ?>">BACK</a>
<?php } ?>
<?php if (!empty($nextID)) { ?>
<a class="next" href="<?php echo get_permalink($nextID); ?>">NEXT</a>
<?php } ?>
p.s Please don't move my question to the Wordpress Stack - that seems to be dying a bit of a death and doesn't get many responses!
My Pages are setup like this:
Parent page
Sub page 1
Sub page 2
Sub page 3
I've created a link on the parent page to goto the first subpage. Then on the subpage template I've got the code above. I just want the next link to appear on each page then when it gets to page 3 it shouldn't show the next link.
If you are saying that this is effectively looping around then $nextID must never be empty, which would be why the link was always displayed.
You could set a $firstID, ie
$firstID = pages[0];
and then check;
if ($firstID != $nextID ) {
// Display link
}

How to create permalinks without single pages in wordpress?

I'm creating my own wordpress theme which is a bit different because it will not have single pages (or atleast, no single page will be reachable). The whole website contains just the homepage (with the loop) and previous posts pages.
I want to link to individual posts inside the loop like site.com#post-124 or site.com/paged=5#post-214.
I already created a function that does this:
function getPermalink($id,$postsPerPage) {
$postNumber = Get_Post_Number($id);
//a function that get's the post number based on
//the chronical order of published posts.
$page = floor(($postNumber - 1) / $postsPerPage);
$url = get_option('home');
if($page > 0) {
$url .= '/?paged=' . ($page + (1 - floor($page / 5)));
}
$url .= '#post-' . $id;
return $url;
}
You can see it live here: http://mijnrealiteit.nl (the previous posts pages are replaced by an infite scroll plugin).
This works, however it breaks when I start adding posts because all the posts before will get shifted back to pages further away (this makes the link invalid).
The way I see it there are two possible solutions:
Change the permalinkstructure to display paging backwards (so x.com/paged=231 becomes the first 'previous' page. However this is not userfriendly.
Make links with just the ID and let wordpress handle custom redirection to the page at that current point in time.
Are there better alternatives? I'm sure this is already solved somewhere, I just couldn't find it.
I got pushed in the right direction by a friend, I build it quite easily using option 2:
The getPermalink function is now much simpler:
function getPermalink($id) {
return get_option('home') . '/?f=' . $id;
}
I didn't make any custom redirection, I just checked at the homepage for a an 'f' being passed in the GET request:
$perma = $_GET['f'];
if(isset($perma) && !is_paged()) {
$customposts = get_posts('p=' . $perma );
foreach( $customposts as $post ) :
setup_postdata($post); ?>
//load the post
<?php endforeach;
}?>
If that is true the post will be fetched using the get_posts function by Wordpress. I also check the (normal) loop for the post that already has been served:
<?php while (have_posts()) : the_post();
if(get_the_ID() != $perma) { ?>
//load the post
<?php } endwhile; ?>

Wordpress, if function returns "nothing"

I am building a wordpress theme I want to have the next and previous DIV only appear IF their is a next and previous button available.
If there is 10 posts and the display limit is 10 posts I don't want an empty div. However when there are 20 posts and the display limit is 10 I want the next and previous buttons to show in there own div.
I got this far and realized that posts_nav_link() doesn't return null.
function next_previous_div(){
print '<!-- ';
$output = posts_nav_link();
print " -->\n";
if ($output != null)
{
echo '<div class="float post" style="text-align:center">';
echo posts_nav_link();
echo '</div><!-- end post-->';
};
};
There are a few different ways you could test if you'll have a previous/next link:
Test $wp_query->max_num_pages
Get a global reference to the $wp_query object and see if it's greater than 1. If yes, you've got paging links:
global $wp_query;
if($wp_query->max_num_pages > 1){
posts_nav_link();
}
Capture the output of posts_nav_link()
This function echos by default, which is why your null test isn't working. You need to capture the echoed output and then test:
// Capture the echoed output in the $links variable and test on it
ob_start();
posts_nav_link();
$links = ob_get_clean();
if(strlen($links) > 0){
echo $links;
}
I think the closest you will get without custom scripting will be...
if (!empty(get_posts_nav_link()) {
echo '<div class="float post" style="text-align:center">';
echo posts_nav_link();
echo '</div><!-- end post-->';
}

Categories