How to fix pagination link to page 2 wordpress? - wordpress

Hi all I am a new of wordpress I have problame with pagination, I used plugin Wp-pagnavi ,When I click link to page 2 it same as page 1,How to to fix it.
You can see it at
http://westecmedia.com/?page_id=758
And this my code in page-event.php
<?php
/*
* Template Name: Page - Events Page
*/
?>
<?php get_header(); ?>
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 300);
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php endwhile; endif; ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query(); ?>
</div>
</div>
<?php get_footer(); ?>
Help me please :(

Include paged, documentation: https://codex.wordpress.org/Pagination
<?php
$args = array(
'cat' => '5',
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post();
/* Do whatever you want to do for every page... */
endwhile;
wp_pagenavi();
wp_reset_query(); // Restore global post data
?>
Also don't use query_posts to fetch data in WordPress, consider using https://codex.wordpress.org/Class_Reference/WP_Query.
Please ask WordPress related question here: http://wordpress.stackexchange.com
I hope this helps.

Related

Wordpress custom loop pagination - always same content

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 set featured images on my post but has_post_thumbnail is still false

I'm creating a home page that will pull in different posts to display them in a custom format. I can get most of the information I want but I haven't been able to to get the featured image for any of them. I set a featured image for all three posts and I'm using my own theme that's a child of the twentysixteen theme. I get has_post_thumbnail returning false on all of them even though I've uploaded a featured image for each post. I only have this on local right now but here's the code I'm using to get the posts:
<?php
global $post;
$myposts = get_posts('numberposts=3&category=1');
foreach($myposts as $post) :
setup_postdata( $post );?>
<div class="article-box">
<div class="article-box-image">
<?php if (has_post_thumbnail($post->ID)) { ?>
has one
<?php } else { ?>
not working
<?php } ?>
<img src="<?php the_post_thumbnail_url(); ?>">
</div>
<div class="article-box-title">
<?php the_title(); ?>
</div>
<div class="article-box-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="article-box-edit">
<?php edit_post_link('Edit'); ?>
</div>
</div>
<?php endforeach; ?>
Use a wp query:
<?php
$args = array(
'cat' => '1',
'posts_per_page' => '3',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="article-box">
<div class="article-box-image">
<?php //check for thumbnail
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
//Give up and start new life picking apples
} ?>
</div>
<div class="article-box-title">
<?php the_title(); ?>
</div>
<div class="article-box-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="article-box-edit">
<?php edit_post_link('Edit'); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
And then double check that thumbnails are supported in your theme's functions.php
add_theme_support( 'post-thumbnails' );
I finally realized that the "preview" post parameters were the problem. I was viewing the page through preview page. I don't know why images won't work in preview mode but apparently it won't.

How to fix pagination not found in wordpress?

I am a new of wordpress. I have a problem with pagination when I click to the next post it shows "not found". I installed the plugin wp pagenavi, and I put code in my blog post . It shows the pagination, but I have a problem with the link to the next post. Example when I click to the next post it is show
Something went Wrong!
404
-->
You can see at: http://westecmedia.com/events/
And this is my code in event-page.php:
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 300);
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php endwhile; else: endif; ?>
<?php wp_pagenavi(); ?>
</div>
</div>
<?php get_footer(); ?>
Help me please ?
Maybe you missed the object here. You can try the code below:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'category_name'=>get_the_title(),
'post_status'=> array('publish', 'future')
);
query_posts($args);
instead:
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>

Pagination For Custom Query Is Not Working

I am creating a custom homepage template in which I am trying to show posts with navigation.
Here is what I have followed.
Created a file homepage.php and gave the template name as "HomePage".
Created a page "Home" from dashboard and assigned this template "HomePage".
Then from settings > reading, chosen static front page "Home".
Here is my code for querying posts.
<div class="posts-container">
<?php query_posts('post_type=post&posts_per_page=2&post_status=publish&paged='. get_query_var('paged')); ?>
<?php if(have_posts()): ?>
<?php while (have_posts() ) : the_post(); ?>
<div class="post">
<h4 class="entry-title"><?php the_title(); ?></h4>
</div>
<?php endwhile; // end of the loop. ?>
</div>
<div class="pagination-nav">
<div class="alignleft"><?php next_posts_link(__('Next »','example')); ?></div>
<div class="alignright"><?php previous_posts_link(__('« Previous','example')); ?></div>
</div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
Now, on clicking the links generated by pagination functions, I am getting redirected to the page where I was first. While the URL gets updated like
http://localhost/mysite/?paged=2
Lets say if there are 4 posts (post 1, post 2, post 3, post 4) that and the recent ones are post 4 and post 3 respectively, then I cannot get to post 1 and post 2. Both of the pages
http://localhost/mysite
http://localhost/mysite/?paged=2
show me only the post 4 and post 3. What can I do to solve this?
For a static front page you need to check for the page var too, in this way:
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts('...&paged=' . $paged);
This was the solution that worked for me. I had to re-write the query and have used WP Query this time.
<?php
global $wp_query, $paged;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query_args = array (
'post_type' => 'post',
'post_status'=>'publish',
'posts_per_page' => 4, //for testing purposes
'paged' => $paged,
);
$wp_query = new WP_Query($query_args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts()) :
$wp_query->the_post();
?>
<div class="post">
<?php if ( has_post_thumbnail() ) { ?>
<a class="entry-image" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); } ?>
</a>
<h4 class="entry-title"><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h4>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<div class="pagination-nav">
<div class="alignleft">
<?php next_posts_link(__('Oudere artikelen')); ?>
</div>
<div class="alignright">
<?php previous_posts_link(__('Newer')); ?>
</div>
</div>
</div>
<!-- /posts-container -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
And Whola!, this one worked.
Might be useful for someone else.

WordPress query_posts()

Hello Im new to Wordpress , But I styled some elements, and had the recent posts in them . My question, can I have them for 5 posts repeated after each other ? thanks :)
<div id="main_content">
<h2>Latest Products</h2>
<div class="latest_products">
<div class="group">
<?php query_posts("post_per_page=1"); the_post(); ?>
<h3 class="stick_note"><?php the_title(); ?></h3>
<div class="pro_content">
<div class="product_thumbnail"> <?php the_post_thumbnail('thumbnail'); ?> </div>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</div> <!-- END LATEST PRODUCTS -->
</div> <!-- END Main Content -->
You forgot to implement The Loop (click for detailed info).
A full Wordpress loop is as follows:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...render your post here, it will keep repeating...
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
Since you don't have an actual loop implemented it can only render 1 post.
You can use:
get_archives('postbypost', 5);
OR
query_posts("showposts=5");
This help you? Be more specific in your question.
You can find the answer in documentation of wordpress:
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Template_Tags/query_posts
http://codex.wordpress.org/Template_Tags/wp_get_archives
EDIT:
$args = array('numberposts' => 5, 'order'=> 'ASC');
$postslist = get_posts( $args );
EDIT2:
You can do:
$args = array('numberposts' => 5, 'order'=> 'ASC');
$postslist = get_posts( $args );
foreach( $postslist as $post ) : setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>

Resources