Wordpress built-in pagination not working - wordpress

In the Wordpress reading options, I set it so that the index only displays 2 posts at most. However, the next and previous page buttons are missing. I went into my template, tried the default methods, and none of them worked.
I tried adding
<?php posts_nav_link(); ?>
Didn't work. I then tried
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
</div>
Which also didn't work. Nothing showed up in the source code of the final page, at all. These are default Wordpress functions, right? What is going wrong here?
If it's of any use, here's the index.php
<?php get_header(); ?>
<section class="content">
<ul class="items wrap">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<h4><?php the_time('F jS, Y') ?></h4>
<p><?php the_content(__('(more...)')); ?></p>
<hr>
<?php endwhile; else: ?>
<p><?php _e('Sorry, we couldn’t find the post you are looking for.'); ?></p>
<?php endif; ?>
<!-- I added the pagination code here. -->
</ul>
</section>
<div id="delimiter"></div>
<?php get_footer(); ?>
Thank you!

Turns out there were only two posts to display, so of course it didn't show the next/previous links. So there it is, problem solved.

Related

Comment 'get_sidebar();' have no effect: remove sidebar from theme

I'm trying to remove the sidebars from a WordPress site;
should be a pretty straight-forward action to do, but I have a problem:
I've commented every get_sidebar() I've found in my theme PHP page (including index.php, page.php etc) but seem that have no effect on the site.
Here is an example from my index.php:
<?php get_header(); ?>
<div id="page-wrapper" class="container">
<div class="row">
<div id="page-content" class="col-sm-7 col-md-8">
<?php if ( have_posts() ) : ?>
<?php get_template_part( 'templates/page-title' ); ?>
<?php $blog_layout = vw_get_option( 'blog_layout' ); ?>
<?php if ( 'classic' == $blog_layout ) : ?>
<div class="row archive-posts post-box-list">
<?php while ( have_posts() ) : the_post(); ?>
<div class="col-sm-12 post-box-wrapper">
<?php get_template_part( 'templates/post-box/classic', get_post_format() ); ?>
</div>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="row archive-posts vw-isotope post-box-list">
<?php while (have_posts()) : the_post(); ?>
<div class="col-sm-6 post-box-wrapper">
<?php get_template_part( 'templates/post-box/large-thumbnail', get_post_format() ); ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php get_template_part( 'templates/pagination' ); ?>
<?php comments_template(); ?>
<?php else : ?>
<h2><?php _e( 'Sorry, no posts were found', 'envirra' ) ?></h2>
<?php endif; ?>
</div>
<!--aside id="page-sidebar" class="sidebar-wrapper col-sm-5 col-md-4">
<?php // get_sidebar(); ?>
</aside-->
</div>
</div>
<?php get_footer(); ?>
in the example above you can see both html / PHP comment, but I've also tried with only HTML comment / only PHP comment.
may notice that i'm using a Child theme, but I've found the get_sidebar() only in the father theme.
I'm probably missing something stupid, should i remove the entire line? i'm not an WordPress expert so i'd like to do the less intrusive modification.
Thanks!
Usually there is an option within the most of WP themes to turn off the sidebar(s), also theme authors care to give you a simple checkbox for that in the single Page, Post, or even Category template, but sometimes there is no easy way out.
I suggest you check this guide back from 2017 and a bit more fresh one from 2019 here, most likely you will find your way without additional plugin in the first reference.

Wordpress - How do I show only the 4 newest posts on my page?

