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
Related
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();
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/
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
Yep, I have been searching all over, and seems the only reason this should be happening is because it is not checked in the Screen Options menu in WordPress. My problem is, the custom post type is not showing in Screen Options so I was wondering if someone could have a look at my code and see if I'm doing some thing wrong.
function my_custom_post_maryland() {
$labels = array(
'name' => _x( 'Maryland', 'post type general name' ),
'singular_name' => _x( 'Maryland', 'post type singular name' ),
'add_new' => _x( 'Add New', 'Maryland page' ),
'add_new_item' => __( 'Add New Maryland Page' ),
'edit_item' => __( 'Edit Page' ),
'new_item' => __( 'New Page' ),
'all_items' => __( 'All Pages' ),
'view_item' => __( 'View Page' ),
'search_items' => __( 'Search Maryland Pages' ),
'not_found' => __( 'No Maryland pages found' ),
'not_found_in_trash' => __( 'No Maryland pages found in the Trash' ),
'parent_item_colon' => __( 'Parent Page' ),
'parent_item_colon' => '',
'menu_name' => 'Maryland'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our Maryland pages specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'show_ui' => true,
'has_archive' => true,
'show_in_nav_menus' => true
);
register_post_type( 'maryland', $args );
}
add_action( 'init', 'my_custom_post_maryland' );
You still need to add it to the menu using
function setup_bonsai_theme_admin_menus()
{
add_menu_page('MyTheme settings', 'My Theme Settings', 'manage_options',
'my_theme_settings', 'my_page_settings');
add_submenu_page('my_theme_settings',
'My Page Elements', 'maryland', 'manage_options',
'my_questions_settings', 'my_maryland_settings');
}
and
add_action('admin_menu', 'setup_bonsai_theme_admin_menus');
and
function my_page_settings()
{
?>
<div class="wrap">
<h2>There are no theme options at this stage.</h2>
</div>
<?php
}
function my_maryland_settings()
{
?>
<div class="wrap">
<h2>Maryland</h2>
</div>
<?php
}
After that you also need to add a new template file for single posts as well as archive posts, etc, so you can actually view them. The wordpress codex for 'theme templates' should help you there, as it's quite easy.
Here's a working example.
functions.php
<?php
function setup_bonsai_theme_admin_menus()
{
add_menu_page('SCHS Theme settings', 'SCHS Theme Settings', 'manage_options',
'SCHS_theme_settings', 'SCHS_page_settings');
add_submenu_page('SCHS_theme_settings',
'SCHS Page Elements', 'Questions', 'manage_options',
'SCHS_questions_settings', 'SCHS_questions_setings');
add_submenu_page('SCHS_theme_settings',
'SCHS Page Elements', 'Answers', 'manage_options',
'SCHS_ansers_settings', 'SCHS_answers_settings');
}
function SCHS_page_settings()
{
?>
<div class="wrap">
<h2>There are no theme options at this stage.</h2>
</div>
<?php
}
function SCHS_questions_setings()
{
?>
<div class="wrap">
<h2>Questions</h2>
</div>
<?php
}
function SCHS_answers_settings()
{
?>
<div class="wrap">
<h2>Answers</h2>
</div>
<?php
}
function question_custom_post()
{
$labels = array(
'name' => _x( 'Questions', 'post type general name' ),
'singular_name' => _x( 'Questions', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New Question' ),
'edit_item' => __( 'Edit Question' ),
'new_item' => __( 'New Question' ),
'all_items' => __( 'All Questions' ),
'view_item' => __( 'View Questions' ),
'search_items' => __( 'Search Questions' ),
'not_found' => __( 'No question found' ),
'not_found_in_trash' => __( 'No question found in the trash' ),
'parent_item_colon' => '',
'menu_name' => 'SCHS Questions'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our questions and question specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail'),
'has_archive' => true,
);
register_post_type( 'question', $args );
}
add_action( 'init', 'question_custom_post' );
add_action('admin_menu', 'setup_bonsai_theme_admin_menus');
?>
single-question.php
<?php
/**
* The template for displaying questions and answers
*
* #package WordPress
* #subpackage SCHS
* #since Twenty Fifteen 1.0
*/
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header>
<div class="entry-content">
<?php while ( have_posts() ) : the_post(); ?>
<nav id="nav-single">
<h3 class="assistive-text"><?php _e( 'Post navigation', 'catchbox' ); ?></h3>
<span class="nav-previous"><?php previous_post_link( '%link', __( '<span class="meta-nav">←</span> Previous', 'catchbox' ) ); ?></span>
<span class="nav-next"><?php next_post_link( '%link', __( 'Next <span class="meta-nav">→</span>', 'catchbox' ) ); ?></span>
</nav><!-- #nav-single -->
<?php endwhile; // end of the loop. ?>
</div>
<?php
wp_reset_query();
the_content();
?>
</article>
</main>
</div>
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
?>
SCHS-questions.php
<?php
/*
Template Name: SCHS Questions
Description: This part is optional, but helpful for describing the Post Template
*/
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<span id="schs-ask-a-question">Ask a question</span>
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header>
<div class="entry-content">
<?php
the_content();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'question','posts_per_page' =>0,'paged' => $paged,'orderby'=>'title','order'=>'asc');
$myposts = query_posts($args);
?>
<fieldset id="schs-question-list">
<legend>The Questions</legend>
<ul>
<?php
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li class="main_question_list_item">
<a href="<?php echo get_permalink($post->id);?>">
<span class="question_class"><?php the_title();?></span><br/>
<!--
<?php $custom = get_post_custom($post->ID);?>
<?php $grower = $custom["tree_grower"][0];?>
<span class="grower_name"><?php echo $grower;?></span>
-->
</a>
</li>
<?php endforeach; ?>
</ul>
</fieldset>
<?php
?>
</div>
</article>
</main>
</div>
</div>
<?php
get_sidebar();
get_footer();
?>
You also need to register the menu in your functions.php in theme:
add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );
}
function pb_custom_post_cases() {
$labels = array(
'name' => _x( 'Recent Cases', 'post type general name' ),
'singular_name' => _x( 'Recent Case', 'post type singular name' ),
'add_new' => _x( 'Add New', 'case' ),
'add_new_item' => __( 'Add New Case' ),
'edit_item' => __( 'Edit Case' ),
'new_item' => __( 'New Case' ),
'all_items' => __( 'All Cases' ),
'view_item' => __( 'View Cases' ),
'search_items' => __( 'Search Cases' ),
'not_found' => __( 'No cases found' ),
'not_found_in_trash' => __( 'No cases found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Recent Cases'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our recent cases specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
'has_archive' => true,
'rewrite' => array('slug' => 'cases', 'with_front'=> false)
);
register_post_type( 'cases', $args );
}
add_action( 'init', 'pb_custom_post_cases' );
/* Taxonomy Recent cases */
function my_taxonomies_cases() {
$labels = array(
'name' => _x( 'Case Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Case Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Case Categories' ),
'all_items' => __( 'All Case Categories' ),
'parent_item' => __( 'Parent Case Category' ),
'parent_item_colon' => __( 'Parent Case Category:' ),
'edit_item' => __( 'Edit Case Category' ),
'update_item' => __( 'Update Case Category' ),
'add_new_item' => __( 'Add New Case Category' ),
'new_item_name' => __( 'New Case Category' ),
'menu_name' => __( 'Case Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'cases_category', 'cases', $args );
}
add_action( 'init', 'my_taxonomies_cases', 0 );
/* Custom Messages - Recent Cases */
function pb_cases_updated_messages( $messages ) {
global $post, $post_ID;
$messages['cases'] = array(
0 => '',
1 => sprintf( __('Recent Case Updated. View case'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Case updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Case restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Recent Case published. View Case'), esc_url( get_permalink($post_ID) ) ),
7 => __('Product saved.'),
8 => sprintf( __('Case submitted. <a target="_blank" href="%s">Preview case</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Case scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview case</a>'), date_i18n( __( 'M j, Y # G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __('RecentCase draft updated. <a target="_blank" href="%s">Preview case</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}
add_filter( 'post_updated_messages', 'pb_cases_updated_messages' );
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.