THE CODE:
author.php (just the useful bit)
<?php if (have_posts()) { ?>
<div class="row archive-grid">
<?php while (have_posts()) : the_post();
include(locate_template('parts/loop-archive-grid.php'));
endwhile; ?>
</div>
<?php }
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$secondloop = new WP_Query(array(
"post_type" => "musikalbum",
"posts_per_page" => 3,
"author_name" => get_query_var('author_name'),
"paged" => $paged
));
if ($secondloop->have_posts()) { ?>
<div class="row archive-grid">
<?php while ($secondloop->have_posts()) : $secondloop->the_post();
get_template_part( 'parts/loop', 'album-grid' );
endwhile; ?>
</div>
<?php author_page_navi( $loop );
} else {
joints_page_navi();
} ?>
Setting the posts_per_page
for my main query in my functions.php
function my_post_queries( $query ) {
if (!is_admin() && $query->is_main_query()){
if (is_author()){
$query->set('posts_per_page', 3);
} else {
$query->set('posts_per_page', 9);
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
But it still shows me a 404 error. If I remove the if clause, then it leads me to the index.php which is not reeeally what I intended. My guess isSo I need that if clause...but WordPress doesn't seem to know it yet!
- either this code gets executed before WordPress knows what template it's dealing with (trying to execute get_current_template(true) in there throws an "Undefined function" error)
- OR because
$wp_query->is_404
is true by the time this code is run, it doesn't attempt to load the author.php template.
Pasting the code directly to the template didn't work either, even if it's just these three lines pasted before get_header();
:
THE CODEEDIT: ATTEMPT 2
author.php I ditched (just the useful bit)template_redirect
for hooking into pre_get_posts
:
<?php iffunction (have_postsmy_post_queries() $query ) { ?>
if <div(!is_admin() class="row&& archive$query-grid">>is_main_query()){
<?php whileif (have_postsis_author()) : the_post();{
include(locate_template('parts/loop-archive$query-grid.php')>set('posts_per_page', 3);
// endwhile;NEW ?>CODE:
</div>
<?php }
$paged = (get_query_var('paged')) ? intval(get_query_var('paged')) : 1;
$secondloop = new WP_Query(array(
global "post_type"$original_page_request; // saves the original value =>of "musikalbum",
$paged because I'm about "posts_per_page"to =>alter 3,it!
"author_name" =>if get_query_var('author_name'),
"paged" => $paged
));
if ($secondloop> $query->have_posts()>max_num_pages) { ?>
<div class="row archive-grid">
<?php while ($secondloop$query->have_posts>set()) :'paged', $secondloop$query->the_post(>max_num_pages);
get_template_part( 'parts/loop',$original_page_request 'album-grid'= );$paged;
} endwhile;else ?>{
</div>
<?php author_page_navi$query->set( $loop'posts_per_page', 9);
} else { }
}
}
add_action( 'pre_get_posts', 'my_post_queries' joints_page_navi();
} ?>
SettingThe idea was to manually set the main query page when it exceeds max_num_pages and store the "actual page" in posts_per_pageglobal $original_page_request
for my main query in myeverything that needs it.
I then edited functionsauthor.php:
functionglobal my_post_queries$original_page_request;
// Check if max_num_pages is exceeded (= $queryif )$original_page_request {is set!)
// Also set the $paged variable for my $secondloop to the unaltered paged query var.
if (!is_adminempty($original_page_request)) &&{
$query->is_main_query $is_within_max = true;
$paged = get_query_var('paged') ? intval(get_query_var('paged')) : 1;
} else {
$is_within_max = false;
$paged = $original_page_request;
}
// Main loop, hide it if max_num_pages exceeded!
if (is_author$is_within_max && have_posts()) { ?>
<div class="row $queryarchive->setgrid">
<?php while ('posts_per_page',have_posts()) 3: the_post();
} else {
$query->setinclude('posts_per_page', 9locate_template('parts/loop-archive-grid.php'));
}
}
} endwhile; ?>
add_action( 'pre_get_posts', 'my_post_queries' ); </div>
<?php }
And here areProblem is, it doesn't work. I was so happy not to see a 404 any more, but now it automatically sets the pagination functions used from my JointsWP4 starter theme:main query page to 0.
<?php // This function was standard from my JointsWP4 starter theme...
function joints_page_navi($before = '', $after = '') {
global $wpdb, $wp_query;
$request = $wp_query->request;
$posts_per_page = intval(get_query_var('posts_per_page'));
$paged = intval(get_query_var('paged'));
$numposts = $wp_query->found_posts;
$max_page = $wp_query->max_num_pages;
//...except for this line, since author_page_navi uses the same logic:
page_navi_logic( $posts_per_page, $paged, $numposts, $max_page );
}
// A "hacked" version of joints_page_navi() for my $secondloop to work (or not)
function author_page_navi( $loop ) {
global $wpdb, $wp_query;
$request = $wp_query->request;
$posts_per_page = max(intval($wp_query->posts_per_page), intval($loop->posts_per_page));
$paged = intval(get_query_var("paged"));
$numposts = max($wp_query->found_posts, $loop->found_posts);
$max_page = max($wp_query->max_num_pages, $loop->max_num_pages);
page_navi_logic( $posts_per_page, $paged, $numposts, $max_page );
}
function page_navi_logic( $posts_per_page, $paged, $numposts, $max_page ) {
if ( $numposts <= $posts_per_page ) { return; }
if(empty($paged) || $paged == 0) {
$paged = 1;
}
$pages_to_show = 3;
$pages_to_show_minus_1 = $pages_to_show-1;
$half_page_start = floor($pages_to_show_minus_1/2);
$half_page_end = ceil($pages_to_show_minus_1/2);
$start_page = $paged - $half_page_start;
if($start_page <= 0) {
$start_page = 1;
}
$end_page = $paged + $half_page_end;
if(($end_page - $start_page) != $pages_to_show_minus_1) {
$end_page = $start_page + $pages_to_show_minus_1;
}
if($end_page > $max_page) {
$start_page = $max_page - $pages_to_show_minus_1;
$end_page = $max_page;
}
if($start_page <= 0) {
$start_page = 1;
}
echo $before.'<nav class="push-down"><ul class="pagination">'."";
if ($start_page >= 2 && $pages_to_show < $max_page) {
$first_page_text = __( 'Erste...', 'jointswp' );
echo '<li><a href="'.get_pagenum_link().'" title="'.$first_page_text.'">'.$first_page_text.'</a></li>';
}
echo '<li>';
previous_posts_link( __('Back', 'jointswp') );
echo '</li>';
for($i = $start_page; $i <= $end_page; $i++) {
if($i == $paged) {
echo '<li class="current"> '.$i.' </li>';
} else {
echo '<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
}
}
echo '<li>';
next_posts_link( __('Forward', 'jointswp'), $max_page );
echo '</li>';
if ($end_page < $max_page) {
$last_page_text = __( 'Last...', 'jointswp' );
echo '<li><a href="'.get_pagenum_link($max_page).'" title="'.$last_page_text.'">'.$last_page_text.'</a></li>';
}
echo '</ul></nav>'.$after."";
}
0?!
When hooking into pre_get_posts
, this is exactly what $query->max_num_pages
gives me. That's because by that time, it's still in the process of gathering query variables and did not execute its query YET. So there are no posts to show because no posts retreived, and thus no max_num_pages.
I then copied the new code from my_post_queries( $query )
to another function which hooks into wp
. There, I have access to max_num_pages and also the template I'm in. Still nothing. Because as soon as I exceed the number of posts, said code doesn't get executed. I could however set $wp_query->is_404 = false
but that returns me to the index, yet again.