I already read several answer in SE network and WP forum and several log. but I am still in the dark.
At first, I have to say, my consequence is different.
I created a Page Template Blog (blog.php) and added a wp_query on that page to show normal blog posts in that post because my homepage is a static homepage.
Then I simply created a blank page named Blog with permalink /blog
Next, I added wppagenavi on that ge after elsewhile
It didn't work so I read couple of SOF answer and added
<?php wp_pagenavi( array( 'query' => $the_query ) ); ?>.
It shows page navigation but not working. I clicked 2,3,4... button and it return same page though url changes to /blog/page/2
So, how to make page navi work?
My snippet
<?php $the_query = new WP_Query( 'showposts=8' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="content-box bg-wrap"><?php
if ( has_post_thumbnail() ) { ?>
<div class="img"><a href="<?php the_permalink(); ?>">
<?php
the_post_thumbnail( 'blog_thumb' );
?>
</a></div>
<?php
}
?>
<div class="post-info p20px">
<h2><?php the_title(); ?></h2>
<div class="post-meta"><span class="theauthor"><i class="fa fa-user"></i> <?php echo get_the_author(); ?></span> <span class="thetime"><i class="fa fa-clock-o"></i> <?php the_date(); ?></span> <span class="thecategory"><i class="fa fa-tags"></i><?php the_category(', '); ?></span> <span class="thecomments"><i class="fa fa-comments"></i><?php echo $write_comments; ?></span> </div>
<br/>
<?php echo substr(get_the_excerpt(), 0,600); ?> Read this post »
</div>
</div>
<?php endwhile;?>
<?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
Do not bother other functions and variables or html. I just need to make pagenavi work.
I got pagenavi code from here
I found the problem and the solution as well.
I have to add '&paged='.$paged on $wp_query() and it enables navigation on that scenario.
So, the first line
<?php $the_query = new WP_Query( 'showposts=8' ); ?>
have to be replaced with
<?php $the_query = new WP_Query( 'showposts=8' . '&paged='.$paged); ?>
That's all.
See a live preview (the project I applied this): Here
Related
I have a related posts slider but currently there is not enough posts to actually slide. The design is such that it's not a simple case of displaying the posts but not sliding them until there is more.
As a short term solution, I am trying to find a way to loop through the posts twice to give the appearance of an infinite loop.
I am using slick slider and initially tried the settings:
infinite: true,
loop: true
but I can't get it to work even though apparently that should do the trick.
I am now trying to just pull through the posts twice
I have tried adjusting the count to things like
$count+1;
$count = 2
$count+=5;
All sorts of variations but I think I am way off base.
I would appreciate any help or a point in the right documentation. I have been reading about iterations but I can't grasp how they would be included in this as I had help with this from another developer.
<div class="log-book-carousel">
<?php
$current_page_id = get_the_ID();
$args = [
'posts_per_page' => '6',
'post__not_in' => [$current_page_id]
];
$the_query = new WP_Query( $args ); ?>
<?php
// Start our WP Query
while ($the_query -> have_posts()) : $the_query -> the_post();
// Display the Post Title with Hyperlink
?>
<div class="slides match-height">
<a href="<?php the_permalink(); ?>" title="Read more of the blog post '<?php the_title_attribute(); ?>'">
<?php if (has_post_thumbnail()) : ?>
<div class="log-book-slider-image">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<div class="log-book-details-wrapper white-bg">
<h3 class="black log-title"><?php the_title(); ?></h3>
<div class="log-book-slider-excerpt ">
<p class="log-book-text"><?php sew_display_post_intro_block( $post->ID, 10 ); ?></p>
</div>
</div>
</a>
</div>
<?php
$count++;
endwhile;
wp_reset_postdata();
?>
</div>
I just started playing around in WordPress, and created a theme for fun.
Everything is working properly except when I click on an archives month or category, the page I'm taken to displays all of the blog posts just like the main page, even though the slug is correct.
I've created an archive page and category page, replicating the main page since I want the design to be the same.
<div class="recentBlogsWrapper">
<h3><?php single_cat_title(); ?><?php get_the_archive_title(); ?> Category</h3>
<div class="blogPostWrapper">
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
);
$blogposts = new WP_Query($args);
while($blogposts->have_posts()) {
$blogposts->the_post();
?>
<a href="<?php echo the_permalink(); ?>" class="blogCard card">
<div class="blogHomeImgWrap">
<img class="blogPostImg" src="<?php echo get_the_post_thumbnail_url('get_the_ID'(),'full') ?>" />
</div>
<div class="blogPadding">
<h3><?php the_title(); ?></h3>
<p><?php the_time('F j, Y') ?></p>
</div>
</a>
<?php } wp_reset_query(); ?>
</div>
</div>
What am I missing with the query and how do I reference the link I just clicked on?
If you have placed your code inside archive.php you shouldn't need to use a WP_Query (custom query), but just use the bog standard WordPress loop like this:
<div class="recentBlogsWrapper">
<h3><?php single_cat_title(); ?><?php get_the_archive_title(); ?> Category</h3>
<div class="blogPostWrapper">
<?php
while ( have_posts() ) {
the_post();
?>
<a href="<?php echo the_permalink(); ?>" class="blogCard card">
<div class="blogHomeImgWrap">
<img class="blogPostImg" src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'full' ) ?>" />
</div>
<div class="blogPadding">
<h3><?php the_title(); ?></h3>
<p><?php the_time( 'F j, Y' ); ?></p>
</div>
</a>
<?php } ?>
</div>
</div>
Explanation: by using the following code as an argument in your custom loop
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
);
You were asking WordPress to get all (any) posts. But in reality, you want to only grab posts in a certain category when you're on the category archive. Luckily for you, WordPress does all this for you, so by removing your custom query you should be good to go.
I created a separate page for blog posts on my website (WordPress). I Used this code for showing 6 last posts:
<div class="container">
<div class="entry-content">
<div class="last-posts">
<?php $the_query = new WP_Query( 'posts_per_page=6' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-md-4">
<div class="post-item">
<a href="<?php the_permalink() ?>" target="_blank">
<figure><?php the_post_thumbnail(); ?></figure>
</a>
<div class="post-detail">
<h3>
<a href="<?php the_permalink() ?>" target="_blank">
<span><?php the_title(); ?></span>
</a>
</h3>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</div>
</div>
The problem is, this loop works from "container" class to the end of the code, not just 6 last posts. This loads all 6 posts 6 times.
HI you are making a small mistake in the WP_Query
please try the following line.
<?php $the_query = new WP_Query( 'posts_per_page' => 6 ); ?>
I hope this will work for you.
$args = array('posts_per_page' =>6, 'post_type' => 'post','order'=>'desc','post_status'=>'publish');
$the_query = new WP_Query( $args );
I'm currently working on my first Wordpress theme and I've a problem:
I want to have my last 3 posts on every site. Everything works fine on my home page, but when I go to another page it just shows the page name and the "Read more..." tag after it.
The code I use is:
<?php while(have_posts()) : the_post(); ?>
<div class="article-preview">
<p>» <?php the_time('l, j. F Y')?></p>
<b><?php the_title(); ?></b>
<?php the_excerpt(); ?> Mehr...
<hr style="margin-top:5px" />
</div>
<?php endwhile; ?>
Does anyone know how to fix this issue? Thanks in advance!
For displaying posts on other pages, you have to display custom query before while loop.
Here is the updated code of your code:
<?php $the_query = new WP_Query( 'post_type'=>'post', 'posts_per_page=3' ); ?>
// Start our WP Query
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="article-preview">
<p>» <?php the_time('l, j. F Y')?></p>
<b><?php the_title(); ?></b>
<?php the_excerpt(); ?> Mehr...
<hr style="margin-top:5px" />
</div>
<?php endwhile;
wp_reset_postdata();
?>
May be this would be helpful for you.
I have managed to get recent posts showing on my homepage which is working well. They only problem i have is that the full post and not the excerpt is showing.
<div id="home-news-container">
<?php query_posts('cat=#&posts_per_page=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="home-post-container">
<div class="home-post-thumb">
<a href="<?php echo get_permalink(); ?>">
<?php the_post_thumbnail('large_wide'); ?>
</a>
</div>
<div class="home-post-copy">
<h4>
<a href="<?php echo get_permalink(); ?>">
<?php the_title(); ?>
</a>
</h4>
<h5>
<?php the_date(); ?>
</h5>
<?php echo the_excerpt(); ?>
<div class="home-news-readmore">
read more
</div>
</div>
</div> <!-- end home-post-container -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<div class="home-news-readmore news-extra">
more news
</div>
</div> <!--- end home-post-container -->
I don't understand what the problem is to be honest. I created a new full width template for the homepage which i thought might be causing it but its not. Bit stumped to be honest. Any help would be greatly appreciated
Use wp_get_recent_posts to get recent post
Use substr to display some limeted character of post character
<?php
$args = array (
'numberposts' => 3,
'post_type' => 'your post type name'
);
// the query
$recent_posts = new wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
echo substr($recent["post_content"],0,100);
}
?>