Y try paginate results of wordpress and use this script , the problem it´s when i go paginate , the number of pages show right but if go the links of paginate always show the same content no change in each number of pages
<?php
global $post;
if (have_posts()) : ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'cat=36&posts_per_page=4' );
while (have_posts()) : the_post(); ?>
<div class="cols_posts">
<?php
$imagen = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
$ruta_imagen = $imagen[0];
echo '<img src="'.$ruta_imagen.'">';
?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="extracto"><?php the_excerpt(); ?></div>
</div>
</div>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
In tye page number one must show the content for that page , in the page 2 the same , etc but always show the same page , the first page content
Regards
The problem is here:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'cat=36&posts_per_page=4' );
You are getting $paged variable correctly but you are not using it in a query.
So add it to the query like this:
query_posts( 'cat=36&posts_per_page=4&paged='.$paged );
You can use a function for this. For that, visit this page
http://design.sparklette.net/teaches/how-to-add-wordpress-pagination-without-a-plugin/
Related
I have one custom loop in a custom homepage in WordPress that is pulling 4 posts with some banners in middle. All is working fine, however pagination always shows same posts. Is there a way for me to add pagination to this custom loop?
My index.php code:
<?php
query_posts(array(
'post_type' => 'post',
'showposts' => 4,
) );
?>
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 3) : ?>
<!-- banners -->
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<?php else : ?>
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<!-- banners -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<p align="center"><?php posts_nav_link(); ?></p>
Inside your query_post array add this line:
'paged' => ( get_query_var('paged') ) ? get_query_var('paged') : 1,
and in settings -> reading set 4 posts per blog page.
I have my own custom WordPress theme and I am building out the 'Latest News' section at the moment. I have a custom field for my page header image and alt tag for each page on my site, I am using the following code to display those custom fields on each page:
<section class="page-heading">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endwhile; endif; ?>
</section>
Which works fine. But on my 'Latest News' page all the custom fields from all the news stories (posts) are being displayed, so for example if I have 3 post displayed per page then I am getting 3 images and 3 alt tag displayed.
In my settings I have my 'Posts Page' set to 'Latest News'.
Is there a way I can only have 1 image and 1 alt tag displayed and not all 3?
Thanks.
EDIT
I've added an image to better explain... The images that span the screen are from the news stories and not the custom field for the 'Latest News' page.
I don't know if I understand your question, but you want your first post show the image and the rest not?
I guess you have to write something like this:
<section class="page-heading">
<php $show_first_image = true; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if($show_first_image): $show_first_image = false; ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endif; ?>
<?php endwhile; endif; ?>
I think I understand your problem, your latest news is also a Page (post type), which has the header image.
Your index file/archive is like this:
<?php
get_header();
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
the problem is, got its items loaded before the get_header(), where your header image will be showed.
What you do is something like this:
<?php
global $wp_query;
$original_query = $wp_query;
$wp_query = null;
$page = get_query_var( 'pagename' );
$wp_query = new WP_Query( array('post_type' => 'page', 'pagename' => $page ) );
get_header(); // in the get_header() you will probably have the <section class="page-heading">
$wp_query = null;
$wp_query = $original_query;
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
I hope this is your answer to your question.
I got around this by creating a new template for my news page and using WP_Query inside that template to bring in the latest news stories. I also unset 'Posts Page' in my reading settings as that's what was causing all the issues.
I'm not sure if that is the most efficient way of doing things but it worked for me.
VVC, I appreciate your help and effort.
<!-- WP Query Arguments -->
<?php
$args = array(
'page_id' => '103'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->
<!-- WP Query Arguments -->
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '5'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="news-story">
<div class="news-heading">
<h5><?php the_title(); ?></h5>
<p class="publish-date"><strong>Posted on:</strong> <?php the_time('j M, Y'); ?></p>
</div>
<?php the_excerpt(); ?>
<p>Full Story <i class="fa fa-angle-double-right"></i></p>
</div>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->
I use the normal pagination method in wordpress
<?php $i = 1 ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => "2", 'paged' => $paged, 'cat' => 26 );
query_posts($args);
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="art_1">
<h5><?php echo $i ?>. <?php the_title(); ?></h5>
<?php $i++; ?>
</div>
<?php endwhile; ?>
<div class="er">
<?php next_posts_link(); ?>
</div>
<div class="err">
<?php previous_posts_link(); ?>
It works fine but the problem is this pagination I use for listing out articles in a particular category ,now I have another category of articles in same page which needs pagination too.pagination of 1st should no affect the other and vice versa. Can anyone suggest me what should I do?
Use this hook after pagination..
<?php wp_reset_query(); ?>
Similar questions have been asked but none seem to apply to my situation here.
I have two custom post-types built into a WP site. I created single-first_one.php to capture each of the first post-types. It works perfectly and the pagination I added works also.
The second post-type, I created single-second_one.php, and on this 'single' page, I'm displaying the current post, as well as below, the most recent 6 posts. Everything is showing as it should except my pagination links (in the single-second_one.php). The code is almost identical to that of the first 'single' template so I don't understand why it's not working. Any help?
<?php get_header(); ?>
<h1>Events</h1>
<section id=featured_post>
<?php $paged = (get_query_var('paged') && get_query_var('paged') > 1) ? get_query_var('paged') : 1; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h5 class=previous><?php previous_post_link('%link'); ?></h5>
<h5 class=next><?php next_post_link('%link'); ?></h5>
<article class="post-<?php the_ID(); ?>">
<div>
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
</div>
<h2><?php the_title(); ?></h2>
<h3><?php the_time('l F j'); ?></h3>
<?php the_content(); ?>
</article>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</section>
<h1>Upcoming Events</h1>
<section id=other_posts>
<?php
$args = array('post_type' => 'event', 'showposts' => 6, 'order' => 'ASC', 'post_status' => 'future');
$loop = new WP_Query($args);
if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();?>
<article class="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<h3><?php the_time('l F j'); ?></h3>
<?php html5wp_excerpt('events_page_listing'); ?>
</article>
<?php endwhile; else: ?>
<?php endif; ?>
</section>
I've gone to Options -> Permalinks, cached everything, taken out the second half section that calls the six recent posts, taken out single-first_one.php altogether, and nothing works. The 's for pagination on the second single page are empty.
EDIT
I've tried moving the next/previous calls around, after the endwhile, between endwhile and else, etc. Nothing is working. It's so bizarre because on the other single.php page, everything is setup the EXACT same way and is showing the next/prev posts links perfectly.
EDIT
I added the pagination argument but it's still not working.
You do not have any pagination arguments in the code ..
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged
);
or
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=3&paged=' . $paged);
or also something like
$paged = (get_query_var('paged') && get_query_var('paged') > 1) ? get_query_var('paged') : 1;
HI I have edited and make a template page of responsive theme to make some thumbnails of the features pics of the posts.
ALl it's okay I can see them, but even if the posts are 9 I could see only 5. If I add one ony I see the new one, there is something like "show only latest 5 posts" but I can't understand WHERE!
get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part( 'loop-header' ); ?>
<?php responsive_entry_before(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php responsive_entry_top(); ?>
<?php get_template_part( 'post-meta-page' ); ?>
<div class="post-entry">
<?php the_content(__('Read more ›', 'responsive')); ?>
<?php wp_link_pages(array('before' => '<div class="pagination">' . __('Pages:', 'responsive'), 'after' => '</div>')); ?>
</div><!-- end of .post-entry -->
(this is my added code)
<ul>
<?php
$posts = get_posts();
foreach($posts as $post) : setup_postdata($post);
?>
<li><div class="fotoBoxContent"><a class="fotoBox" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); the_title(); ?></a></div></li>
<?php endforeach; ?>
</ul>
<?php responsive_entry_bottom(); ?>
</div><!-- end of #post-<?php the_ID(); ?> -->
<?php responsive_entry_after(); ?>
<?php responsive_comments_before(); ?>
<?php comments_template( '', true ); ?>
<?php responsive_comments_after(); ?>
<?php
endwhile;
get_template_part( 'loop-nav' );
else :
get_template_part( 'loop-no-posts' );
endif;
?>
Try adding query_posts( 'posts_per_page=NUMBER_GOES_HERE' ); immediately before the <?php while (have_posts()) : the_post(); ?>
Replacing NUMBER_GOES_HERE with the amount of posts you want displayed. Use -1 to show all posts
Also within Wordpress itself Settings->Reading has a field where you can set Blog pages show at most
I'm still not sure what exactly you want to achieve but if you just want to have thumbnails of posts that are in the main loop, then you do not need to do additional query.
All you need to do is something like this:
1.) Before <?php if (have_posts()) : ?> you initialize some variable:
$thumb_data='';
2.) After <?php if (have_posts()) : ?>
$thumb_data='<ul>';
3.) replace your "added code" with this:
$thumb_data.='<li><div class="fotoBoxContent"><a class="fotoBox" href="'.get_the_permalink().'">'.get_the_post_thumbnail()." ".get_the_title().'</a></div></li>';
4.) After the main while loop, add:
$thumb_data='</ul>';
5.) Mow all the HTML code for the list of thumbnails will be in $thumb_data, so just echo this variable in the template where you want the HTML code to appear.