Wordpress | Custom Post | Display Content - wordpress

I am attempting to create a Wordpress theme that calls for a bespoke Post Type.
I have created the post type via functions.php. This seems to have worked and shows up in the admin area and allows me to generate posts. Code exerpt below:
// Register Custom Post Type
function courses_post_type() {
$labels = array(
'name' => _x( 'Courses', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Course', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Training Courses', 'text_domain' ),
'name_admin_bar' => __( 'Course Type', 'text_domain' ),
'archives' => __( 'Course Archives', 'text_domain' ),
'parent_item_colon' => __( 'Parent Course:', 'text_domain' ),
'all_items' => __( 'All Courses', 'text_domain' ),
'add_new_item' => __( 'Add New Course', 'text_domain' ),
'add_new' => __( 'New Course', 'text_domain' ),
'new_item' => __( 'New Course', 'text_domain' ),
'edit_item' => __( 'Edit Course', 'text_domain' ),
'update_item' => __( 'Update Course', 'text_domain' ),
'view_item' => __( 'View Course', 'text_domain' ),
'search_items' => __( 'Search Courses', 'text_domain' ),
'not_found' => __( 'No courses found', 'text_domain' ),
'not_found_in_trash' => __( 'No courses found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$rewrite = array(
'slug' => 'post_type',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'Course', 'text_domain' ),
'description' => __( 'Training Course information pages', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-welcome-learn-more',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'query_var' => 'course',
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'courses', $args );
}
add_action( 'init', 'courses_post_type', 0 );
My question is how do I get the the content from the post to display in my custom theme?
I am currently stuck and encountering errors. Here is where I am upto:
I have created a template called single-course.php and for now not changed anything diffrent to my main template.
However when I go to view the post in the browser I get the error message:
Sorry, no posts matched your criteria.
Within single-course.php I have placed the code below inside the <div></div> where I would like the content to display.
If it helps to visual what I am trying to achieve overall is a custom post type (in my case each post is a training course) and a page that displays each individual custom post. Equally it could be shop and custom post is a product where my single-course.php is supposed to display A particular products information inside a div set aside for the purpose.
// This div shows the post title using a wordpress php function
<div class="page-title bg-alpha-moderateblue">
<h1 style="margin:0px;"><?php echo get_the_title(); ?></h1>
</div>
//This div should show the post or in my case the training course overview
//From my understading I have to use the loop although I only want to SHOW THE CONTENT OF A PARTICULAR CUSTOM (course) POST.
<div class="page-content bg-white">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'courses');
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile;?>
</div>
All constructive feedback and suggestions are greatly appreaciated, thanks in advance

I'm not sure if you wrote all that code (from your functions.php file) yourself or if you snagged it from somewhere else but I recommend using the example code at https://codex.wordpress.org/Post_Types#Custom_Post_Types with as little customization as possible. You may also want to use the code from Wordpress' TwentySixteen single template (https://github.com/WordPress/twentysixteen/blob/master/single.php) after all, who knows Wordpress better than Wordpress? I don't mean use the code in production per say just as a starting point to help you eliminate the possibility that the error is in those two blocks of code.
That said, have you tried renaming single-course.php to single-courses.php? After all that's the name you specified in the register_post_type call.
Hope that helps, good luck!

Problem solved when using the code generator I had failed to change the default 'slug'=> 'post_type' to my custom post type name, in my case "courses".

On a side note if you are using single-courses.php you shouldn't need to use
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'courses');
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile;?>
</div>
If the theme template is named correctly you should just be able to use the standard wordpress loop and markup to display your content.
ie. no need to call another query.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//Content
<?php endwhile; endif; ?>
Unless of course you are modifying the query to get related content within the single.
In which case I'd suggest using get_posts(); with a simple foreach.
$args = array(
'post_type' = 'courses',
'posts_per_page' = -1,
);
<?php $related_posts = get_posts($args);
if ( $related_posts ) : ?>
<?php $temp_post = $post; //temporarily store the $post query for use after the related content
foreach ( $related_posts as $post ) : setup_postdata( $post ); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endforeach;
$post = $temp_post; //set the post back to the main query
endif; ?>
Wordpress codex for get_posts

Related

Custom Post Type Taxonomy remove category slug

