我目前正在制作一个自定义Wordpress模板paginate_link
在底部创建页面链接。
指数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(); ?>
但当我转到任何其他页面(从第2页等)时,标题将显示“未找到页面”,但内容在那里,分页正在工作。
为了显示标题,我在函数中使用了这个。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 );
看起来是这样的
wp_title()
未从自定义中获取其结果
wp_query()
, 因为它适用于本机帖子。