Get next_post_link which is Sticky? - php

I am trying to get the link of next Sticky post with it's title using:
<h2><?php next_post_link('%link') ?></h2>
I have tried passing TRUE argument but that only filters the taxonomy not Sticky Posts.

There is no option in next_post_link to just get sticky post (correct me if i am wrong) . You need custom navigation here. First you need to get all sticky post in array and then make next posts links :
// get all sticky posts
$sticky = get_option('sticky_posts');
// if there are any
if (!empty($sticky)) {
// newest IDs first, optional
rsort($sticky);
$args = array(
'post__in' => $sticky
);
$postlist = get_posts();
$posts = array();
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
$posts[] = get_the_ID();
}
//wp_reset_postdata(); uncomment this, if this is a nested loop
$current = array_search($post->ID, $posts);
$prevID = $posts[$current-1];
$nextID = $posts[$current+1];
// Link for previous post
if (!empty($prevID)) {
echo '<div>Prev</div>';
}
// Link for next post
if (!empty($nextID)) {
echo '<div>Next</div>';
}
}

Related

Wordpress, setup_postdata() not doing its job

My posts get ordered by date which is picked by an advanced custom field datepicker. I want to use the regular Wordpress function references [the_title(), etc …] and the post related custom field's.
Right now the output of every loop is the the same. I read setup_postdata() can solve this issue and enable the use of the regular function references. I tried to apply it, but the output keeps being always the same. Thanks
<?php
global $posts;
$posts = get_posts(array(
'post_type' => 'post',
'meta_key' => 'release_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
$group_posts = array();
if( $posts ) {
foreach( $posts as $post ) {
$date = get_field('release_date', $post->ID, false);
$date = new DateTime($date);
$year = $date->format('Y');
$month = $date->format('F');
$group_posts[$year][$month][] = array($post, $date);
}
}
foreach ($group_posts as $yearKey => $years) {
foreach ($years as $monthKey => $months) {
echo '<li class="time">' . $monthKey . ' ' . $yearKey . '</li>';
foreach ($months as $postKey => $posts) {
setup_postdata($posts); ?>
<li class="item clearfix">
<!-- Wordpress Functions -->
<?php the_title();?>
<?php the_permalink();?>
<!-- Advanced Custom Fields -->
<?php the_field('blabla')?>
</li>
<?php
}
}
} wp_reset_postdata();
?>
You just need to add a line $post = $current_post; just before calling setup_postdata( $post ). See my example below to have a clear idea:
$posts = get_posts(array(.......));
// Call global $post variable
global $post;
// Loop through sorted posts and display using template tags
foreach( $posts as $current_post ) :
//the below line is what you missed!
$post = $current_post; // Set $post global variable to the current post object
setup_postdata( $post ); // Set up "environment" for template tags
// Use template tags normally
the_title();
the_post_thumbnail( 'featured-image-tiny' );
the_excerpt();
endforeach;
wp_reset_postdata();
For details please see this comment posted in the WP-Codex;

Unable to use the get_excerpt function in wordpress

I am new to wordpress and php. I am trying to get the excerpt of a post retrieved using foreach in wordpress. I am trying to display all the posts on a page but I dont want to display the whole content I want only excerpt.
I am able to displat the title but unable to get the excerpt.
I have tried: $excerpt = get_post_excerpt($post),$excerpt = get_the_excerpt($post);, $excerpt = $post->the_excerpt()
please also tell me if iam missing some basics.
here is my full code
<?php
function some_code() {
// query
$query = 'orderby=date&order=asc&posts_per_page=-1';
$wpq = new WP_Query($query);
$posts = $wpq->get_posts();
foreach($posts as $post)
{
$link = get_permalink($post);
echo "<a href='$link'><h3>{$post->post_title}</h3></a>";
$excerpt = get_the_excerpt($post);
echo "$excerpt";
}
}
?>
An easy solution compatible with your code would be to use setup_postdata($post); inside your foreach loop, which makes all the post related data available:
$query = 'orderby=date&order=asc&posts_per_page=-1';
$wpq = new WP_Query($query);
$posts = $wpq->get_posts();
foreach($posts as $post)
{
setup_postdata($post);
$excerpt = get_the_excerpt($post);
echo $excerpt;
}
More about setup_postdata() can be found here.
Try this also if that doesn't work, I think using a traditional loop instead of foreach is a better approach:
$query = array('orderby' => 'date', 'order' => 'asc', 'posts_per_page' => '-1');
$wpq = new WP_Query($query);
if($wpq->have_posts()){
while($wpq->have_posts()){
$wpq->the_post();
the_excerpt();
}
}
Or you can use a function called wp_trim_words:
echo wp_trim_words( $post->post_content );

Display the posts from the same category when user clicks on next/previous post in single.php page

I am using this code but once i click on the next post/previous post link i am redirected to the next post/ previous post of the different category
previous_post_link('%link', 'Prev post in category', $in_same_term = true);
next_post_link('%link', 'Next post in category', $in_same_term = true);
I am trying to fix my issue using this article http://codex.wordpress.org/Function_Reference/next_post_link
Thanks
Here is the code to get category based previous and next links on posts
$post_id = $post->ID; // current post id
$cat = get_the_category();
$current_cat_id = $cat[0]->cat_ID; // current category Id
$args = array('category'=>$current_cat_id,'orderby'=>'post_date','order'=> 'DESC');
$posts = get_posts($args);
// get ids of posts retrieved from get_posts
$ids = array();
foreach ($posts as $thepost) {
$ids[] = $thepost->ID;
}
// get and echo previous and next post in the same category
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
if (!empty($previd)){
?>
<a rel="prev" href="<?php echo get_permalink($previd) ?>">Previous</a>
<?php
}
if (!empty($nextid)){
?>
<a rel="next" href="<?php echo get_permalink($nextid) ?>">Next</a>
<?php
}
?>
You can do this...
Just change the argument $in_same_term as true..
previous_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' );
its wordking category wise please check
previous_post_link('%link', ' Privious',$in_same_term = true) ;
next_post_link('%link','Next', $in_same_term = true) ;

is_page() is not executed

I am making infinite scroll page. I created a page template where posts of certain category are loaded. Everything works fine with a single page and single category.
But I have three pages with this page template and each page should load articles from a specific category. I am using the is_page() if elseif block to determine on which page visitor is. But is_page() is not executed.
Here is the loop:
$cat = '';
if(is_page(703)){
$cat = 4;
} elseif (is_page(706)) {
$cat = 21;
}
$args = array(
'cat' => $cat,
'paged' => $paged
);
$infinite_news_query = new WP_Query($args);
if ( $infinite_news_query -> have_posts() ) : while ( $infinite_news_query -> have_posts() ) : $infinite_news_query -> the_post();
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php wp_reset_postdata();
?>
This code displays all the posts, regardless of category, and $cat is empty inside the loop.
What am I doing wrong?
Thanks!
Change the following:
$cat = '';
if(is_page(703)){
$cat = 4;
} elseif (is_page(706)) {
$cat = 21;
}
to:
if(is_page('703')){
$cat = 4;
} else { if(is_page('706')) {
$cat = 21;
}
Also removing $cat = '';
Look at it like this, at first you have the variable $cat set to empty value, respectively, to false , zero or 0.
Then you set a loop, with only two states, leaving out a default value.
So, what is going to happen, when your page ID is changed?
You get all the posts displayed.
In other words, I do not see anything bad in the code. I would rather recommend you to use is_page('slug') than with id.

Getting Next/Previous Post ID using Current Post ID in Wordpress

I want to write a custom next/prev function to dynamically display post information in a fancybox pop-up. So I need to use PHP to get the next and previous Post ID based on whatever Post is currently showing. I know what the current post ID is and I can send that to a function ok, but I can't figure out how to use that ID to obtain the adjacent IDs.
Edit:Here is my code so far (which is not working)
<?php
require_once("../../../wp-blog-header.php");
if (isset($_POST['data'])){
$post_id = $_POST['data'];
}else{
$post_id = "";
}
$wp_query->is_single = true;
$this_post = get_post($post_id);
$in_same_cat = false;
$excluded_categories = '';
$previous = false;
$next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
$post_id = $next_post->id;
$title = $next_post->post_title;
$dataset = array ( "postid"=>$post_id, "posttitle"=>$title );
//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.
echo json_encode($dataset);
?>
get_adjacent_post() uses the global $post as its reference point, so you'll want to replace this:
$this_post = get_post($post_id);
with this:
global $post;
$post = get_post($post_id);
WordPress also provides get_next_post() and get_previous_post(), which you can use here instead of using get_adjacent_post() with all of those arguments. Here's the final product:
<?php
require_once("../../../wp-blog-header.php");
if (isset($_POST['data'])){
$post_id = $_POST['data'];
}else{
$post_id = "";
}
$wp_query->is_single = true;
global $post;
$post = get_post($post_id);
$previous_post = get_previous_post();
$next_post = get_next_post();
$post_id = $next_post->id;
$title = $next_post->post_title;
$dataset = array ( "postid"=>$post_id, "posttitle"=>$title );
//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.
echo json_encode($dataset);
?>
I'm not sure what keys you'd like to use for the IDs and titles of the previous and next posts in the $dataset array, so I'll leave that as is for now.
To get this to work I had to change $next_post->id; to $next_post->ID;, i.e. capitalise ID.
I used it in Wordpress on index.php like this – I inserted this at the top of the loop:
<div class="work-post" id="<?php the_ID(); ?>">
then within the loop I use this:
`
if (isset($_POST['data'])){
$post_id = $_POST['data'];
}else{
$post_id = "";
}
$wp_query->is_single = true;
global $post;
$post = get_post($post_id);
$next_post = get_next_post();
$previous_post = get_previous_post();
?>
<!-- call only if a value exists -->
<?php if($next_post) : ?>
<div>PREV.</div>
<?php endif; ?>
<!-- call only if a value exists -->
<!-- call only if a value exists -->
<?php if($previous_post) : ?>
<div>NEXT</div>
<?php endif; ?>
<!-- call only if a value exists -->`
This is my code, it worked good. $post_id is current post ID.
function get_previous_post_id( $post_id ) {
// Get a global post reference since get_adjacent_post() references it
global $post;
// Store the existing post object for later so we don't lose it
$oldGlobal = $post;
// Get the post object for the specified post and place it in the global variable
$post = get_post( $post_id );
// Get the post object for the previous post
$previous_post = get_previous_post();
// Reset our global object
$post = $oldGlobal;
if ( '' == $previous_post )
return 0;
return $previous_post->ID;
}
function get_next_post_id( $post_id ) {
// Get a global post reference since get_adjacent_post() references it
global $post;
// Store the existing post object for later so we don't lose it
$oldGlobal = $post;
// Get the post object for the specified post and place it in the global variable
$post = get_post( $post_id );
// Get the post object for the next post
$next_post = get_next_post();
// Reset our global object
$post = $oldGlobal;
if ( '' == $next_post )
return 0;
return $next_post->ID;
}
Then you just call function. Example:
echo get_previous_post_id( $current_id );
echo get_next_post_id( $current_id );
Good luck!

Categories