B
B
Boris Belov2015-09-25 10:36:57
JavaScript
Boris Belov, 2015-09-25 10:36:57

How to display posts of only this author in author.php?

Good afternoon.

The question is this.

If I draw a conclusion through

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
, then everything works as it should.

But if I infer through
<?php if (have_posts()) : ?>
<?php query_posts('posts_per_page=5'); ?>
<?php while (have_posts()) : the_post(); ?>
, then the posts of all authors are displayed.

Looks like the system is starting to mess with query_posts, maybe there is some way?

I definitely need to limit the output of posts to 5 entries, because then others are loaded.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mr Crabbz, 2015-09-25
@iborisbelov

Withdraw through
And in functions.php add:

function author_per_page( $query ) {

    if( is_author() && !is_admin() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 5 );
    }
add_action( 'pre_get_posts', 'author_per_page' );

M
MonsterMan, 2015-09-25
@MonsterMan

There is an opinion that the good old query_posts should never be used to avoid problems in the future.
Use WP_Query where you need it and you'll be fine.
For example, authors can be displayed like this:
or so
$query = new WP_Query( 'author_name=KrutoiAvtor' );

D
DVamp1r3, 2015-09-25
@DVamp1r3

Example

M
Maxim E, 2015-09-25
@creativeworm

if you use query_posts, then you need to "add" to what is already there, and you redefine the main loop.

global $wp_query;
query_posts(
  array_merge( // склеиваем массивы
    $wp_query->query, // это массив базового запроса текущей страницы
    array('posts_per_page' => 5) // это параметры который добавили мы
  )
);
// Дальше цикл

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question