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"');
Im forced to work with wordpress, and if you work with it, you probably know what i mean:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Its working, no question. But i do not understand what this actually means. Its not a ternary operator nor anything else i know. Ive never seen a statement like this in any php-projects ive worked on. So i got several questions:
What is this line exactly doing? I know that it gets all posts, iterates over them and ... what is this the_post() doing? And what are these doubledots doing?
Is this Wordpress-Only or could it be used somwhere else too?
Where is the current post stored?
Ive already googled it, but there are no information regarding my problem, noone seems to be interested in how wordpress works. I am, but i do not get it. If somebody got an explanation for me, it would be great.
<?php define('WP_USE_THEMES', false); get_header(); ?>
The loop starts here:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
and ends here:
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
This is using PHP's alternative syntax for control structures, and could also be expressed as:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
the post()
This function does not accept any parameters.
Return Values
This function does not return any values.
<?php
while ( have_posts() ) : the_post();
echo '<h2>';
the_title();
echo '</h2>';
the_content();
endwhile;
?>
have_posts()
Parameters
This function does not accept any parameters.
Return Values
(boolean)
True on success, false on failure.
Examples
The following example can be used to determine if any posts exist, and if they do, loop through them.
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Your loop code
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
?>
Note
Calling this function within the loop will cause an infinite loop. For example, see the following code:
<?php
while ( have_posts() ): the_post();
// Display post
if ( have_posts() ): // If this is the last post, the loop will start over
// Do something if this isn't the last post
endif;
endwhile;
?>
If you want to check if there are more posts in the current loop without this unfortunate side effect, you can use this function.
function more_posts() {
global $wp_query;
return $wp_query->current_post + 1 < $wp_query->post_count;
}
1. What is LOOP
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags.
It will fetch data related to specific page
:(colon) is used to tell condition/loop starts from here. You can replace it with { }(bracket quotes)
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
2. Is this Wordpress-Only or could it be used somwhere else too?
yes of course you can use it.
You can access full wordpress functionality by including one core file with name of "wp-blog-header.php" that is located on root of wordpress directory.
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>
Include this file in the top of your external file, you can access wordpress database, wordpress function , wordpress hooks too.
3. Where is the current post stored?
11 default tables are existed in wordpress database. You can see wp_posts table in database. all posts are store in this table.
suppose, if your are creating meta tag in your post, it will store in wp_postmeta
It's just an alternative syntax for:
if ( have_posts() ) { //open if
while ( have_posts() ) { //start while loop
the_post(); //call a function
See http://php.net/manual/en/control-structures.alternative-syntax.php
It's not wordpress specific and can be used in any php code.
To whom it may concern.
What is WordPress Loop?
When we save data inside WordPress(posts, pages, almost everything) data gets saved as a row inside our database fx. MySQL.
WordPress dynamically query the database and finds which row corresponds to the page you are on, and pulls that data then displays it in that section.
As this is a dynamic query and executed in a repeated manner, it is called a WordPress Loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
What are these doubledots doing? condition/loop starts from here :(colon) and is a ternary operators.
I think rest of the questions has been answered by others.
I have the following function in functions.php page
function viewpost($num)
{
echo $num;
query_posts('order=dsc&cat=$num & showposts=2');
while (have_posts()) : the_post();
?> <span> <?php the_title(); ?>
<?Php
echo get_the_post_thumbnail();
the_excerpt();
?>
<?Php
endwhile;
wp_reset_query();
}
When I call viewpost function for values of viewpost(1)(to view post from category one ) it shows correct values, but when I put the same function again viewpost(2) (to view post from category 2) it shows the previous function values i.e. from category. What can I do to get the post from different categories by changing the passing value
Without trying your code, I think the most likely problem is you're using single quotes. Variable names won't get expanded to their values. See this answer.
Try
query_posts("order=dsc&cat=$num&showposts=2");
instead of
query_posts('order=dsc&cat=$num & showposts=2');
This might be worth a read too. Using query_posts is normally not recommended.
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
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