I have created a custom post type 'Shop' with a custom taxonomy for Shop Categories
For my shop category archive pages I want the URL structure to be:
example.com/shop/tech/ (instead of example.com/shop/category/tech/)
And I want Shop posts to be example.com/shop/shop-post-title-example
I have tried the code below and saved permalinks but when I visit a Shop post it shows a 404 error.
Is it possible to remove the category base without errors?
// Custom Post Type : Shop
function my_custom_post_shop() {
$labels = array(
'name' => _x( 'Shop', 'post type general name' ),
'singular_name' => _x( 'Product', 'post type singular name' ),
'add_new' => _x( 'Add New', 'Product' ),
'add_new_item' => __( 'Add New' ),
'edit_item' => __( 'Edit' ),
'new_item' => __( 'New Product' ),
'all_items' => __( 'All Products' ),
'view_item' => __( 'View Product' ),
'search_items' => __( 'Search Products' ),
'not_found' => __( 'No projects found' ),
'not_found_in_trash' => __( 'No projects found in the Trash' ),
'menu_name' => 'Shop'
);
$args = array(
'label' => __( 'Shop' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'comments', 'revisions', 'custom-fields', 'thumbnail', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'shop' ),
'has_archive' => 'shop',
);
register_post_type( 'shop', $args );
}
add_action( 'init', 'my_custom_post_shop' );
// Shop Categories
function my_taxonomies_shop() {
$labels = array(
'name' => _x( 'Shop Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Shop Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category' ),
'menu_name' => __( 'Shop Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'show_admin_column' => true,
'rewrite' => array( 'slug' => 'shop'),
);
register_taxonomy( 'shop_category', 'shop', $args );
}
add_action( 'init', 'my_taxonomies_shop', 0 );
You can remove the category base in custom taxonomy URLs with:
$args = array(
'rewrite' => array( 'slug' => '/', 'with_front' => false),
/* ...(other args)... */
);
register_taxonomy( 'shop_category', 'shop', $args );
And re-save Permalinks settings.
However, this will make your (basic post type) posts go 404, if you have permalinks set to Post name. It seems you cannot have both custom taxonomy and posts reside in the root. Therefore you would go to Permalinks settings and set Custom structure , e.g. /blog/%postname%/.
A side effect is that your CPTs would have this "front", too, e.g. blog/yourcustomtype. You can get rid of this with 'with_front' => false in your product type registration:
register_post_type( 'yourcustomtype', array(
'rewrite' => array(
'slug' => 'yourcustomurl',
'with_front' => false
),
/* ... */
));
Found here.
As far as I know, in order to display custom post type and/or its category page, you would need to create template files (corresponding PHP files) first. Template files would pull your posts and display them.
For example, to display your categories "shop_category" you could create a file called "taxonomy-shop_category.php".
If you like to create a template specifically for "tech" Shop Category, then you can create a file called "taxonomy-shop_category_tech.php".
There is a hierarchy of templates: https://wphierarchy.com/
Also more about templates: https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/
Nice article about custom post types taxonomies: https://code.tutsplus.com/tutorials/taxonomy-archives-list-posts-by-post-type--cms-20340
Template files should be placed in your WordPress theme folder (or child theme folder)
!important. Don't forget to update your permalinks. Go to /wp-admin/options-permalink.php and click "Save Changes".
Here is an example of a simple template file:
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
?>
<?php get_header();
$term = get_queried_object();
$args = array(
'post_type' => 'shop',
'shop-category' => $term->slug
);
$query = new WP_Query( $args );
?>
<header class="archive-header">
<h1 class="archive-title">
<?php echo $term->name; ?>
</h1>
</header>
<div id="content">
<?php
if ($query->have_posts()) {
echo'<h2>Your shop category name ' . $term->name . '</h2>';
echo '<ul>';
while ( $query->have_posts() ) : $query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php endwhile;
echo '</ul>';
}
wp_reset_postdata();
?>
</div><!-- #content -->
<?php
get_footer();

Wordpress get_the_terms for custom Taxonomy

Within an archive page, I'm trying to show the a custom taxonomy (called location) along with each post title, category and tag. I can't get the 'get_the_terms' to work.
The post title, category and tag is working perfectly. The taxonomy doesn't work.
<div class="post-listing">
<h4><?php the_title(); ?></h4>
<p><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<p><?php $tags = get_the_tags(); foreach($tags as $tag) { echo "$tag->name"; } ?></p>
<p><?php $terms = get_the_terms( 'locations' ); foreach($terms as $term) { echo "$term->name"; } ?></p>
</div>
This is my functions.php code.
//hook into the init action and call create_locations_nonhierarchical_taxonomy when it fires
add_action( 'init', 'create_locations_nonhierarchical_taxonomy', 0 );
function create_locations_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'Locations', 'taxonomy general name' ),
'singular_name' => _x( 'Location', 'taxonomy singular name' ),
'search_items' => __( 'Search Locations' ),
'popular_items' => __( 'Popular Locations' ),
'all_items' => __( 'All Locations' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Location' ),
'update_item' => __( 'Update Location' ),
'add_new_item' => __( 'Add New Location' ),
'new_item_name' => __( 'New Location Name' ),
'separate_items_with_commas' => __( 'Separate locations with commas' ),
'add_or_remove_items' => __( 'Add or remove locations' ),
'choose_from_most_used' => __( 'Choose from the most used locations' ),
'menu_name' => __( 'Locations' ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('locations','post',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'location' ),
));
}
Any ideas? It's driving me mad!
There are two parameters of get_the_terms function. They are $post and $taxonomy. Unlike the other "get_the_xxxx" functions that you can skip the $post (post object or post ID) parameter inside the loop, you must add the post object or post ID to the first parameter.
I think you should write
$terms = get_the_terms( $post, 'locations' );
Instead of
$terms = get_the_terms( 'locations' );
Documentation: https://developer.wordpress.org/reference/functions/get_the_terms/

