WP_TITLE()在第2页返回‘找不到页面’

时间:2019-08-12 作者:janlindso

我目前正在制作一个自定义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(), 因为它适用于本机帖子。

1 个回复
最合适的回答,由SO网友:janlindso 整理而成

似乎标题是在查询之前获取的,因此没有获取自定义帖子。必须添加pre_get_posts 给我的functions.php 要使其工作:

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;
}

相关推荐

Show pagination in WP_Query

我正在编写一个foodblog,并试图在特定页面中显示收藏夹帖子列表,但我对分页有一些问题。我在前面的问题中尝试了不同的解决方案,但都没有解决问题。非常感谢 <?php $my_favs = get_user_meta(get_current_user_id(), \'user_favs\', true); $args = array( \'post_type\' => array(\'recipe\', \'post