I'm currently making a custom Wordpress template, and I'm using paginate_link
to make page links at the bottom.
index.php
<?php
get_header();
?>
<div class="container">
<div class="card-columns">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args1 = array(
'post_type' => array('theory', 'tasks', 'tutorials', 'video', 'interactive'),
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged
);
$query = new WP_Query( $args1 ); ?>
<?php if( $query->have_posts() ): ?>
<?php while( $query->have_posts() ): $query->the_post();
if (get_post_meta( get_the_ID(), 'external', true ))
{
$weblink = get_post_meta( get_the_ID(), 'external', true );
}
else
{
$weblink = get_permalink();
}
?>
<div class="card">
<div class="card-top"><?php
$post_type = get_post_type_object( get_post_type($post) );
echo strtoupper($post_type->labels->singular_name) . " ";
?>
</div>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(500,500, 'class' => "card-img-top img-fluid") ); ?></a>
<div class="card-body">
<h4 class="card-title"><a href="<?php echo $weblink ?>"><?php the_title(); ?></a> <?php
if (get_post_meta( get_the_ID(), 'external', true ))
{
echo '<i class="material-icons">link</i>';
} ?></h4>
<p class="card-text"><?php
if (get_post_meta( get_the_ID(), 'external', true ))
{
$kilde = get_post_meta( get_the_ID(), 'source', true );
echo 'External: ' . $source[name] . ".";
} else {
echo get_the_excerpt();
} ?></p>
<p class="card-text"><small class="text-muted"><?php
$post_id = get_the_ID();
$args = [
'taxonomy' => 'topic'
];
$terms = wp_get_post_terms($post_id, $args ); foreach($terms as $term) {
if ($term->parent == 0) //check for parent terms only
echo " <a href='" . get_term_link($term, $args) ."'>" . $term->name . "</a>"; } ?></small></p>
</div>
</div>
<?php
endwhile;?>
<div class="nav-previous alignleft"><?php
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => rtrim( get_pagenum_link( 1 ), '/' ) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« previous'),
'next_text' => __('next »'),
));
}
?></div>
<?php endif; ?>
</div>
<?php
?>
</div>
<?php get_footer(); ?>
But when I go to any other pages (from page 2 and so on) the title will show "Page not found", but the content is there and pagination is working.
To show the title I'm using this in functions.php:
add_theme_support( 'title-tag' );
function site_wp_title( $title, $sep ) {
global $paged, $page;
if ( is_feed() )
return $title;
// Add the site name.
$title .= get_bloginfo( 'name' );
// Add the site description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) )
$title = "$title $sep $site_description";
// Add a page number if necessary.
if ( $paged >= 2 || $page >= 2 )
$title = "$title $sep " . sprintf( __( 'Page %s', 'it' ), max( $paged, $page ) );
return $title;
}
add_filter( 'wp_title', 'site_wp_title', 10, 2 );
It seems like that wp_title()
doesn't get its results from the custom wp_query()
, cause it works with native posts.
I'm currently making a custom Wordpress template, and I'm using paginate_link
to make page links at the bottom.
index.php
<?php
get_header();
?>
<div class="container">
<div class="card-columns">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args1 = array(
'post_type' => array('theory', 'tasks', 'tutorials', 'video', 'interactive'),
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged
);
$query = new WP_Query( $args1 ); ?>
<?php if( $query->have_posts() ): ?>
<?php while( $query->have_posts() ): $query->the_post();
if (get_post_meta( get_the_ID(), 'external', true ))
{
$weblink = get_post_meta( get_the_ID(), 'external', true );
}
else
{
$weblink = get_permalink();
}
?>
<div class="card">
<div class="card-top"><?php
$post_type = get_post_type_object( get_post_type($post) );
echo strtoupper($post_type->labels->singular_name) . " ";
?>
</div>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(500,500, 'class' => "card-img-top img-fluid") ); ?></a>
<div class="card-body">
<h4 class="card-title"><a href="<?php echo $weblink ?>"><?php the_title(); ?></a> <?php
if (get_post_meta( get_the_ID(), 'external', true ))
{
echo '<i class="material-icons">link</i>';
} ?></h4>
<p class="card-text"><?php
if (get_post_meta( get_the_ID(), 'external', true ))
{
$kilde = get_post_meta( get_the_ID(), 'source', true );
echo 'External: ' . $source[name] . ".";
} else {
echo get_the_excerpt();
} ?></p>
<p class="card-text"><small class="text-muted"><?php
$post_id = get_the_ID();
$args = [
'taxonomy' => 'topic'
];
$terms = wp_get_post_terms($post_id, $args ); foreach($terms as $term) {
if ($term->parent == 0) //check for parent terms only
echo " <a href='" . get_term_link($term, $args) ."'>" . $term->name . "</a>"; } ?></small></p>
</div>
</div>
<?php
endwhile;?>
<div class="nav-previous alignleft"><?php
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => rtrim( get_pagenum_link( 1 ), '/' ) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« previous'),
'next_text' => __('next »'),
));
}
?></div>
<?php endif; ?>
</div>
<?php
?>
</div>
<?php get_footer(); ?>
But when I go to any other pages (from page 2 and so on) the title will show "Page not found", but the content is there and pagination is working.
To show the title I'm using this in functions.php:
add_theme_support( 'title-tag' );
function site_wp_title( $title, $sep ) {
global $paged, $page;
if ( is_feed() )
return $title;
// Add the site name.
$title .= get_bloginfo( 'name' );
// Add the site description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) )
$title = "$title $sep $site_description";
// Add a page number if necessary.
if ( $paged >= 2 || $page >= 2 )
$title = "$title $sep " . sprintf( __( 'Page %s', 'it' ), max( $paged, $page ) );
return $title;
}
add_filter( 'wp_title', 'site_wp_title', 10, 2 );
It seems like that wp_title()
doesn't get its results from the custom wp_query()
, cause it works with native posts.
1 Answer
Reset to default 0Seems like the title was fetched before the query, and therefore didn't fetch the custom posts. Had to add a pre_get_posts
to my functions.php
to make it work:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array('theory', 'tasks', 'tutorials', 'video', 'interactive') );
return $query;
}
title-tag
feature? Second, I tried your code on a Page (post typepage
) andwp_title()
doesn't include "not found". And you should usertrim( get_pagenum_link( 1 ), '/' )
to remove the extra slash. – Sally CJ Commented Aug 12, 2019 at 1:30$query
? Is it your custom query? Where and how do you use it? – Krzysiek Dróżdż Commented Aug 12, 2019 at 5:24