WordPress Custom Taxonomy showing 404

I have created a Custom Post Type in WordPress called "Found". Inside that, I have created two taxonomies, "Pets" (named petsfound) and "Electronics" (named electronicsfound) and both of those taxonomies have various terms.
If I view the posts that use the taxonomy terms on the site, they show correctly, however, attempting to view a list of posts for the taxonomy shows a 404. So, the following happens:
https://example.com/found/pets-found/ shows a 404 page.
https://example.com/found/pets-found/dog shows the list of dogs in the pets found taxonomy.
I have tested with using both archive.php and taxonomy-petsfound.php but both show the 404 for the taxonomy. This is the same for the electronics taxonomy too.
Below is the code for the "Found" CPT and "petsfound" Taxonomy:
function found_custom_post_type() {
$labels = array(
'name' => _x( 'Found Items', 'Post Type General Name', 'lost_and_found' ),
'singular_name' => _x( 'Found', 'Post Type Singular Name', 'lost_and_found' ),
'menu_name' => __( 'Found', 'lost_and_found' ),
'name_admin_bar' => __( 'Found', 'lost_and_found' ),
'archives' => __( 'Item Archives', 'lost_and_found' ),
'parent_item_colon' => __( 'Parent Item:', 'lost_and_found' ),
'all_items' => __( 'All Items', 'lost_and_found' ),
'add_new_item' => __( 'Add New Item', 'lost_and_found' ),
'add_new' => __( 'Add New', 'lost_and_found' ),
'new_item' => __( 'New Item', 'lost_and_found' ),
'edit_item' => __( 'Edit Item', 'lost_and_found' ),
'update_item' => __( 'Update Item', 'lost_and_found' ),
'view_item' => __( 'View Item', 'lost_and_found' ),
'search_items' => __( 'Search Item', 'lost_and_found' ),
'not_found' => __( 'Not found', 'lost_and_found' ),
'not_found_in_trash' => __( 'Not found in Trash', 'lost_and_found' ),
'featured_image' => __( 'Featured Image', 'lost_and_found' ),
'set_featured_image' => __( 'Set featured image', 'lost_and_found' ),
'remove_featured_image' => __( 'Remove featured image', 'lost_and_found' ),
'use_featured_image' => __( 'Use as featured image', 'lost_and_found' ),
'insert_into_item' => __( 'Insert into item', 'lost_and_found' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'lost_and_found' ),
'items_list' => __( 'Items list', 'lost_and_found' ),
'items_list_navigation' => __( 'Items list navigation', 'lost_and_found' ),
'filter_items_list' => __( 'Filter items list', 'lost_and_found' ),
);
$args = array(
'label' => __( 'Found', 'lost_and_found' ),
'description' => __( 'Found Post Type', 'lost_and_found' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'petsfound', 'electronicsfound', 'countyfound' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'rewrite' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'found', $args );
}
add_action( 'init', 'found_custom_post_type', 5 );
}
function found_taxonomies_pets() {
$labels = array(
'name' => _x( 'Pets', 'taxonomy general name' ),
'singular_name' => _x( 'Pet', 'taxonomy singular name' ),
'search_items' => __( 'Search Pets' ),
'all_items' => __( 'All Pets' ),
'parent_item' => __( 'Parent Pets' ),
'parent_item_colon' => __( 'Parent Pet:' ),
'edit_item' => __( 'Edit Pet' ),
'update_item' => __( 'Update Pet' ),
'add_new_item' => __( 'Add New Pet' ),
'new_item_name' => __( 'New Pet' ),
'menu_name' => __( 'Pets' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'found/pets-found', 'with_front' => true ),
'show_admin_column' => true,
//'has_archive' => true
);
register_taxonomy( 'petsfound', 'found', $args );
flush_rewrite_rules();
}
add_action( 'init', 'found_taxonomies_pets', 1 );
I have reset the permalinks on multiple occasions.
I have tested amending the hierarchical and with_front values from true to false and back again in case they had any affect.
I have ran print_r($wp_query); on the 404.php template and receive the following for the start of the query_vars:
[query_vars] => Array ( [page] => 0 [found] => pets-found [post_type] => found [name] => pets-found
Not sure what else to look to, so hopefully someone can help.
Cheers
Damien
Edit - Adding the code for taxonomy-petsfound.php
<?php
/**
* The template for displaying Pets Found Taxonomy.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* #package dazzling
*/
get_header(); ?>
<div class="breadcrumb" typeof="BreadcrumbList" vocab="http://schema.org/">
<?php if(function_exists('bcn_display'))
{
bcn_display();
}?>
</div>
<?php print_r($wp_query); ?>
<section id="primary" class="content-area col-sm-12 col-md-12 <?php echo of_get_option( 'site_layout' ); ?>">
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title">
Found Items 2
</h1>
<?php
// Show an optional term description.
$term_description = term_description();
if ( ! empty( $term_description ) ) :
printf( '<div class="taxonomy-description">%s</div>', $term_description );
endif;
?>
</header><!-- .page-header -->
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php dazzling_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
</section><!-- #primary -->
<?php get_footer(); ?>
When it comes to the rewrite rules for post types and taxonomies, sometimes other rules match before the one you prefer. Maybe from a post or page slug. Even if it's one in the trash.
Try reviewing all your rewrite rules and seeing what is matching your URL request. You might need to change the order in which you create your post types or taxonomies. Try registering your taxonomies before your post types.
You can see the matching rewrite rules with this snippet:
function debug_rewrite_rules() {
global $wp, $template, $wp_rewrite;
echo '<pre>';
echo 'Request: ';
echo empty($wp->request) ? "None" : esc_html($wp->request) . PHP_EOL;
echo 'Matched Rewrite Rule: ';
echo empty($wp->matched_rule) ? None : esc_html($wp->matched_rule) . PHP_EOL;
echo 'Matched Rewrite Query: ';
echo empty($wp->matched_query) ? "None" : esc_html($wp->matched_query) . PHP_EOL;
echo 'Loaded Template: ';
echo basename($template);
echo '</pre>' . PHP_EOL;
echo '<pre>';
print_r($wp_rewrite->rules);
echo '</pre>';
}
add_action( 'wp_head', 'debug_rewrite_rules' );
As far as I know if you add custom taxonomies you can access the list of products or anything you want with the help of passing parameters with that like in above if you access the list you have to use the parameter like the below url :
https://example.com/found/pets-found/dog This will give you the result because here dog is the parameters and you got the dog list.
or
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
}
Hope this will help you

