Im just wondering is it possible to use the pagination_links() function in a foreach loop in wordpress?
When I try it nothing happens, I have looked around and it seems this is a little trickier than I was expecting...
<?php
$args = array( 'numberposts' => 6, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="events">
<div class="newslistingblock">
<div class="newslistingblockheader"><p><?php the_title(); ?></p>
</div>
<div class="newslistingblockthumbnail">
<?php echo get_the_post_thumbnail( $post_id, 'news-thumb', $attr ); ?> </div>
<div class="newslistingexcerpt">
<?php the_excerpt( ); ?> </div>
</div>
</div>
<?php endforeach; ?>
Im basically looking for basic pagination, with "next", "prev" and numbers.
Any help on this would be great thanks.
EDIT:
I have decided to change the code to this to suit wordpress...
<?php
query_posts( 'posts_per_page=5' );
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<!-- Do suff -->
<div class="events">
<div class="newslistingblock">
<div class="newslistingblockheader"><p><?php the_title(); ?></p>
</div>
<div class="newslistingblockthumbnail">
<?php echo get_the_post_thumbnail( $post_id, 'news-thumb', $attr ); ?> </div>
<div class="newslistingexcerpt">
<?php the_excerpt( ); ?> </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 endif; ?>
Why are you using foreach instead of while?
The default loop with pagenation should look like this (should work with foreach as well):
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- Do suff -->
<?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 endif; ?>
This just shows the next and previous link, but if you want pagination with numbers, I would suggest the great plugin: Wp-Pagenavi.
Good luck!
EDIT:
The error you are experiencing is that you haven't set the paged variable correctly. You need to do the following:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=5&paged=' . $paged);
?>
Then everything should work.
You can find more information in the codex: http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
Related
I'm building a theme and on my category.php page I want to show several full posts (let's say 3, but need to be able to change this to 2 or 1 easily), and then the rest of the posts in the category as title links.
I have quite a bit of HTML in my loop for styling my posts and adding custom fields so sorry about all the code, but this is what my category.php page looks like now. I've tried a few things that haven't worked so have edited this to show my original code which just has a normal list of posts. I'm somewhat new to editing The Loop so would appreciate as much explanation/clarity as possible.
<?php
/**
* The template for displaying Category Archive pages.
*/
get_header(); ?>
<div id="primary" class="<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>">
<div id="feature-container" class="full-width-container">
<div class="full-width-container content-page" id="tagline-wrapper">
<div id="left-black"></div>
<div class="page-width-container">
<div id="tagline-box">
<h1 class="category-title">Transactions</h1>
</div>
</div>
</div>
</div>
<div id="content-wrapper">
<div id="project-menu" class="page-width-container">
<?php wp_nav_menu( array( 'theme_location' => 'project-types' ) ); ?>
</div>
<div id="content" role="main" >
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="story-container" class="module-container">
<div class="our-story">
<div class="story-image">
<?php
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="story-text">
<article class="post" id="post-<?php the_ID(); ?>">
<div class="entry-container">
<h2><?php the_title(); ?></h2>
<div class="project-details">
<p><span class="details-location"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-location', true);
wp_reset_query();
?></span><br />
<span class="details-funding"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-type', true);
wp_reset_query();
?> | <?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-source', true);
wp_reset_query();
?></span><br />
<span class="details-value"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-value', true);
wp_reset_query();
?></span></p>
</div>
<div class="entry">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => __('Pages: ','html5reset'), 'next_or_number' => 'number')); ?>
</div>
<?php edit_post_link(__('Edit this entry','html5reset'), '<p>', '</p>'); ?>
</div>
</article>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div><!-- #content -->
</div>
</div><!-- #primary -->
<?php get_footer(); ?>
You can achive above thing using following code:
First you have to loop all post and and put counter when it reach more then 2 its stop to print a content.but title will be there always.
<?php $countPost=1;?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2 id="post-<?php the_ID(); ?>">
<?php the_title(); ?></h2>
<?php if($countPost>2) : /*Condition for Content*/
the_content();
endif;
?>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft">
<?php posts_nav_link('','','« Previous Entries') ?>
</div>
<div class="alignright">
<?php posts_nav_link('','Next Entries »','') ?>
</div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
<?php endif; ?>
</div>
For more details please refer :
https://codex.wordpress.org/The_Loop_in_Action
I figured out a bit of a workaround solution on my own, although it relies on using plugins/widgets which isn't what I'd prefer.
I simply set the Reading settings to display 2 posts, and then below the Loop I added a widget area and used the Recent Posts Extended widget to display a list of titles/links. This widget allows you to skip a certain amount of posts in the list, so I set it to start at post #3. There was no option to show posts from the current category only, so I had to use the Widget Context plugin as well and make individual widgets with a specific category to show on each corresponding category page. As I said, a bit of a convoluted solution, but the end result is exactly what I wanted to achieve.
I am trying to get latest posts to display through a template page i am building for pages, the loop is not running the latest post only one page
ok, I have a simple loop that gets latest post
my loop
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
and content.php
<div class="blog-post">
<h2 class="blog-post-title">
<?php the_title(); ?>
</h2>
<p class="blog-post-meta">
<?php the_date(); ?>by <?php the_author(); ?>
<a href="<?php comments_link(); ?>">
<?php printf(_nx('One Comment', '%1$s Comments', get_comments_number(), 'comments title', 'textdomain'), number_format_i18n(get_comments_number())); ?>
</a>
</p>
<?php if ( has_post_thumbnail() ) {?>
<div class="row">
<div class="col-md-4">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="col-md-6">
<?php the_excerpt(); ?>
</div>
</div>
<?php } else { ?>
<?php the_excerpt(); ?>
<?php } ?>
</div>
when i run the loop in index.php i get my latest blog post, perfect.
however, i am building a template page, i try and include the loop in this page, i just get one page (not all posts).
my template
<div class="row">
<div class="col-sm-12">
// content bar
<?php get_template_part('advicecentre_bar', get_post_format()) ?>
// cmd driven content
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content_page', get_post_format());
endwhile; endif;
?>
// recent post
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
If you are using multiple loops on the same page, you must use rewind_posts() like so:
<div class="row">
<div class="col-sm-12">
// content bar
<?php get_template_part('advicecentre_bar', get_post_format()); ?>
// cmd driven content
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content_page', get_post_format());
endwhile; endif;
?>
<?php rewind_posts(); ?>
// recent post
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
This "resets" the loop to it's original state and allows you to look through the posts again. In your original code you scan through all the posts, and then in your second loop scan through nothing, as you have already scanned through all the posts!
Hmm I have found this solution using a for each rather than the while loop seems to work, but im not sure if its the best way around.
<ul>
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
}
wp_reset_query();
?>
</ul>
UPDATE
<?php
$args = array('numberposts' => 5);
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
$excerpt = wp_trim_excerpt($recent['post_content']);
$permalink = get_permalink($recent["ID"]);
$title = esc_attr($recent["post_title"]);
$thumbnail = get_the_post_thumbnail($recent["ID"], 'thumbnail');
echo '<li><a href="' . $permalink . '" title="Look ' . $title . '" >' . $thumbnail . $title . '</a></li>';
echo $excerpt;
}
?>
So I'm trying to pull only the latest sticky post from WordPress and only 1. I am using 'showpost=1' but for some reason if I have two posts tagged as sticky both show up?
<h2>Breaking News</h2>
<?php
query_posts('posts_per_page=1');
if (have_posts()) {
while (have_posts()) : the_post();
if ( is_sticky() ) : ?>
<div class="small-12 medium-6 large-4 columns">
<div class="img-holder">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('sticky-hp-thumbnails');
}
?>
<?php if( get_field('sticky_sub_heading') ): ?>
<div class="tag">
<p>
<?php the_field('sticky_sub_heading'); ?>
</p>
</div>
<?php endif; ?>
</div>
</div>
<div class="small-12 medium-6 large-4 columns">
<h3><a href"<?php the_permalink() ?>">
<?php the_title(); ?>
</a></h3>
<?php if( get_field('sticky_date') ): ?>
<p class="sticky-date">
<?php the_field('sticky_date'); ?>
</p>
<?php endif; ?>
<p>
<?php the_field('sticky_summary'); ?>
</p>
Read More </div>
<?php endif;
endwhile;
}
wp_reset_query();
?>
Where am I going wrong in the above code?
Use posts_per_page instead of "showposts"
i.e.
query_posts( 'posts_per_page=1' );
Source:
https://codex.wordpress.org/Function_Reference/query_posts
Updated: Adding WP_QUERY code to fetch latest sticky post:
<?php
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="post">
<h3><?php echo get_the_title(); ?></h3></a>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
The easiest solution to solve (workaround) your problem is to remove the while loop. This way you'll print only one.
Of course, this is sub-optimal since other pages may be fetched and unused; you could try using posts_per_page=1 or post_limits=1 to see if solve the issue.
I have WordPress onepage and I displayed posts from specific category in one of page.
When I click to link with permalink href, I am redirecting to home page with added /post-name/ to url, but no to post page. I have index.php and single.php.
I have this index.php:
<?php
query_posts(array(
'post_type' => 'page',
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'menu_order'
));
$tpl_parts = array(
'5' => 'about',
'7' => 'team',
'76' => 'tech',
'81' => 'services',
'101' => 'contact',
);
?>
<?php get_header('home'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if(array_key_exists($post->ID, $tpl_parts)) : ?>
<?php get_template_part('template-parts/'. $tpl_parts[$post->ID], 'template'); ?>
<?php else: ?>
<section id="<?php echo $post->post_name; ?>">
<div class="container">
<div class="row">
<?php the_content(); ?>
</div>
</div>
</section>
<?php endif; ?>
<?php endwhile; else : ?>
<?php endif; ?>
<?php get_footer(); ?>
This code show all pages in index.php by template part, it's working.
When I added to services page, a few posts like this:
<section id="services" class="services-section">
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2 class="text-left">Services</h2>
</div>
</div>
<?php $inner_query = new WP_Query( 'category_name=services' ); ?>
<?php if ( $inner_query->have_posts() ) : while ( $inner_query->have_posts() ) : $inner_query->the_post(); ?>
<div class="row box-service">
<div class="col-sm-6 col-xs-12">
<?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<div class="col-sm-6">
<h3><?php the_title(); ?></h3>
<p class="intro"><?php echo the_field('short_caption'); ?></p>
More
</div>
</div>
<?php endwhile; else: endif; wp_reset_postdata(); ?>
</div>
</section>
This code isn't working, becouse when I want to click on link and go to post, website is refreshing with url localhost/mywebsite/name-of-post/ but I want to redirect to post page. I have a single.php file:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
</div>
</section>
<?php endwhile; else: endif; ?>
<?php get_footer(); ?>
What's wrong ? How can I fix that ? My theme ignore files like page.php or single.php
Thanks for help.
Are You trying to use Wp_Query to single.php too ?
And maybe try to change posts_per_page in top index to number of your pages instead of -1.
you can try this for display single post page.
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
</h2>
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
Ok, after a few hours spending on this problem, I tried to find why page.php and single.php is ignored.
Polylang plugin - after turned it off. Everything working fine.
Thanks everybody for help me.
Not sure if this pertains specifically to Advanced Custom Fields, but I'll keep this short. I'm relatively new to PHP and want to know the best way of approaching this task. Also, this is a WordPress (4.1.1) environment.
I'm sending a query for all post types with an associated category type to pull in and display. If the query can find posts, it produces an <article> block with additional html for each result. Not to complicated right?
Now, I want to create a pagination element if/when the query produces greater than 8 results.
Environment with the front-end of the query results can be found:
http://test-hdwg.pantheon.io/news/
Template being utilized for this page (news.php) contains the following:
<?php /* Template Name: News */ ?>
<?php get_header(); ?>
<main id="primary" class="content-area">
<div class="jumbotron">
<div class="row">
<div class="intro">
<p><?php the_field('news_heading') ?></p>
</div>
</div>
</div>
<div class="news">
<div class="row news-articles">
<div class="column-wrapper">
<div class="small-4 columns news-left-column">
<div class="select-dropdown">
<button href="#" data-dropdown="dropdown-items" aria-controls="dropdown-items" aria-expanded="false" class="dropdown">Select a news category</button>
<ul id="dropdown-items" data-dropdown-content class="f-dropdown" aria-hidden="true" tabindex="-1">
<?php $args = array(
'exclude' => '',
'title_li' => __( '' ),
'show_option_none' => __( '<li>No categories</li>' ),
'taxonomy' => 'category',
'child_of' => 2,
'current_category' => 0
); ?>
<?php wp_list_categories($args); ?>
<li class="cat-item cat-item-all-news">All News
</ul>
</div>
<div class="news-block">
<span class="news-block-heading">Email Newsletters</span>
<div class="news-inner-block">
<?php if( have_rows('newsletters_list') ): ?>
<?php while( have_rows('newsletters_list') ): the_row(); ?>
<span class="year"><?php the_sub_field('newsletter_year'); ?></span>
<?php if( have_rows('newsletter_resource') ): ?>
<ul>
<?php while( have_rows('newsletter_resource') ): the_row(); ?>
<li><?php the_sub_field('newsletter_title'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</div>
<div class="small-8 columns news-right-column">
<?php query_posts( 'post_type=post&cat=' ); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article class="large-6 columns ui-article-block">
<h3><?php echo get_the_date( 'd M Y' ); ?></h3>
<hr/>
<h2><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></h2>
<h1><?php the_title(); ?></h1>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 175) . "..."; // set a character cut-off at 175 characters
?>
</p>
Read More
</article>
<?php endwhile; ?>
<?php else : ?>
<div class="large-8 columns">
<p class="lead">Well this is embarrassing. There are currently no news articles.</p>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>
</div>
</main>
<?php get_footer(); ?>
Just to be clear, this is not an issue, but more-so a request from more experience developers on how to approach a task utilizing Advanced Custom Field elements (if that matters).
Any help would be greatly appreciated!
EDIT
This edit is after clarification from OP on nature of the question.
The tutorial here, explains how to handle numeric navigation in posts at a beginner level.