I am trying to only load the 4 latest posts on my page and I have found a line of code that should work. The problem is that I don't know where to put it.
<?php query_posts('posts_per_page=20'); ?>
This is what I found when I was searching for a solution (although I don't know if this will also work for showing only the 4 newest posts. As you can see I already have 'catname=projecten' on that line and I have no idea how to combine the two.
<?php
query_posts('catname=projecten');
while (have_posts()) : the_post();?>
<div class="col-md-3">
<div class="newsfeed center">
<h1><?php echo get_the_title( $ID ); ?> </h1>
<p>
<?php the_content(); ?>
</p>
</div>
</div>
<?php endwhile;
?>
I hope someone can help me out!
I'd like to answer it, because there are few things need to be corrected. This should do it, and notes below.
<?php
query_posts('category_name=projecten&posts_per_page=4');
while (have_posts()) : the_post(); ?>
<div class="col-md-3">
<div class="newsfeed center">
<h2><?php echo get_the_title(); ?> </h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
A few notes:
It's category_name, not catname.
You should not use <h1> for each title, for a better accessibility use <h2> or <h3> etc.
get_the_title( $ID ); the $ID seems not necessary to be there.
Don't wrap the_content(); with <p> it's rich content already, use <div> if you need.
Don't forget to reset the query afterwards.
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query

How to get category name from URL and pass to a template

I'm using the following page template to display the posts in a single category and plan to format the first post differently than the others. This works as desired but I have the category_name hard coded in the template. I want to use this template for several different categories and would like to learn how to pass the category_name to the template from a link.
For example, the link to the desired page using the special template is http://wildcatweb.net/5th/ and '5th' is also the category_name. How do I tell the template to get the category_name from the URL and use it in the template?
<?php
/*
Template Name: pageAssignments
*/ ?>
<?php get_header(); ?>
<div class="small-12 large-8 columns" id="content" role="main">
<header>
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<!-- show latest post FULL -->
<?php query_posts('showposts=1&category_name=5th'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="lastest-post">
<h2 class="pagetitle"><?php the_title(); ?></h2>
<?php the_content(); ?>
</div><!--close .latest-post -->
<?php endwhile; endif; ?><!-- end lastest post -->
<!-- show older post excerpts -->
<?php query_posts('showposts=5&offset=1&category_name=5th'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="older-post">
<h3 class="pagetitle"><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</div><!--.older-post -->
<?php endwhile; endif; ?><!-- end past-entry -->
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
try this :
$current_url = $_SERVER['PHP_SELF'];
$category_name = explode ('',$current_url);
$category_name = $category_name['3'];
query_posts('showposts=1&category_name='. $category_name);

Wordpress, custom page theme next/previos posts

I have a custom page template with code:
<?php
/*
Template Name: Featured
*/
get_header(); ?>
<div id="content_box">
<div id="content" class="posts">
<img src="http://www.dinneralovestory.com/wp-content/uploads/2010/04/favorites.png" alt="Favourites"/><br clear="all" /><br />
<?php
//The Query
$my_query = new WP_Query('category_name=favourites');
if ($my_query -> have_posts()) :
?>
<?php while ($my_query -> have_posts()) : $my_query -> the_post(); ?>
<div class="featured_box">
<div class="featured_thumb">
<?php the_post_thumbnail(); ?>
</div>
<div class="featured_content">
<span class="post_title"><?php the_title(); ?></span>
<?php the_excerpt(); ?>
</div>
</div>
<br clear="all" /><br />
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/navigation.php'); ?>
<?php else : ?>
<h2 class="page_header center">Not Found</h2>
<div class="entry">
<p class="center">Sorry, but you are looking for something that isn't here.</p>
</div>
<?php
endif;
wp_reset_query();
?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
The navigation.php file has the previous / next code (it works fine for the standard post pages and archive pages)
navigation.php:
<?php if (is_single()) : ?>
<div class="navigation">
<span class="previous"><?php previous_post_link('← %link') ?></span>
<span class="next"><?php next_post_link('%link →') ?></span>
</div>
<div class="clear whitespace"></div>
<?php else : ?>
<div class="navigation">
<div class="previous"><?php next_posts_link('← Previous Entries') ?></div>
<div class="next"><?php previous_posts_link('Next Entries →') ?></div>
</div>
<div class="clear flat"></div>
<?php endif; ?>
I have set the maximum posts per page to 5, but the page using this theme template wont show the links. Any ideas? What code can i use to get them.
Thankyou
previous_post_link and next_post_link and the like don't make any sense for Pages. Pages are not ordered by date and time, they are hierarchical. In other words, there is no "next" Page. That doesn't make any sense in that respect.
Your base problem is that you're using a custom Page template to display Posts from a specific Category. This is the wrong way to do it, WordPress has perfectly valid category archives already, which work fine and which expect to display Posts properly.
Long story short: You will never get your Page Template approach to work 100% correctly. It just doesn't work that way. Posts were never meant to be displayed on Pages, and trying to do so only leads to things not working.

WordPress previous_posts_link() leads to a 404 error not found

Below is the code I am using. I have tried everything I have been able to find and it still doesn't work. My permalink structure is /%category%/%postname%/. I believe that the url is correct that it is trying to go to i.e. http://localhost:8888/wordpress/blog/page/2. Annoyingly, the exact same code works on another site I have designed previously.
Could someone point me in the right direction please? Thanks
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=3&showposts=2&paged=" . $paged);
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
<?php if (have_posts()) : ?>
<div id="lefttop"></div>
<div id="blogpoint">
<div id="leftcol">
<?php while (have_posts()) : the_post(); ?>
<div id="leftsquidge">
<h2><?php the_title(); ?></h2><br /><br />
<?php the_excerpt(); ?>
</div>
<div id="rightsquidge">
<?php the_tags( '<p><strong>File under:</strong> ', ', ', '</p>'); ?>
<?php the_time('F jS, Y') ?> by <strong><?php the_author() ?></strong>
</div>
<div style="clear:both;"></div>
<br /><br />
<?php endwhile; ?>
<div class="navigation" style="padding:0px;margin:0px;">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php endif; ?>
<div style="clear:both;"></div>
</div>
</div>
<div id="leftbot"></div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
EDIT
I have answered my own question. It was something I had tried before and wasn't working. You have to create a page, on the dashboard, that uses your category as the template.
Try going to "Reading Settings" in the Wordpress admin, and makes sure the "Blog pages show at most _ posts" is set to 2, or whatever your 'showposts' limit is in your query. Your query_posts is giving you accurate results but Wordpress is paging it based on your reading settings.
If the same code works fine in another site then check your settings for this site. Compare your permalinks settings on both the sites.
Does both the site work on the same environment (Apache or iis)?

Resources