0
votes

I've created a custom post type to display a portfolio of my work. When a user clicks a project, i want it to display the project details.

On the single project detail page i'd like a user to be able to navigate to the project posted before and after it but i can't get the pagination to work correctly.

My loop is only displaying the most recent post no matter what link i go to and i can't figure out why. I think this is the issue to my pagination issue. Any help is appreciated.

I've included a video of what im experiencing: https://gyazo.com/4f148154e05bc205fd78bdde89de50c3

As you can see I am on "test project 1" and it's displaying "test 2" content. Also, on Test project 1 (which is the oldest post) the pagination is directing me to the same page instead of Test 2 (newer post)

single-project.php

<?php
  $mypost = array( 'post_type' => 'project', 'posts_per_page' => 1, );
  $loop = new WP_Query( $mypost );
  ?>
  <?php while ( $loop->have_posts() ) : $loop->the_post();?>

  <div class="col-lg-8" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <?php the_content(); ?>
  </div>

  <div class="col-lg-4">
    <h3><?php the_title(); ?></h3>
    <p><?php the_excerpt(); ?></p>
    <h5>Date</h5>
    <p><?php the_date(); ?></p>
    <h5>Link</h5>
    <p>
      <?php $site= get_post_custom_values('project_link_detail');
          if($site[0] != ""){
      ?>
      <a href="http://<?php echo esc_html( get_post_meta( get_the_ID(), 'project_link_detail', true ) ); ?>" target="_blank">Visit the Site</a>
      <?php }else{ ?>
          <em>Link Unavailable</em>
      <?php } ?>
  </div>
  <hr>
  <div class="col-lg-12">
    <div class="row">
      <div class="col-lg-6">
        <?php previous_post_link('%link') ?>
      </div>
      <div class="col-lg-6 text-right">
        <?php next_post_link(' %link') ?>
      </div>
    </div>
  </div>
  <?php endwhile; ?>

portfolio.php

function my_portfolio() {

  // Set the labels, this variable is used in the $args array
  $labels = array(
    'name'               => __( 'Projects' ),
    'singular_name'      => __( 'Project' ),
    'add_new'            => __( 'Add New Project' ),
    'add_new_item'       => __( 'Add New Project' ),
    'edit_item'          => __( 'Edit Project' ),
    'new_item'           => __( 'New Project' ),
    'all_items'          => __( 'All Projects' ),
    'view_item'          => __( 'View Project' ),
    'search_items'       => __( 'Search Project' ),
    'featured_image'     => 'Preview',
    'set_featured_image' => 'Add Preview'
  );

  // The arguments for our post type, to be entered as parameter 2 of register_post_type()
  $args = array(
    'labels'            => $labels,
    'description'       => 'Holds portfolio specific data',
    'public'            => true,
    'menu_position'     => 25,
    'supports'          => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
    'has_archive'       => true,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'has_archive' => 'page template',
    'rewrite'           => true,
    'query_var'         => 'project'
  );

  // Call the actual WordPress function
  // Parameter 1 is a name for the post type
  // Parameter 2 is the $args array
  register_post_type( 'project', $args);
}
1

1 Answers

0
votes

When you create a custom post type, your archive and single files must contain the custom post name like archive-portfolio and single-portfolio. When you do this, Wordpress automaticaly map the query for each file, so in your single-portfolio you don't have to create a new query, if you access to the global $post variable, you can get all the information of the post you are seeing.

Try to remove your custom query, also you can download a clean theme with all that you need from here: http://underscores.me/, there you will see how single file works.