Next / Prev post link not working wp - wordpress

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.

Related

have_post() does return null value in wp

I used the following code in my page template:
<?php
while(have_posts()):the_post();
the_content();
?>
But nothing is displayed. The loop is not working. I'm sure that, there is sufficient information as content in my template page.
You should use if condition to check if post exists else skip the loop. Make sure to ON the error log and check the exact error.
<?php wp_reset_query(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php
/**
* Template Name: My Template
*/
the_post();
the_content();
?>
If you save the following code as a page template and call the page template into a page by choosing it, then the page will show the page content without any hassle.
Note: it's THE minimal bit of code.
use the following code
Try using wp_reset_query()
<?php
wp_reset_query();
while(have_posts()):the_post();
the_content();
endwhile;
?>

Search results that include words in the category and tags? WP

EDIT: Solved my main question.
I'm going around in circles here, finding similar questions but nothing quite right.
Q: I'm trying to add search results that include words in the category and tags.
For example; I have posts with documentary in the title and or description but not the title of the said documentary. If I search for the name of this documentary that isn't mentioned in the title and or description but it's filed under category x and or tags then I'd like it to show up in search. Reason being, I made posts about the documentary before I knew the name of it yet.
Also, is there a way to divide search results up, such as:
News (Posts) results found:
Page results found:
Or somewhere in the title that says what type of result each one is (post or page)
If too complicated, don't worry about that one. I'm mainly focusing on the main question.
I'm ever so grateful for your help. Thank you in advance.
My search.php is this:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<div id="content">
<div class="article">
<?php if ( have_posts() ) : ?>
<?php
global $wp_query;
$total_results = $wp_query->found_posts;
?>
<?php printf( __( 'Search results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?>
<br/><br/>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="date">
<?php the_time('M j, Y') ?> </div><div class="posttitle"><?php the_title(); ?></div>
<?php endwhile; ?>
</div>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { ?>
<div class="right"><?php next_posts_link('Next Page »') ?></div>
<div class="left"><?php previous_posts_link('« Previous Page') ?></div>
<?php } ?>
<br><br><br>
<?php else : ?>
<div class="posttitle">Nothing found. Try something else?</div>
This page doesn't exist
<?php endif; ?>
<?php get_footer(); ?>

show wordpress posts only under current category

i'm trying to build a navigation for a website, and i'm struggling with it. I'm trying to make a foldable navigation that shows only categories at first, when clicked, they link to the category, and show current category posts only.
I came this far:
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo '<h2><a href="./?cat='.$cat->term_id.'">'.$cat->name.'</h2>';
// create a custom wordpress query
query_posts("cat=$cat_id");
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?><br>
<?php endwhile; ?> <?php endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
The problem with this is: when i fill in the query so that it only takes the current cat, it displays current cat posts on both my categories.
this is what i want for the nav actually:
graphic design
Other Projects
when clicked on graphic design:
graphic design
Project 1
Project 2
Other Projects
when clicking on Other projects:
graphic design
Other Projects
Project 1
Project 2
so basically:
- when clicking from Index page to a category, only that category should expand
- when clicking the other category, the current category changes, so the previous category collapses and the other one expands.
and a bonus: is it possible, that when on a single post, there expands another level of info? for example, a few custom fields per post. like this:
graphic design
Other Projects
Project 1
custom field 1
custom field 2
…
Project 2
thank you very much
Try this code
<?php $article_categories = get_categories(array(
'child_of' => get_category_by_slug('graphic design')->term_id
));
$talentChildren = get_categories(array('child_of' => get_category_by_slug('Project 1')->term_id));
?>
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : ?>
<div class="post-list">
<?php foreach($talentChildren as $talent): ?>
<?php
$talentSubChildren = new WP_Query();
$talentSubChildren->query(array('category_name' => $talent->slug));
?>
<h2><?php echo $talent->name; ?></h2>
<ul>
<?php while ($talentSubChildren->have_posts()) : $talentSubChildren->the_post(); ?>
<li>
<?php talent_thumbnail(); ?>
<h4>
<?php the_title(); ?>
</h4>
<p><?php the_excerpt(); ?></p>
read on »
</li>
<?php endwhile; ?>
</ul>
<?php endforeach; ?>
<?php if($wp_query->max_num_pages!=1):?>
<div class="pagination">
<?php previous_posts_link('« prev') ?>
<span class="current"><?php echo $wp_query->query['paged']; ?></span>
of <span class="total"><?php echo $wp_query->max_num_pages; ?></span>
<?php next_posts_link('next »') ?>
</div><!-- .pagination -->
<?php endif; ?>
</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; ?>
</div>
Ive gotten a bit further, but i need some help now.
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo '<h2><a href="./?cat='.$cat->term_id.'">'.$cat->name.'</h2>';
// create a custom wordpress query
query_posts("cat=$cat_id"); ?>
<?php if (in_category($cat_id)) { ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?><br>
<?php endwhile; ?> <?php endif; // done our wordpress loop. Will start again for each category ?>
<?php } else { ?>
<?php } ?>
<?php } // done the foreach statement ?>
So this is working now: it lists the posts per category, if in the current category, else it doesnt show anything.
now what i still want: i want to add something extra within the loop IF i'm on a single page. I have this code:
<?php wp_reset_query(); ?>
<?php if (is_single('84')) { ?>
Yes
<?php } else { ?>
no
<?php } ?>
But that would mean i have to break a query in the middle of a loop. and the is_single thing does not work inside a loop / without the query reset.
I want it looking like this with above code:
Graphic Design
project 1 (for example id=84)
Yes
project 2 (for example id=101)
No
thanks

Wordpress blog isn't showing posts. Does adding a custom portfolio post field effect this?

Now I've done this a couple other times with no problem, I have a main page using home.php and the blog as another page using "blog-home.php" blog template with all the right code to grab the posts but it's not displaying. The only difference is I've added a custom portfolio post field to the functions, would this be effecting it or could it be something else? I can access a post from the home page under latest post putting the code below but that's it.
<?php query_posts("post_per_page=1"); the_post(); ?>
<p><?php the_excerpt(); ?></p>
<?php wp_reset_query(); ?></div>
*UPDATE: I've tried another code but now it is only displaying the blog page as a post. *
<?php
/*
Template Name: Blog Home
*/
?>
<?php get_header(); ?>
<div id="contentwrapper">
<?php query_posts( array ( 'category_name' => 'Blog', 'posts_per_page' => 5 ) ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="blogentry">
<h4><?php the_title(); ?> </h4>
<?php the_content(); ?>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
</div>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_footer(); ?>
Maybe if you use
$posts = get_posts(array('numberposts' => 1));
global $post;
$post = $posts[0];
the_excerpt();
instead
query_posts();
is never a good idea change the global query if get_posts dosn't work for you try with WP_Query()

Make the_author only echo once in the loop

basically I have my site setup to display the title of each page/post in the header of my theme. I also have it setup to display the name of an author on my author pages(I did this using a conditional). The problem I'm having is that the name of the author is echoed for the number of posts the author has written and I only want it to echo once. Is there a way I can do this? I realize that its doing this because the_author has to be in a loop(I have a loop in my header to do this). Is there anyway to be able to display the_author outside of the loop?
My site is http://www.imagineitstudios.com . you can see what I'm talking about if you click on the "Posted by Abel" link on the first post of the home page
Thanks for your help.
Here's my code:
<div id="title">
<?php //Check to see if this is an author page ?>
<?php if(is_author()): ?>
<?php while (have_posts()) : the_post(); ?><?php //Creat a mini loop to display the author ?>
<h1><?php the_author();?></h1>
<?php endwhile;?><?php //End of Mini Loop ?>
<?php else :?>
<h1><?php the_title();?></h1>
<?php endif ;?>
</div>
You can use the following code:
<div id="title">
<?php //Check to see if this is an author page ?>
<?php if(is_author()): ?>
<?php
if(get_query_var('author_name')) :
$curauth = get_userdatabylogin(get_query_var('author_name')); ?>
<?php else :
$curauth = get_userdata(get_query_var('author')); ?>
<h1><?php echo $curauth->first_name; ?> <?php echo $curauth->last_name; ?></h1>
<?php endif; ?>
<?php else :?>
<h1><?php the_title();?></h1>
<?php endif ;?>
</div>

Resources