custom post type template returns wrong permalink in loop

I created a custom post type(employee bio) and a custom template to display excerpts, attached images, and get the permalink to link to the custom post type.
When I use my custom template to loop through my custom post type it gets the attached image and the excerpt, but when i use get_permalink() in the loop it returns the permalink of the page the template is used on rather than the permalink of each post it's looping through, I exhausted so I'm probably overlooking something.
custom post type (functions.php)
add_action('init', 'employee_bio',1);
function employee_bio() {
$feature_args = array(
'labels' => array(
'name' => __( 'Employee Bios' ),
'singular_name' => __( 'Employee Bio' ),
'add_new' => __( 'Add New Bio' ),
'add_new_item' => __( 'Add New Bio' ),
'edit_item' => __( 'Edit Bio' ),
'new_item' => __( 'New Bio' ),
'view_item' => __( 'View Bio' ),
'search_items' => __( 'Search Employee Bios' ),
'not_found' => __( 'No Employee Bio found' ),
'not_found_in_trash' => __( 'No Employee Bio found in trash' )
),
'public' => true,
'show_ui' => true,
'capability_type' => 'page',
'hierarchical' => true,
'has_archive' => false,
'can_export' => true,
'rewrite' => array('pages' => true, 'with_front' => false),
'menu_position' => 20,
'supports' => array('title', 'editor', 'thumbnail','excerpt', 'page-attributes','post-formats' )
);
register_post_type('bio',$feature_args);
}
custom template (bio_overview.php )
<?php get_header(); ?>
<?php $loop = new WP_Query( array( 'post_type' => 'bio') ); ?>
<div id="primary" class="content-area">
<div id="contentwide" class="site-content" role="main">
<?php while ($loop->have_posts() ) : $loop->the_post(); ?>
<div class="bio_summeries">
<div class="mini_headshot">
<?php echo the_post_thumbnail('full');?>
</div>
<p class="bio_excerpt">
<?php the_excerpt(); ?>
</p>
Read More >
<!--<div class="read_more_bio"></div>-->
</div>
<?php //comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content .site-content -->
</div><!-- #primary .content-area -->
Found it, i had to remove the extra quotes and concatenation from the <a> tag and echo the result. So: Read More > becomes: Read More >
Which got it working. Now, time to get some sleep.

