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.
Related
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.
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.
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');?>
When I type "localhost/?cat=3" for example, to show all posts of category whose ID is 3, instead of doing it, it shows all posts of all categories (which is the job of my index.php), so I guess that a redirection to homepage is happening. I really don't know what to do. Can you help me? Here is the code of index.php to retrieve posts:
<?php
$tmp = $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query('cat=-4&posts_per_page=5&paged=' . $paged);
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="wrapper2">
<div id="topicos" >
<p id="titulo"><?php the_title(); ?></p>
<p id="data_public">Publicado em <b><?php the_time('j') ?> de <?php the_time('F, Y') ?> </b> por <?php the_author() ?></p>
<?php if(has_post_thumbnail()){
?> <div id="thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php
}
?>
<div id="prv_texto"><?php the_excerpt(); ?></div>
<p id="cont_lendo">Continuar lendo...</p>
</div>
<?php endwhile; ?>
<?php endif; ?>
Have you tried making an archive.php file in your theme, and running a bare bone Wordpress loop without a custom query before it?
Here's my HTML for an archive page:
<?php get_header(); ?>
<nav>
<?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?>
</nav>
<div id="main-content-archive">
<h5 class="inner_text_shadow">Archive</h5>
<div id="clear-box">
<?php query_posts( array(
'posts_per_page' => 16,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
));
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php $bg_image = post_thumb( get_the_post_thumbnail() );?>
<a href="<?php the_permalink() ?>"><div class="post-bg-archive" style="background: #777777 url(<?php echo $bg_image;?>);">
<div id="title-bg">
<div class="transparency"></div>
<div class="archive-title"><h98><?php the_title(); ?></h98></div>
</div>
</div></a> <!-- END post-bg -->
</div>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div> <!-- END post_class function -->
<div id="archive-nav"><h12><?php include (TEMPLATEPATH . '/inc/nav.php' ); ?></h12></div>
</div> <!-- END main-content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
It's not actually an archive page btu modified front page that has more posts showing and a grid view.
The only thing missing here is the reverse order of posts. I want my archive to start with the oldest post. I've read all over and found a PHP code for reversing order but I can't seem to input it correctly.
This is the code that I used:
<?php query_posts($query_string . "&order=ASC"); ?>
Depending on where I put it, it either:
messes up something and I get a PHP error
does nothing
instead of showing a 4x4 grid, it shows 1 box that leads to localhost/archive i.e. the page that I'm on already.
I also tried resetting query and using the aforementioned code after that but that gave me the 3rd version of the problem (also mentioned above).
So, how to get a reverse order of posts at this page?
Try this code:
<?php query_posts( array(
'posts_per_page' => 16,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )).'&order=ASC');?>