Taxonomy link not showing in custom admin menu wordpress - php

I found many solutions for this but my scenario is total different.
Now my issue is, i am working on a plugin and in plugin i make custom post type and custom taxonomy, firstly its slugs are different, now i want that post type slug and taxonomy slugs are same and i also achieve this successfully but there is a one problem that is my category link is not showing in admin menu because when i register taxonomy i set object_type as slug which is same in post type slug and taxonomy slug but when i change the object type as post type name category menu showing perfectly.
Here is my code
Post Type Code:
add_action('init', 'kbe_articles');
function kbe_articles() {
$labels = array(
'name' => __('Articles', 'kbe'),
'singular_name' => __('Articles', 'kbe'),
'all_items' => __('Articles', 'kbe'),
'add_new' => __('New Article', 'kbe'),
'add_new_item' => __('Add New Article', 'kbe'),
'edit_item' => __('Edit Article', 'kbe'),
'new_item' => __('New Article', 'kbe'),
'view_item' => __('View Articles', 'kbe'),
'search_items' => __('Search Articles', 'kbe'),
'not_found' => __('Nothing found', 'kbe'),
'not_found_in_trash' => __('Nothing found in Trash', 'kbe'),
'parent_item_colon' => ''
);
$kbe_rewrite = array(
'slug' => KBE_PLUGIN_SLUG,
'with_front' => false,
'pages' => true,
'feeds' => true,
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => WP_Articles.'images/icon-kbe.png',
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 3,
'supports' => array('title','editor','thumbnail','comments'),
'rewrite' => $kbe_rewrite,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true
);
register_post_type( 'kbe_articles' , $args );
flush_rewrite_rules();
}
Taxonomy Code:
add_action( 'init', 'kbe_taxonomies');
function kbe_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => __( 'Articles Category', 'kbe'),
'singular_name' => __( 'Articles Category', 'kbe' ),
'search_items' => __( 'Search Articles Category', 'kbe' ),
'all_items' => __( 'All Articles Categories', 'kbe' ),
'parent_item' => __( 'Parent Articles Category', 'kbe' ),
'parent_item_colon' => __( 'Parent Articles Category:', 'kbe' ),
'edit_item' => __( 'Edit Articles Category', 'kbe' ),
'update_item' => __( 'Update Articles Category', 'kbe' ),
'add_new_item' => __( 'Add New Articles Category', 'kbe' ),
'new_item_name' => __( 'New Articles Category Name', 'kbe' ),
);
register_taxonomy( 'kbe_taxonomy', array(KBE_PLUGIN_SLUG), array(
'hierarchical' => true,
"label" => 'Categories',
"singular_label" => "Articles Category",
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => true,
'rewrite' => array( 'slug' => KBE_PLUGIN_SLUG, 'with_front' => true ),
));
flush_rewrite_rules();
}
And this is the code so my post type slug and taxonomy slug are same:
function taxonomy_slug_rewrite($wp_rewrite) {
$rules = array();
$taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
$post_types = get_post_types(array('public' => true, '_builtin' => false), 'names');
foreach ($post_types as $post_type) {
$post_type_data = get_post_type_object( $post_type );
$post_type_slug = $post_type_data->rewrite['slug'];
foreach ($taxonomies as $taxonomy) {
if ($taxonomy->object_type[0] == $post_type_slug) {
$categories = get_categories(array('type' => $post_type_slug, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
/* #var $category type */
foreach ($categories as $category) {
$rules[$post_type_slug . '/' . $category->slug . '/?$'] = 'index.php?' . $category->taxonomy . '=' . $category->slug;
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'taxonomy_slug_rewrite' );
And here is my menu code:
add_action('admin_menu', 'kbe_plugin_menu');
function kbe_plugin_menu() {
add_submenu_page('edit.php?post_type=kbe_articles', 'Order', 'Order', 'manage_options', 'kbe_order', 'wp_kbe_order');
add_submenu_page('edit.php?post_type=kbe_articles', 'Settings', 'Settings', 'manage_options', 'kbe_options', 'wp_kbe_options');
}
Now when i change register_taxonomy( 'kbe_taxonomy', array('kbe_articles'), My taxonomy link appears in admin custom menu but when i change register_taxonomy( 'kbe_taxonomy', array(KBE_PLUGIN_SLUG), taxonomy link disapears from menu.
So what can i do that my taxonomy link appear in menu. but not change slugs.

Ok here is answer.
I add one more sub menu on a condition
here is my code:
add_submenu_page('edit.php?post_type=foo_articles', 'Categories', 'Categories', 'manage_options', 'edit-tags.php?taxonomy=foo_taxonomy&post_type=foo_articles');

Related

Custom URL in custom post type, using taxonomy slug

I need a URL like
www.example.com/taxonomy/taxonomy-slug/post-slug
I can get you, but there's something wrong.
I created two items in my taxonomy; ITEM1 and ITEM2
I created a post and I stuck to these two items
On the taxonomy.php page, I opened both pages, that is
www.example.com/taxonomy/item1/
www.example.com/taxonomy/item2/
So far so good, but the post link is coming like this for both pages
www.example.com/taxonomy/item1/post-slug
www.example.com/taxonomy/item1/post-slug
And in fact, it should come as
www.example.com/taxonomy/item1/post-slug
www.example.com/taxonomy/item2/post-slug
My code below
// Create a post type
function custom_post_type1() {
$labels = array(
'name' => _x( 'Curso', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Curso', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Curso', 'twentythirteen' ),
'parent_item_colon' => __( 'Curso Relacionado', 'twentythirteen' ),
'all_items' => __( 'Todos os Curso', 'twentythirteen' ),
'view_item' => __( 'Ver Curso', 'twentythirteen' ),
'add_new_item' => __( 'Adicionar novo Curso', 'twentythirteen' ),
'add_new' => __( 'Adicionar novo', 'twentythirteen' ),
'edit_item' => __( 'Editar Curso', 'twentythirteen' ),
'update_item' => __( 'Atualizar Curso', 'twentythirteen' ),
'search_items' => __( 'Buscar por Curso', 'twentythirteen' ),
'not_found' => __( 'Nenhum Curso encontrado', 'twentythirteen' ),
'not_found_in_trash' => __( 'Nenhum Curso encontrado na lixeira', 'twentythirteen' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'curso', 'twentythirteen' ),
'description' => __( 'curso news and reviews', 'twentythirteen' ),
'rewrite' => true,
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'menu_icon' => '',
'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,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'rewrite' => array( 'slug' => 'curso/%actor%', 'with_front' => false ),
'has_archive' => 'curso'
);
// Registering your Custom Post Type
register_post_type( 'curso', $args );
}
// Create a taxonomy
function add_actor_taxonomy() {
if (!is_taxonomy('actor')) {
register_taxonomy('actor', 'curso' ,
array(
'hierarchical' => FALSE,
'label' => __('actor'),
'public' => TRUE,
'show_ui' => TRUE,
'query_var' => 'actor',
'rewrite' => array( 'slug' => 'unidade', 'with_front' => false )
)
);
}
}
add_action('init', 'add_actor_taxonomy');
//Custom Permalink
function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'curso' ){
$terms = wp_get_object_terms( $post->ID, 'actor' );
if( $terms ){
return str_replace( '%actor%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
I hope to return the following
www.example.com/taxonomy/item1/post-slug
www.example.com/taxonomy/item2/post-slug
Can you see something wrong with my code?

WordPress taxonomy doesn't work for my custom post type

I try to create custom post with a taxonomy in WordPress for my theme, my custom post works fine but my taxonomy doesn't show in admin wordpress panel.
I write this part of code in other page named library.inc.php
and I include it in function.php
I cant find where exactly is problem?
here is my code
function p2p2_register_book(){
$labels = array(
'name' => _x( 'کتابخانه', 'books'),
'singular_name' => _x( 'کتاب', 'book' ),
'add_new' => _x( 'افزودن کتاب', '' ),
'add_new_item' => __( 'افزودن کتاب جدید' ),
'edit_item' => __( 'ویرایش کتاب' ),
'new_item' => __( 'کتاب جدید' ),
'all_items' => __( 'همه کتاب ها' ),
'view_item' => 'View Book',
'search_items' => __( 'جست و جو کتاب' ),
'not_found' => _( 'کتاب یافت نشد' ),
'not_found_in_trash' => __( 'کتاب در زباله دان یافت نشد' ),
'parent_item_colon' => '',
'menu_name' => 'کتابخانه'
);
$args=array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'menu_position' => 2,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ,'custom-fields'),
// 'taxonomies' => array( 'Books_category','post_tag' ),
);
register_post_type('Book',$args);
}
add_action('init', 'p2p2_register_book');
and here is taxonomy part
function wp_library_post_taxonomy() {
register_taxonomy(
'Books_category', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'Book', //post type name
array(
'hierarchical' => true,
'label' => 'دسته بندی کتاب', //Display name
'query_var' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'Books-category', // This controls the base slug that will display before each term
'with_front' => true // Don't display the category base before
)
)
);
}
add_action( 'init', 'wp_library_post_taxonomy');
include_once( 'mixitup-plug.php' );
add_theme_support('post_thumbnails');
add_image_size('Books_small_image',150,200,true);
here is the answer
I spend near 6 hours to find the problem but no result so I rewrite the taxonomy part with exact name and .....
problem solved.
I don't know exactly what's wrong with WordPress. really 6 hours for nothing?
use following code to register custom taxonomy it will definetly work. :)
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'wp_library_post_taxonomy', 0 );
//create a custom taxonomy name it topics for your posts
function wp_library_post_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
$labels = array(
'name' => _x( 'Taxonomy Names', 'taxonomy general name' ),
'singular_name' => _x( 'Taxonomy Name', 'taxonomy singular name' ),
'search_items' => __( 'Search Taxonomy Names' ),
'all_items' => __( 'All Taxonomy Names' ),
'parent_item' => __( 'Parent Taxonomy Names' ),
'parent_item_colon' => __( 'Parent Taxonomy Name:' ),
'edit_item' => __( 'Edit Taxonomy Name' ),
'update_item' => __( 'Update Taxonomy Name' ),
'add_new_item' => __( 'Add New Taxonomy Name' ),
'new_item_name' => __( 'New Granite Taxonomy Name' ),
'menu_name' => __( 'Taxonomy Name' ),
);
// Now register the taxonomy
register_taxonomy('Taxonomy Name',array('custom_post_type name'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'Taxonomy Name' ),
));
}

Restrict category for custom post type in wordpress

I have created a custom post type for adding employees. I have added the category taxonomy for it. Then I have added a category named "Employees" (slug: employees). But when I add a new employee, it shows me all the categories to choose. How can I restrict it to show only "Employees" category here?
Here is my php code in functions.php:
function employees_register() {
$labels = array(
'name' => _x('Employees', 'post type general name'),
'singular_name' => _x('Employee', 'post type singular name'),
'add_new' => _x('Add New', 'employees'),
'add_new_item' => __('Add New Employee'),
'edit_item' => __('Edit Employee'),
'new_item' => __('New Employee'),
'view_item' => __('View Employee'),
'search_items' => __('Search Employee'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'taxonomies' => array( 'category' ),
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'employees' , $args );
}
add_action('init', 'employees_register');
You should register custom taxonomies with the register_taxonomy() function:
add_action( 'init', 'employee_tax' );
function create_book_tax() {
register_taxonomy(
'employee-categories',
'employees',
array(
'label' => __( 'Employee Categories' ),
'hierarchical' => true,
)
);
}
https://codex.wordpress.org/Function_Reference/register_taxonomy
Then, only these taxonomies will show as options for the post type, rather than Categories of other post types.

How to make custom post type single page

I make a custom post type foo_work and in theme folder, i make a folder foo, in foo folder i make single-foo_work.php
But it does not work, when i click on custom post link it show me single.php lay out,
not single-foo_work.php
Is there extra code to write in single-foo_work.php file.
In write a link like this:
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
in single-foo_work.php i write this query
<?php
if(have_posts()) :
while(have_posts()) :
the_post();
?>
Content
<?php
endwhile;
else :
echo "No Post";
endif;
?>
Here is my custom post type code:
add_action('init', 'foo_articles');
function foo_articles() {
// Get Knowledge Base slug from settings
$foo_slug_option = 'foo_work';
$foo_slug_option = get_option('foo_plugin_slug');
$labels = array(
'name' => _x('work', 'post type general name'),
'singular_name' => _x('work', 'post type singular name'),
'add_new' => _x('Add New work', 'work'),
'add_new_item' => __('Add New work'),
'edit_item' => __('Edit work'),
'new_item' => __('New work'),
'view_item' => __('View work'),
'search_items' => __('Search work'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$foo_rewrite = array(
'slug' => $foo_slug_option,
'with_front' => false,
'pages' => true,
'feeds' => true,
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => WP_WORK.'images/icon-foo.png',
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 3,
'supports' => array('title','editor','thumbnail','comments'),
'rewrite' => $foo_rewrite,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true,
'publicly_queryable' => true,
'capability_type' => 'post'
);
register_post_type( 'foo_work' , $args );
}
add_action( 'init', 'foo_taxonomies', 0 );
// Article taxonamy
function foo_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'work Category', 'taxonomy general name' ),
'singular_name' => _x( 'work Category', 'taxonomy singular name' ),
'search_items' => __( 'Search work Category' ),
'all_items' => __( 'All work Categories' ),
'parent_item' => __( 'Parent work Category' ),
'parent_item_colon' => __( 'Parent work Category:' ),
'edit_item' => __( 'Edit work Category' ),
'update_item' => __( 'Update work Category' ),
'add_new_item' => __( 'Add New work Category' ),
'new_item_name' => __( 'New work Category Name' ),
);
register_taxonomy( 'foo_cat', array( 'foo_work' ), array(
'hierarchical' => true,
"label" => 'Category',
"singular_label" => "work Category",
'show_ui' => true,
'query_var' => true,
'rewrite' => false
));
}
any idea what can i do
If you must want to add single-foo_work.php in a foo folder,
Then You can do something like this:-
Add following code in your single.php file:-
<?php
global $post;
if($post->post_type == 'foo_work')
{
get_template_part( 'foo/single', 'foo_work' );
}
else
{
//Your Original Single.php Content.
}
I hope this will help you...

Wordpress custom post type category listings not showing up

I have set up a custom post type called products:
add_action( 'init', 'register_cpt_product' );
function register_cpt_product()
{
$labels = array(
'name' => _x( 'products', 'products' ),
'singular_name' => _x( 'product', 'products' ),
'add_new' => _x( 'Add New', 'products' ),
'add_new_item' => _x( 'Add New product', 'products' ),
'edit_item' => _x( 'Edit product', 'products' ),
'new_item' => _x( 'New product', 'products' ),
'view_item' => _x( 'View product', 'products' ),
'search_items' => _x( 'Search products', 'products' ),
'not_found' => _x( 'No products found', 'products' ),
'not_found_in_trash' => _x( 'No products found in Trash', 'products' ),
'parent_item_colon' => _x( 'Parent product:', 'products' ),
'menu_name' => _x( 'products', 'products' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'taxonomies' => array( 'category', 'post_tag' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'products', $args );
}
The products work with my custom archive page. But not with the categories; I just get a "not found".
http://drysuits.dannycheeseman.me/category/lights/
I have deleted .htaccess and resaved the permalinks.. (/%postname%/)
Any ideas as to why this is happening?
Use for custom taxonomy category list plugin for display category sidebar. or
You can use the wp_list_categories function to create a list in your sidebar, the Codex article has a code example, but something like this should work:
<ul>
<?php wp_list_categories('taxonomy=products'); ?>
</ul>

Categories