Is it possible to add next and previous post links to a page that is querying posts from one category and displaying one post of that category per page?
This is what I currently have:
<?php query_posts('cat=2&posts_per_page=1'); ?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article class="overlay" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="inner">
<div class="gallery" style="background-image: url(<?php the_field('image'); ?>);">
<div class="close" data-home="<?php echo home_url(); ?>">
<span class="oi" data-glyph="x"></span>
</div>
</div>
<div class="copy">
<h2><?php the_title(); ?></h2>
<?php the_field('news_content'); ?>
Next
</div>
</div>
</article>
<!-- /article -->
<?php endwhile; ?>
You can try with next_post_link()
<?php next_posts_link(); ?>
This seemed to be the only solution that worked for me, if anyone needs a template:
<?php get_header(); ?>
<main role="main">
<!-- section -->
<section>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article class="overlay" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="inner">
<div class="copy">
<h2><?php the_title(); ?></h2>
<?php the_field('news_content'); ?>
<?php the_field('copy'); ?>
<br>
<br>
<?php next_post_link( '%link', 'Next', TRUE, 'post_format' ); ?> | <?php previous_post_link( '%link', 'Previous', TRUE, 'post_format' ); ?>
</div>
</div>
</article>
<!-- /article -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h1><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>
</article>
<!-- /article -->
<?php endif; ?>
</section>
<!-- /section -->
</main>
<?php get_footer(); ?>
Related
Before I decided to post, I read all I could on this site with people of the same issues. Nothing seemed to work... I will explain it the best I can, I am using ACF + Repeater add-on to make A Restaurant Menu. I have Bootstrap loaded to help with making things easier, I want to have 3 columns going across. This is the HTML and PHP side of things.. I am using Bridge Theme, so I had to change the Bootstrap container class to container-acf because it kept going to the Bridge style instead. My end Goal if for it Look Similar To This All help is appreciated Thanks
My guess is I will need a Foreach loop.
`<?php
/*
Template Name: Restaurant Menu Template
*/
get_header(); ?>
<div class="content-fill-in-menu">HERE</div>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content wpb_wrapper container-acf">
<?php if ( have_rows('menu_sections') ):
while ( have_rows('menu_sections') ): the_row(); ?>
<h2 class="section_desc"><?php the_sub_field('section_title'); ?></h2>
<?php if ( have_rows('section_items') ): ?>
<?php while ( have_rows('section_items') ): the_row(); ?>
<article class="lmenu">
<ul>
<li>
<div class="container-acf">
<div class="row">
<div class="col-md-3 item_info">
<img class="dish_pic" src="<?php the_sub_field('dish_pic'); ?>" alt="<?php the_sub_field('dish_name'); ?>">
<h3 class="item_name"><?php the_sub_field('dish_name'); ?></h3>
<p class="item_desc"><?php the_sub_field('dish_description'); ?></p>
<h4 class="price">$<?php the_sub_field('dish_price'); ?></h4>
<span class="separator"></span>
</div>
</div>
</div>
</li>
</ul>
</article>
<?php endwhile; ?>
</table>
<?php endif; ?>
<?php endwhile;
endif; ?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php endwhile; // End the loop. ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<div class="content-fill-in-menu">HERE</div>
<?php get_footer(); ?>`
I think a lot of your problems are in the loop into "section_items" repeater field.
In the "while" loop, you open and close a container and a row for each item, so you don't have your render in columns.
Here in a idea of changes, you need open a row before the start of the loop. I put a counter ($i) to restart a row each 4 items to prevent problems :
<?php if ( have_rows('menu_sections') ): ?>
<?php while ( have_rows('menu_sections') ): the_row(); ?>
<h2 class="section_desc"><?php the_sub_field('section_title'); ?></h2>
<?php if ( have_rows('section_items') ): ?>
<?php $i = 1; ?>
<div class="row">
<?php while ( have_rows('section_items') ): the_row(); ?>
<div class="col-md-3 item_info">
<article class="lmenu">
<img class="dish_pic" src="<?php the_sub_field('dish_pic'); ?>" alt="<?php the_sub_field('dish_name'); ?>">
<h3 class="item_name"><?php the_sub_field('dish_name'); ?></h3>
<p class="item_desc"><?php the_sub_field('dish_description'); ?></p>
<h4 class="price">$<?php the_sub_field('dish_price'); ?></h4>
<span class="separator"></span>
</article>
</div><!-- /.col -->
<?php
if( $i == 4 ){
echo '</div><div class="row">';
$i = 0;
}
$i++;
?>
<?php endwhile; ?>
</div><!-- /.row -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
I created my own template in WordPress, but the loop entries does not work. I would like to entries work on one of the subpages. I also added entries.
This is my code of my subpage. Please help me. I don't know what is wrong. I added a picture under the code.
<?php include 'header.php'; ?>
<main class="subpage-blog">
<div class="subpage-banner">
<div class="container">
<h3>BLOG SIDEBAR</h3>
<div class="breadcrumbs">
</div>
</div>
</div>
<aside class="side-menu col-md-4">
<div class="search">
<h4>Search blog</h4>
<input type="text" value="Search">
</div>
<!-- .search -->
<div class="categories">
<h4>Blog Categories</h4>
<ul class="categories-blog-ul">
<li>Inspirtation</li>
<li>Work</li>
<li>Tech</li>
<li>Creative</li>
</ul>
</div>
<!--.categories-->
<div class="recent-posts">
<h4>Recents posts</h4>
<ul>
</ul>
</div>
<!-- .recent-posts-->
<div class="tags-spot">
<h4>Tags</h4>
<div class="tag"></div>
</div>
<!-- .tags-spot-->
</aside>
<!-- .side-menu-->
<article class="content">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="news-box">
<div class="news-list-content">
<a href="">
<h3><?php the_title(); ?></h3>
<?php the_content('czytaj dalej'); ?>
</a>
</div>
<!-- .news-list-content-->
<div class="image-box-news">
<img src="<?=get_template_directory_uri(); ?>/images/ikona-wpisu.png" alt="" />
</div>
</div>
<!-- .news-box-->
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<!-- .container-->
</article>
<!-- .content-->
</main>
<?php include 'footer.php'; ?>
The problem is that:
if (have_posts()) : while (have_posts()) : the_post();
is using the current page's have_posts query.. That means that it probably will just show whatever that current page template's content would be.
Instead, you'll want to create an entirely new query object and call these functions on it like so:
<?php
$the_query = new WP_Query( array('posts_per_page' => 10 ) ); //Create our new custom query
if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="news-box">
<div class="news-list-content">
<h3><?php the_title(); ?></h3>
<?php the_content('czytaj dalej'); ?>
</div>
<!-- all your other markup goes here.... -->
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php wp_reset_postdata(); //Restore the original postdata for this page, if needed later ?>
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/
Any ideas why the_content() is returning null? When I use $page->content in the same place, this returns the correct value from database.
<?php get_header(); ?>
<div id="main" class="clearfix" role="main">
<?php $attachments = new Attachments( 'attachments' ); ?>
<?php if( $attachments->exist() ) : ?>
<div id="myCarousel" class="carousel">
<!-- Carousel items -->
<div class="carousel-inner">
<?php $i = 0; while( $attachments->get() ) : $i++; ?>
<div class="<?php if($i == 1){ echo 'active'; } ?> item item-<?php echo $attachments->id(); ?>">
<?php echo $attachments->image( 'featured-home' ); ?>
<?php if($attachments->field('caption')){ ?>
<div class="carousel-caption">
<?php echo $attachments->field('caption'); ?>
</div>
<?php } ?>
</div>
<?php endwhile; ?>
</div>
<?php if($i > 1) { ?>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
<?php } ?>
<div class="carousel-overlay icons">
Facebook
Twitter
YouTube
RSS Feed
</div>
</div>
<?php endif; ?>
<?php if (have_posts()) : ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(array('row-fluid','clearfix')); ?> role="article">
<div id="content" class="span8">
<header>
<h1><?php echo get_post_meta($post->ID, 'custom_tagline' , true);?></h1>
<?php the_content(); ?>
</header>
<section class="post_content hasSidebar">
<?php query_posts('showposts=10');
while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<header>
<?php the_post_thumbnail( 'wpbs-featured' ); ?>
<h2><?php the_title(); ?></h2>
<?php if(get_comments_number() >= 1): ?>
<a href="<?php comments_link() ?>" title="<?php comment_date() . ' - ' . comment_excerpt(); ?>" <?php comment_class(); ?>><?php comments_number(); ?></a>
<?php endif; ?>
<p class="meta"><?php _e("Posted", "bonestheme"); ?> <time datetime="<?php echo the_time('Y-m-j'); ?>" pubdate><?php the_date(); ?></time> <?php _e("by", "bonestheme"); ?> <?php the_author_posts_link(); ?> <span class="amp">&</span> <?php _e("filed under", "bonestheme"); ?> <?php the_category(', '); ?>.</p>
</header>
<section class="post_content">
<?php the_excerpt(); ?>
</section>
<?php if(has_tag()): ?>
<footer>
<p class="tags"><?php the_tags('<span class="tags-title">' . __("Tags","bonestheme") . ':</span> ', ' ', ''); ?></p>
</footer>
<?php endif; ?>
</article>
<?php endwhile ?>
<?php content_paging_nav('nav-below'); ?>
</section>
<footer>
<p class="clearfix"><?php the_tags('<span class="tags">' . __("Tags","bonestheme") . ': ', ', ', '</span>'); ?></p>
</footer>
</div>
<?php get_sidebar('sidebar2'); ?>
</article>
<?php else : ?>
<article id="post-not-found">
<header>
<h1><?php _e("Not Found", "bonestheme"); ?></h1>
</header>
<section class="post_content">
<p><?php _e("Apologies, but no results were found. Perhaps searching will help find a related post.", "bonestheme"); ?></p>
</section>
<footer>
</footer>
</article>
<?php endif; ?>
</div> <!-- end #main -->
<?php //get_sidebar(); // sidebar 1 ?>
<?php get_footer(); ?>
You're not in a WP loop. You should call e.g. the_post() to set up WP's slew of globals.
Beware of nested WP loops, too. There used to be a gazillion bugs related to them, and there might still be a few lying around just waiting to bite.
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(); ?>