WordPress Custom Post Type Pagination Issue

Both me and my friend are completely bamboozled as to whats going on here. The quest is to have standard pagination linking from one post to the next. We can see the pages going from page/2/, page/3/ etc but the content does not change.
Here is what we've got in the custom template.
<?php
/**
* The Template for displaying all single posts.
*
* Template Name: Portfolio
*
* #package WordPress
* #subpackage Boilerplate
* #since Boilerplate 1.0
*/
get_header();
// Enable Pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array(
'portfolio'
),
'orderby' => 'date',
'posts_per_page' => 1,
'paged'=>$paged
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<article id="item<?php the_ID(); ?>" <?php post_class('post portfolio'); ?>>
<h2><?php the_title(); ?></h2>
<div>
<?php the_content(); ?>
</div>
</article>
<?php endwhile; ?>
<?php next_posts_link('« Older Entries') ?>
<?php previous_posts_link('Newer Entries »') ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
And in the bottom of the functions.php lives some custom post type script...
// Custom Post Type
function foggin_Portfolio() {
$labels = array(
'name' => _x( 'Portfolio', 'post type general name' ),
'singular_name' => _x( 'Portfolio', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New Item' ),
'edit_item' => __( 'Edit item' ),
'new_item' => __( 'New Item' ),
'all_items' => __( 'All Items' ),
'view_item' => __( 'View Item' ),
'search_items' => __( 'Search items' ),
'not_found' => __( 'No item' ),
'not_found_in_trash' => __( 'No items found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Portfolio'
);
$args = array(
'labels' => $labels,
'description' => 'Holds portfolio items and portfolio specific data',
'public' => true,
'menu_position' => 5,
'rewrite' => array('slug'=>'','with_front'=>false),
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'taxonomies'),
'taxonomies' => array('post_tag'),
'has_archive' => true,
);
register_post_type( 'portfolio', $args );
}
add_action( 'init', 'foggin_Portfolio' );
function portfolio_messages( $messages ) {
global $post, $post_ID;
$messages['portfolio'] = array(
0 => '',
1 => sprintf( __('Portfolio item updated. View item'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Product updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Portfolio item restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Portfolio item published. View item'), esc_url( get_permalink($post_ID) ) ),
7 => __('Portfolio item saved.'),
8 => sprintf( __('Portfolio item submitted. <a target="_blank" href="%s">Preview item</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Portfolio item scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview item</a>'), date_i18n( __( 'M j, Y # G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __('Portfolio item draft updated. <a target="_blank" href="%s">Preview item</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}
add_filter( 'post_updated_messages', 'portfolio_messages' );
function portfolio_taxonomies() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category' ),
'menu_name' => __( 'Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'portfolio_category', 'portfolio', $args );
}
add_action( 'init', 'portfolio_taxonomies', 0 );
?>
Thoughts, ideas, advice would be really helpful I think it's fair to say we're both stumped on this.
It may sound silly, but reset your permalinks to default, then back to your desired format. The .htaccess file needs to be rewritten after the post types have been added into functions.php
Your action for rewrite option being set to false could be causing an issue as well. If WP isn't clear on rewriting yourdomain.com/page/2/ so having the rewrite to true makes it yourdomain.com/portfolio/page/2/
Everything else seems to be in order with your query.

Resources