How to limit number of posts in index.php - php

I have an index.php with loop
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
// render post here
<?php endwhile; ?>
<?php endif; ?>
Now I want to limit number of post by N on the page and create
a link to next/previous N posts.
A small code snippet is very appreciated.
UPDATE
What about the URL string? I want to make functionality similar to SO
where query is determined by URL for example
get next twenty questions from the recent
https://stackoverflow.com/questions?page=2&sort=active

You can achieve that in this way:
query_posts( 'posts_per_page=5' );
References:
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query

Related

How to show only posts with certain category in wordpress theme?

I was wondering if there is a way to change this code to only display posts from certain category created in wordpress. Now it displays every recent post. Let's say I would create "News" category in wordpress and I want this piece of code to display only News posts.
Thanks for help
<?php
if( have_posts() ){
while( have_posts() ){
the_post();
get_template_part( 'template-parts/content', 'koncert');
}
}
?>
You can override the usual query that wordpress uses and create a custom one;
https://developer.wordpress.org/reference/classes/wp_query/
usually though for just displaying a category you can just open the category slug and provided your template has the correct archive page it will display the posts for that category.
If not use something similar to below. You can further refine your search parameters like number of posts and post types as defined in the link above.
<?php
//refine your query to the category you desire either a slug(example below) or category id
$args = array(
'category_name' => 'my_category_slug',
);
//create the query using the arguments
$query = new WP_Query($args);
?>
//create the loop to show the posts
<?php if($query->have_posts()): ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>

Fix for wordpress loop

I am new too wordpress and have one problem. I finaly understand post loop and it work well but have one problem. For example: I have two pages of my posts (all are 6). In one page i have five posts (excerpt only) and on secound page i have one post only and in this case (when on one of my pages i have only one post it change to content post but it need to stay excerpt). So my question is what can i change in my code to make it work well in this case when on some page left only one post?
It's my loop:
<?php if (have_posts()) : ?>
<?php if (($wp_query->post_count) > 1) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do your post header stuff here for excerpts-->
<?php the_excerpt() ?>
<!-- Do your post footer stuff here for excerpts-->
<?php endwhile; ?>
<?php else : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do your post header stuff here for single post-->
<?php the_content() ?>
<!-- Do your post footer stuff here for single post-->
<?php endwhile; ?>
<?php endif; ?>
<?php else : ?>
<!-- Stuff to do if there are no posts-->
<?php endif; ?>
Use the posts_per_page parameter to an array and start the loop.
$args = array( 'post_type' => 'post', 'posts_per_page' => 4,'category' => 2, 'paged' => $paged );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
$content = get_the_content();
echo $content;
endwhile;
Here I have printed the 4 contents from the archive. So the page will only have 4 posts in it. You can change the value based on your need.
I have used and got the output. Hope this helps you too.
Ur second if condition should be inside while and while shuld be called once ..
Somthing like
If
While
Then if for count
Elseif
Else
End while
End if
I hope it wil help ..
Answer for all: $wp_query->post_count need to be replaced by $wp_query->found_posts and everything working fine now!

How to call limit post of author on author page (wordpress)

I want to show author's limited post titles on the author page.
I'm using the code outside the wordpress loop to show limited post of author and the code is <?php query_posts('posts_per_page=20'); ?> . but its showing recent post and i want only posts from author.
How to do this, anybody have idea?
Sample Code
<?php
$the_query = new WP_Query(array('posts_per_page'=>20,'author'=>$put_author_id_here) );
while ( $the_query->have_posts() ) : the_post();
/* display the post as you do in loop*/
endwhile;
wp_reset_postdata();
?>
use
query_posts('posts_per_page=20&author=$ID');
or use
query_posts('posts_per_page=20&author_name="admin"');

What is the proper way to query a Wordpress custom field

I have this working query that successfully gets the custom field data in my page template file:
<?php $featuredpost_cat = get_field('featured_category_id'); ?>
If I echo that out into the page I get "23" the value of the custom field, so I know that is working, what I want to do is grab that value and use it as a query parameter.
Farther down my page I have this:
<?php query_posts( $featuredpost_cat . '&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); ?>
All that this does is ignore my variable and return the latest post on the site.
I hope this is clear enough.
== Edit ===
In case I am not being clear, I want to get a custom field which is a category ID from the page, then use it in a query on the page template.
So I set the field as category ID: 23 and then call it in my query_posts function so that I only return posts from that category.
Maybe the full page of code will help: template code
How about
<?php query_posts( 'cat='.$featuredpost_cat . '&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); ?>
I assume that $featuredpost_cat is a category id
Sorry, I don't understand your second code example. Are you trying to use ternary operator to accomplish this?
query_posts('cat='.$featuredpost_cat . '&posts_per_page=1');
if (have_posts()){
while (have_posts()){
the_post();
}
}
What does query_posts() and the_post()do? If query_post() fetches the posts, have_post() checks the existance of posts and the_post() echoes them on the page, the code above should work. If this is not the case, please tell what the functions do.
Edit. Removed the question mark.

Wordpress list all posts (excerpt) php loop

I am creating a wordpress template and I now need some code that creates a loop to show all posts but not full posts, just excerpts.
Can anyone help please?
Use this code to generate the excerpt into the loop:
<?php
if(have_posts())
{
while(have_posts())
{
the_post();
the_excerpt();
}
}
?>
The above will generate only the excerpt of the posts. If you need extra options like post title, date, author and more you have to read the WordPress codex. http://codex.wordpress.org/Main_Page
You can read more by following the link given by markratledge below.
Everything you need to know - with examples - is here: http://codex.wordpress.org/The_Loop
The most basic loop is
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
and you want to use the_excerpt() instead of the_content() See http://codex.wordpress.org/Function_Reference/the_excerpt

Categories