1

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
    First, are you not using the title-tag feature? Second, I tried your code on a Page (post type page) and wp_title() doesn't include "not found". And you should use rtrim( get_pagenum_link( 1 ), '/' ) to remove the extra slash. – Sally CJ Aug 12 '19 at 1:30
  • What is $query? Is it your custom query? Where and how do you use it? – Krzysiek Dróżdż Aug 12 '19 at 5:24
  • @SallyCJ I'm using it now. Problem is still there though. – janlindso Aug 12 '19 at 22:59
  • @KrzysiekDróżdż Yes, it's a custom query. I have added it to my questions now. Hope it helps. – janlindso Aug 12 '19 at 23:01
  • 1
    @SallyCJ Found the error, and answered my own questions. It helped write my thoughts and discuss it. Thanks! – janlindso Aug 15 '19 at 17:41
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.