WordPress - pagination on template page? - php

I'm building my site with WordPress templates for granular control over how each of them looks.
I have my loop on my news page to pull in posts from, well, posts.
Working fine, or so I thought.
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<div id="main-content">
<?php get_sidebar(); ?>
<?php query_posts ("posts_per_page=4"); ?>
<?php if (have_posts()) : while ( have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content(); ?>
</div>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
Posted in <?php the_category(', ') ?> |
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
</div>
</div>
<?php endwhile; endif; ?>
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
</div> <!-- end div main-content -->
<?php get_footer(); ?>
The older/newer entries show up, but after clicking on them it just takes me to the same page/newest 4 posts.
How do I get the pagination working with template pages like this?

You need to add the paged parameter to query_posts() to paginate the results:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=3&paged=' . $paged);
?>
http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »') ?></div>
Just put this line in your page.

Related

Wordpress - When clicking on posted in link, categories aren't filtering

I have a new blog I am building here https://www.storageworks.biz/blog
When on a single post page such as https://www.storageworks.biz/blog/6-must-have-tips-for-moving-into-your-first-apartment/ when I go to the bottom and click on the posted in link to view other posts in the same category, it shows me all posts in place of only the one category type.
How do I set it up to filter out the results?
My code is as follows,
<?php get_header(); ?>
<div id='page_wrapper'>
<div id='page'>
<div id='slider_wrapper' >
<?php get_sidebar(); ?>
<div id='wrapper_right'>
<div id="content">
<div id="nav-above" class="navigation">
<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">«</span> %title' ) ?></div>
<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">»</span>' ) ?></div>
</div><!-- #nav-above -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content(); ?>
</div>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php //comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php //comments_template(); ?>
<?php endwhile; endif; ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">«</span> %title' ) ?></div>
<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">»</span>' ) ?></div>
</div><!-- #nav-below -->
</div>
<div style="clear:both;"></div>
</div>
<div class="clear"></div>
</div>
</div>
<div class="clear"></div>
<?php get_footer(); ?>
I'm running worpress version 4.5.3.
My template parts are made up of:
Header
index
single
sidebar
footer
functions
Thanks!

Single.php showing blank page

I'm trying to build a theme of my own and uploading it to Wordpress, but my single.php file it's not working at all. It's just showing a blank page. I've tried so many things to get it to work, but now I don't know what to do anymore. This is my php-file for the blog page:
<?php
/**
*Template Name: Blog Posts
*/
get_header('header4'); ?>
<section id="headerbox">
<header>
<h2 class="referensrubrik">Nyheter</h2>
</header>
<p class="referenstext">Det senaste från AL Konsult.</p>
</section>
<main id="blog">
<?php // Display blog posts on any page # http://m0n.co/l
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query('showposts=5' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article id="blogpost" id="post-<?php get_the_ID(); ?>" <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<h5><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></h5>
<?php the_excerpt(); ?>
<hr>
</article>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
<div class="next"><?php previous_posts_link('Newer Posts »'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
</main>
<?php get_footer(); ?>
Now my single.php just looks like this (I've tried the loop, but it's just not working...):
<?php
/**
* The Template for displaying all single posts.
*/
get_header('header3'); ?>
<section id="headerbox">
<header>
<h2 class="referensrubrik">Rubrik</h2>
</header>
<p class="referenstext">Text</p>
</section>
<?php
error_reporting(-1);
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
</article>
<?php endwhile; ?>
<?php get_footer(); ?>
What am I doing wrong!?
I fixed it! The single.php code that works for me looks like this:
<?php
/**
* The Template for displaying all single posts.
*/
get_header('header3'); ?>
<section id="headerbox">
<header>
<h2 class="referensrubrik">Rubrik</h2>
</header>
<p class="referenstext">Text</p>
</section>
<?php
$post = $wp_query->post;
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article id="blogpost" id="post-<?php get_the_ID(); ?>" <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<h5><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></h5>
<?php the_content(); ?>
<hr>
</article>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Äldre inlägg'); ?></div>
<div class="next"><?php previous_posts_link('Nyare inlägg »'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Äldre inlägg'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
Your template tag is outside of the block.
<!-- /*
Template Name: Blog Posts
*/ -->
<?php
It should be:
<?php
/*
Template Name: Blog Posts
*/
Make sure there are no blank spaces or carriage returns outside the tags (especially in WP page templates): The spaces/carriage returns will cause the page not to render by throwing an exception (which you are likely not seeing because you dont have reporting enabled).
Specifically I am referring to this:
<!-- /*
Template Name: Blog Posts
*/ -->
It needs to be inside the opening PHP tag, and there should be no empty lines at the top of the page.
It should look like (no empty line breaks):
<?php
/**
* The Template for displaying all single posts.
*/
require_once('../../../wp-load.php');
get_header('header3'); ?>
Remove the following line from your code:
require_once('../../../wp-load.php');

WordPress custom post previous and next link not working

I created custom theme with WordPress and bootstrap and everything works fine except previous and next link in singe.php.
Here is my code:
<?php get_header(); ?>
<article>
<div class="container single-project">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<div class="row">
<div class=" col-md-4 col-lg-4 pull-right">
<header class="entry-header">
<h1 id="post-<?php the_ID(); ?>"><?php the_title();?></h1>
<hr>
</header><!-- .entry-header -->
</div>
<div class=" hidden-md hidden-lg"><hr></div>
<div class="col-md-4 col-lg-4"><?php the_content(); ?></div>
<?php previous_posts_link() ?>
<?php next_posts_link() ?>
</div> <!-- end of row -->
</div> <!-- end of container -->
<?php endwhile; endif; ?>
</article>
<?php get_footer(); ?>
Thanks in advance.
You have to move
<?php previous_posts_link() ?>
<?php next_posts_link() ?>
after
</article>
preferably inside some div so you can style it, like
<div class="navi">
<?php previous_posts_link() ?>
<?php next_posts_link() ?>
</div>
Found solution:
<?php previous_post('« « %', 'Previous Post', 'yes'); ?> |
<?php next_post('% » » ', 'Next Post', 'yes'); ?>
http://wpsites.net/web-design/previous-next-single-post-navigation-links-wordpress/

Why isn't my Wordpress next_posts_link not working with exclude category loop?

I'm trying to setup a loop in Wordpress that will show all posts from one category, which is working just fine, however my problem is that my 'next_posts_link' and 'previous_posts_link' doesn't work. They navigate between pages just fine but the results are the same as the first page all the time.
<?php get_header(); ?>
<div id="main" role="main">
<?php
if (is_home()) {
query_posts("cat=-6");} //Exclude work posts (cat 6) from the news page
?>
<div class="inner">
<h1><?php trim(wp_title("")); ?></h1>
<?php include ('sidebartwo.php'); ?>
<section class="main-wrap twocol news">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article class="box-style">
<time><?php the_time('M d') ?><span><?php the_time('Y') ?></span></time>
<h2><a href="<?php the_permalink()?>" title="<?php the_title(); ?>"><?php the_title(); ?>
</a></h2>
<?php the_content(''); ?>
</article>
<?php endwhile; ?>
<div class="next-prev-wrap">
<!-- This is what isn't working properly -->
<span class="next"><?php next_posts_link( 'Older posts', $post->max_num_pages ); ?></span>
<span class="prev"><?php previous_posts_link( 'Newer posts', $post->max_num_pages ); ?>
<!-- /end -->
</span>
</div>
</section>
<?php endif; ?>
</div> <!-- /inner -->
</div> <!-- /main -->
<?php get_footer(); ?>
I don't think I'm using the right syntax, in fact according to the WP codex page, I don't even think my next/prev links are able to work the way I want it to. How should I approach this?
Fixed myself. This post is now resolved. After much (and I mean much) Googling, I found this article which solved my problem: http://www.dynamicwp.net/articles-and-tutorials/pagination-problem-when-excluding-certain-category-from-blog-main-page/
For reference my new code now looks like this:
<?php get_header(); ?>
<div id="main" role="main">
<?php
if (is_home()) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=-6&paged=$paged");
}
?>
<div class="inner">
<h1><?php trim(wp_title("")); ?></h1>
<?php include ('sidebartwo.php'); ?>
<section class="main-wrap twocol news">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article class="box-style">
<time><?php the_time('M d') ?><span><?php the_time('Y') ?></span></time>
<h2><?php the_title(); ?> </h2>
<?php the_content(''); ?>
</article>
<?php endwhile; ?>
<div class="next-prev-wrap">
<span class="next"><?php next_posts_link( 'Older posts', $post->max_num_pages ); ?></span>
<span class="prev"><?php previous_posts_link( 'Newer posts', $post->max_num_pages ); ?></span>
</div>
</section>
<?php endif; ?>
</div> <!-- /inner -->
</div> <!-- /main -->
<?php get_footer(); ?>

Wordpress search results

I have created a searchfrom.php for wordpress but then it's giving me a false returns you can try the search yourself here
Here is the code for my search form
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<input type="text" class="form-text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" class="goField" />
</form>
Here is the code of my search.php page
get_header(); ?>
<div id="BodyWrap">
<div id="mainCont">
<?php get_sidebar(); ?>
<div id="mainCopy">
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Search Results</h2>
<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 while (have_posts()) : the_post(); ?>
<div <?php post_class() ?>>
<h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>
<small><?php the_time('l, F jS, Y') ?></small>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</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">No posts found. Try a different search?</h2>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
It does do the search but there are this "at | Uncategorized | No comment" which is not even part of the search term.
Oh, I see. You want to know why it lists categories even if they don't match the search term?
Well, that's the default WordPress behavior. I guess you could change it, but I don't see why, actually.
It is the
Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
part in your search.php that displays the categories and comments count. However, if you remove the code above, the categories and comments count will not be shown (even if the categories matches your search term).
You may replace the code above with this code, if you only want to display the categories if they are the same as the search term:
<?php
foreach(get_the_category() as $cat){
if(strtolower($cat->cat_name)==strtolower($_GET['s'])){
$match = true;
}
}
if($match){ echo 'Posted in '; }
foreach(get_the_category() as $cat){
if(strtolower($cat->cat_name)==strtolower($_GET['s'])){
echo $cat->cat_name;
echo ', ';
}
}
edit_post_link('Edit', '', ' | ');
comments_popup_link('No Comments »', '1 Comment »', '% Comments »');
?>
It should produce something like this, if the categories match the search term:
Posted in Category That Matches, Edit | 1 Comment
If the categories don't match the term:
Edit | 1 Comment
If you don't want the comments count (or Comments Off) to display, remove the comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); line from my code.
It does do the search but there are
this "at | Uncategorized | No comment"
which is not even part of the search
term.
You'll have to edit your page.php or single.php to exclude the search page from showing the Comments/Categories.
Maybe change searchform.php to this?
Remove the line that's wrapped in . That will get rid of the "Posted in Uncategorized | Comments Off".
The reason it show's is because it is the meta information for your search result. It will be displayed in your results even if it wasn't the search term because it is associated with it.
All in all, your search.php page should look like this:
get_header(); ?>
<div id="BodyWrap">
<div id="mainCont">
<?php get_sidebar(); ?>
<div id="mainCopy">
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Search Results</h2>
<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 while (have_posts()) : the_post(); ?>
<div <?php post_class() ?>>
<h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>
<small><?php the_time('l, F jS, Y') ?></small>
</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">No posts found. Try a different search?</h2>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>

Categories