Here’s where the post navigation code lives in “content-single.php”:
<?php if ( ! get_theme_mod( 'new_standard_hide_post_nav' ) ) : ?> <?php new_standard_post_nav(); ?><?php endif; ?><?php if ( is_active_sidebar( 'after-post' ) ) : ?> <?php dynamic_sidebar( 'after-post' ); ?><?php endif; ?>
And here’s where that lives in the theme’s function files:
if ( ! function_exists( 'new_standard_post_nav' ) ) :
/**
* Display navigation to next/previous post when applicable.
*/
function new_standard_post_nav() {
// Don't print empty markup if there's nowhere to navigate.
$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous ) {
return;
}
?>
<nav class="navigation post-navigation" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Post navigation', 'new-standard' ); ?></h1>
<div class="nav-links">
<?php
previous_post_link( '<div class="nav-previous"><div class="post-nav-title">' . __( 'Older post', 'new-standard' ) . '</div>%link</div>', _x( '%title', 'Previous post link', 'new-standard' ) );
next_post_link( '<div class="nav-next"><div class="post-nav-title">' . __( 'Newer post', 'new-standard' ) . '</div>%link</div>', _x( '%title', 'Next post link', 'new-standard' ) );
?>
</div><!-- .nav-links -->
</nav><!-- .navigation -->
<?php
}
endif;
I’m a bit of a novice with PHP so I appreciate any guidance or help.
]]>At the bottom and top of this post, the auto-populating link to the previous post goes to a separate, unrelated page that we did not post, for the Lucky Duck online casino
Notice how it’s got our site URL, and tacks on the post URL for the casino.
Same situation with our previous legitimate post. At that page the link for next post goes to the same outside page.
Any help would be most appreciated.
]]>Also I think there are a couple of bugs in v1.8, the button showed up as white with a black border and was smaller than the ‘previous’ button until I put some text in it; then the button was correct size but the font size was larger than the default setting.
]]>What I want to accomplish is have my single.php display posts in the same category for the previous and next posts underneath the post. The problem is that each of my posts are in multiple categories, and they are display through one of the other categories versus the one I want them to display from. Does that even make sense?
Example Categories:
Characters (parent category) (Subcategories listed below:)
– Tony
– John (This one is displaying instead.)
– Farah
– Stories (I want these ones to display.)
Now, my question is, how do I choose the category I want to be pulled from instead of the one being automatically pulled? Here is my code below (that I’ve found and been fiddling with), with what I’ve tried so far.
<?php
if (is_single() && in_category('stories')) {
$post_id = $post->ID;
$cat = get_the_category(); //I've tried changing this to my category (both ID and slug)
$current_cat_id = $cat[0]->cat_ID; //Also tried plugging ID and slug
$args = array(
'category' => $current_cat_id, //Also tried plugging ID and slug
'orderby' => 'post_date',
'order' => 'DESC'
);
$posts = get_posts($args);
$ids = array();
foreach ($posts as $thepost) {
$ids[] = $thepost->ID;
}
$thisindex = array_search($post_id, $ids);
$previd = $ids[$thisindex - 1];
$nextid = $ids[$thisindex + 1];
if (!empty($nextid)) {
?><div class="double-grid"><a rel="next" href="<?php echo get_permalink($nextid) ?>"><div class="image-tile tile-on-archive-page" style="background-image: url('<?php echo get_the_post_thumbnail_url($nextid); ?>'"> <div class="gold-button">LAST STORY >></div></div></a></div><?php
}
if (!empty($previd)) {
?><div class="double-grid"><a rel="prev" href="<?php echo get_permalink($previd) ?>"><div class="image-tile tile-on-archive-page" style="background-image: url('<?php echo get_the_post_thumbnail_url($previd); ?>'"> <div class="gold-button">NEXT STORY >></div></div></a></div><?php
}
}
?>
Any input appreciated!
]]>On my site www.maartendemeijer.nl the secondary sidebar is where it should be on the home page (on the right) but it skips to the left in single post view.
I’ve noticed that in single post view the ‘previous post’ link and ‘next post’ link aren’t where they supposed to be as well: they appear on the right side while they should be on the bottom. Are these links perhaps ‘pushing away’ my secondary sidebar…?
I hope somebody can help with this problem. Please let me know if you need any CSS-rules from the theme I’m using to see what’s causing this skipping of elements.
By the way: I have a multisite installation and I don’t have this problem on my other sites (www.maartendemeijer.nl/improzaken and www.maartendemeijer.nl/brievenaanjan)
Kind regards
Maarten
The Netherlands
]]>By DEFAULT it:
But I NEED it:
I found this code that let me now order the Next/Prev Links alhpabetically however I still can’t show links only from same category of the post.
function filter_next_post_sort($sort) {
if (get_post_type($post) == 'post') {
$sort = "ORDER BY p.post_title ASC LIMIT 1";
}
else{
$sort = "ORDER BY p.post_date ASC LIMIT 1";
}
return $sort;
}
function filter_next_post_where($where) {
global $post, $wpdb;
if (get_post_type($post) == 'post') {
return $wpdb->prepare("WHERE p.post_title > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
}
else{
return $wpdb->prepare( "WHERE p.post_date > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'", $post->post_date, $post->post_type );
}
}
function filter_previous_post_sort($sort) {
if (get_post_type($post) == 'post') {
$sort = "ORDER BY p.post_title DESC LIMIT 1";
}
else{
$sort = "ORDER BY p.post_date DESC LIMIT 1";
}
return $sort;
}
function filter_previous_post_where($where) {
global $post, $wpdb;
if (get_post_type($post) == 'post') {
return $wpdb->prepare("WHERE p.post_title < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
}
else{
return $wpdb->prepare( "WHERE p.post_date < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'", $post->post_date, $post->post_type );
}
}
add_filter('get_next_post_sort', 'filter_next_post_sort');
add_filter('get_next_post_where', 'filter_next_post_where');
add_filter('get_previous_post_sort', 'filter_previous_post_sort');
add_filter('get_previous_post_where', 'filter_previous_post_where');
Note: Every post is assigned to only one category.
THANK YOU FOR HELP ME!
]]><?php get_header(); ?>
<div class="wrapper2">
<div class="container">
<div class="padding"></div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (in_category(3)) { ?>
<div class="main_comic">
<img src="<?php the_field('the_comic'); ?>" alt="<?php the_title(); ?>" class="fit_img">
</div>
<div class="post_info">
<h1 class="post_title"><?php the_title(); ?></h1>
<div class="share_this"><?php echo sharing_display(); ?></div>
<div class="post_navigation">
<?php next_post_link('%link', 'Next post in category', TRUE); ?>
</div>
</div>
<?php } else { ?>
<article>
<h1><?php the_title(); ?></h1>
<h2><?php the_field('sub_header'); ?></h2>
<time datetime="<?php the_time('Y-m-d'); ?>"><?php the_date(); ?></time>
<?php the_content(); ?>
</article>
<div class="post_info">
<div class="share_this"><?php echo sharing_display(); ?></div>
<div class="post_navigation on_blog">
<?php next_post_link('%link', 'Next post in category', TRUE); ?>
</div>
</div><br>
<?php } endwhile; else: ?>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
Here is the website is Monstrouspulp.com.
I’ve tried several ways to get this to work. I read somewhere that it might have to do with setting the post ID. I have tried replacing the next_post_link code above with this:
<div class="post_navigation">
<?php
$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) ?>"><div class="arrow-left genericon genericon-leftarrow"></div></a>
<?php } if (!empty($nextid)){ ?>
<a rel="next" href="<?php echo get_permalink($nextid) ?>"><div class="arrow-right genericon genericon-rightarrow"></div></a>
<?php } ?>
</div>
This works perfectly for the 5 most recent posts then it stops and you can’t get to the rest.
So, what is my problem? I’m leaving the site up with the second set of code in it so it at least somewhat works. If anyone wants to see how it works with the first set of code let me know.
Thanks.
I’ve started using onclick push data to Google Analytics to get a better idea of how visitors are navigating through my site. It’s going great except I’ve hit a snag tracking clicks on next and previous post links, as I can’t figure out how to insert the onclick code into the actual next and previous post links. Does anyone know how to do this?
For example, I need to insert:
onclick="_gaq.push(['_trackEvent', 'Next Page']);"
Into a href=”next post”, so that it looks like
a href="next post"
onclick=”_gaq.push([‘_trackEvent’, ‘Next Page’]);”`
Thanks!
-Alec
]]><?php next_posts_link('? Older Entries') ?>
<?php previous_posts_link('Newer Entries ?') ?>
I have also tried a Plugin version using PageNavi
<?php wp_pagenavi(); ?>
Example here
However, I am having the same problem with both techniques. If I choose older poses it loads page two, but the posts stay the same.
You can test it on both of the links above.
Has anyone got any idea why this might be happening.
Below is the code from one of the the page-template using the wordpress php:
<?php
/*
Template Name: Food and Travel - Eat
*/
get_header(); ?>
<div id="content" class="narrowcolumn" role="main">
<div class="homeHolder">
<ul id="myRoundabout">
<?php
$catID = 188;
if ($catID) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=$catID&paged=$paged");
} ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<li class="item1"><a>"> <span class="img-title"><?php the_title(); ?></span><?php the_post_thumbnail(); ?></a>
<?php endwhile; ?>
<?php else : ?>
<!-- <h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php /*?><?php get_search_form(); ?><?php */?>-->
<?php endif; ?>
</div>
<div class="home-hr"><img src="https://www.macsmagazine.com/wp-content/themes/macs/_assets/hr-line.jpg"/></div>
<div class="homepage-heading"><a href="https://www.macsmagazine.com/living/diet-and-fitness/">Eat</a></div>
<div class="home-hr"><img src="https://www.macsmagazine.com/wp-content/themes/macs/_assets/hr-line.jpg"/></div>
<div class="sub-stories-holder">
<?php
if ($catID) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=26&paged=$paged");
} ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div id="sub-post">
<div class="post-content">
<div class="post-thumbnail">
<a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
</div>
<div class="post-copy">
<div class="post-title">
<a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</div>
<span style="text-decoration:none;"><a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">Read more ></a></span>
</div>
</div>
</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 /*?><?php get_search_form(); ?><?php */?>-->
<?php endif; ?>
</div>
</div>
<?php get_sidebar('foodtravel'); ?>
<?php get_footer(); ?>
[mod: the above code is partially broken – don’t use it for copy/paste]
Regards
Gary
we’d like to have the next/previous post links show up underneath each single post.
i tried adding this bit of code near the bottom of the single post template.
<div id="nav-below" class="navigation">
<div class="alignleft"><?php previous_post_link(); ?></div>
<div class="alignright"><?php next_post_link(); ?></div>
</div><!-- #nav-below -->
here is the whole template so you can see where i inserted it.
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-header">
<h1><?php the_title(); ?></h1>
<div id="single-date" class="date"><span><?php the_time( 'Y' ); ?></span> <?php the_time( __( 'F j', 'vigilance' )); ?></div>
</div><!--end post header-->
<div class="meta clear">
<div class="tags"><?php the_tags( 'tags: ', ', ', '' ); ?></div>
<div class="author"><?php printf( __( 'by %s', 'vigilance' ), get_the_author()); ?></div>
</div><!--end meta-->
<div class="entry clear">
<?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail( array(250,9999), array( 'class' => 'alignleft' ) ); ?>
<?php the_content(); ?>
<?php edit_post_link( __( 'Edit this', 'vigilance' ), '<p>', '</p>' ); ?>
<?php wp_link_pages(); ?>
</div><!--end entry-->
<div class="post-footer">
<p><?php _e( 'from →', 'vigilance' ); ?> <?php the_category( ', ' ); ?></p>
</div><!--end post footer-->
</div><!--end post-->
<?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
<div id="nav-below" class="navigation">
<div class="alignleft"><?php previous_post_link(); ?></div>
<div class="alignright"><?php next_post_link(); ?></div>
</div><!-- #nav-below -->
<?php comments_template( '', true); ?>
<?php else : ?>
<?php endif; ?>
</div><!--end content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
when the links appear, they overlap with the comments area… i’m sure this is an easy fix but i am still pretty new at this. thanks for any help!
]]>