paging function for similar posts shows the right links when hovering, but stays on page 1
i show similar posts based on tags:
//similar posts
$postID = get_queried_object_id();
$tags = wp_get_post_tags($postID);
foreach ($tags as $tag) {
//make array $xtag
$xtag[] = $tag->slug;
$count_tag = $tag->count;
}
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
//echo $paged;
$args = array(
'exclude' => $postID,
'order' => 'ASC',
'orderby' => 'name',
'posts_per_page' => 2,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $xtag //apply $xtag array
)
)
);
$customPostQuery = new WP_Query($args);
//checking the max number of pages
echo $customPostQuery->max_num_pages;
//etc. printing the posts in a foreach loop
This works. Then i call a paging function, that should show similar posts by page:
if (function_exists("similar_post_pagination")) {
similar_post_pagination($customPostQuery->max_num_pages);
}
This is the similar_post_pagination
function. It prints the pagination and when hovering the links also shows the correct links, but stays on page 1. I noticed that global $wp_query
is empty. Apperently nothing happens here.
function similar_post_pagination($pages = "", $range = 2) {
$showitems = $range * 1 + 1;
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($pages == "") {
global $wp_query;
**//this is empty**
$pages = $wp_query->max_num_pages;
echo 'empty $pages '.$pages;
if (!$pages) {
$pages = 1;
}
}
if (1 != $pages) {
echo "<div class='archiv-pager'>";
if ($paged > 2) {
echo "<a class='page-numbers' title='" .
$first ."' href='" . get_pagenum_link(1) . "'><<</a><span>| </span>";
}
if ($paged > 1) {
echo "<a class='page-numbers' title='" . $prev . "' href='" . get_pagenum_link($paged - 1) . "'>< </a>";
}
for ($i = 1; $i <= $pages; $i++) {
$delimiter = " ";
if ($i > 1) {
$delimiter = "• ";
} else {
$delimiter = "";
}
if (
1 != $pages &&
(!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) ||
$pages <= $showitems)
) {
if ($paged == $i) {
echo $delimiter . "<span class='page-numbers current'>" . $i . "</span>";
} else {
echo $delimiter . "<a class='page-numbers inactive' title='" . $page . $i . "' href='" . get_pagenum_link($i) . "' >" . $i . "</a>";
}
}
}
if ($paged < $pages) {
echo "<a class='page-numbers' title='" . $next . "' href='" . get_pagenum_link($paged + 1) . "'> ></a>";
}
if ($paged + 1 < $pages) {
echo "<span>| </span><a class='page-numbers' title='" . $last . "' href='" . get_pagenum_link($pages) . "'>>></a>";
}
echo "</div>";
} else {
echo '<div class="dummy-pager"></div>';
}
}
What do i have to use in this function for the pagination to work? Another query? Is it a scope issue? The same function works for archiv pages, but not for the similar posts.
Sorry about the length of this question and thanks for your interest. gurky