I'm learning php & wordpress but this escapes from my knowledge. I have a portfolio theme that I want to separate for SEO pourposes.
I have this. (productos is the current portfolio slug)
website.com/productos/portfoliopost1 (category-a slug)
website.com/productos/portfoliopost2 (category-b slug)
But this is that I want to show:
website.com/category-a/portfoliopost1 (category-a slug)
website.com/category-b/portfoliopost2 (category-b slug)
It's possible? Thanks for all.
This is the current portfolio.php that generate the slug.
if ( !function_exists( 'pexeto_register_portfolio_post_type' ) ) {
/**
* Registers the portfolio custom type.
*/
function pexeto_register_portfolio_post_type() {
//the labels that will be used for the portfolio items
$labels = array(
'name' => _x( 'Portfolio', 'portfolio name', 'pexeto_admin' ),
'singular_name' => _x( 'Portfolio Item', 'portfolio type singular name', 'pexeto_admin' ),
'add_new' => _x( 'Add New', 'portfolio', 'pexeto_admin' ),
'add_new_item' => __( 'Add New Item', 'pexeto_admin' ),
'edit_item' => __( 'Edit Item', 'pexeto_admin' ),
'new_item' => __( 'New Portfolio Item', 'pexeto_admin' ),
'view_item' => __( 'View Item', 'pexeto_admin' ),
'search_items' => __( 'Search Portfolio Items', 'pexeto_admin' ),
'not_found' => __( 'No portfolio items found', 'pexeto_admin' ),
'not_found_in_trash' => __( 'No portfolio items found in Trash', 'pexeto_admin' ),
'parent_item_colon' => ''
);
//register the custom post type
register_post_type( PEXETO_PORTFOLIO_POST_TYPE,
array( 'labels' => $labels,
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array( 'slug'=> 'productos', 'with_front' => false ),
'taxonomies' => array( PEXETO_PORTFOLIO_TAXONOMY ),
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'page-attributes' ) ) );
}}
You can use post_type_link hook for this.
Here you can find explanation:
WordPress Exchange
WordPress Exchange2
Related
I'm theme developer and I want register custom taxonomy like "way" to one of learnpress plugin's post type (i know learnpress plugin register "lp_course" as post type)
I want "way taxonomy" share with "lp_press" and "post" post types!
This is my code that i registered my taxonomy :
// Register Custom Taxonomy
function custom_taxonomy() {
$labels = array(
'name' => _x( 'ways', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'way', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Taxonomy', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'New Item Name', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'way', array( 'post ', ' lp_course' ), $args );
}
add_action( 'init', 'custom_taxonomy', 0 );
Taxonomy added
But when i add taxonomy in my post or course this error apear on taxonomy-term page :
Register a custom post by the following code
<?php
/*Add the code below in function.php */
function post_type_movie() {
$labels = array(
'name' => _x( 'movie', 'Post Type General Name'), // it apper on top
'singular_name' => _x( 'movie', 'Post Type Singular Name'),
'menu_name' => __( 'movie'),//name that display on menu
'parent_item_colon' => __( 'Parent movie'),
'all_items' => __( 'All movie'),
'view_item' => __( 'View movie'),//it see on menu
'add_new_item' => __( 'Add New movie'),//it apper on tp when try add new post
'add_new' => __( 'Add New'),
'edit_item' => __( 'Edit movie'),
'update_item' => __( 'Update movie'),
'search_items' => __( 'Search movie'),// it apper on top of the search
'not_found' => __( 'Not Found'),
'not_found_in_trash' => __( 'Not found in Trash'),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'movie'),
'description' => __( 'movie'),
'labels' => $labels,
'supports' => array( 'title','thumbnail','editor'),
'taxonomies' => array('languge','type','time'),
'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,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'movie', $args );
}
add_action( 'init', 'post_type_movie', 0 );
?>
Register a custom taxonomy by the following code
<?php
function tax_languge() {
register_taxonomy( 'languge',//catagory naem
'movie',//post type name
array(
'label' => __( 'Languge' ), //Label that shhow on backend
'rewrite' => array( 'slug' => 'languge' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'tax_languge', 0 );//call
?>
Here Post type movie (Replace With Your Won)
Here taxonomy languge (Replace With Your Won)
I created three Custom Post Types, one of them is called "TV". I also created a category in the WP dashboard that appears in all those three Custom Post Types as well the "Posts" too. I assigned several posts from those Custom Post Types and Posts to the category. However, when I tried to display the category both in "the_query" loop and the Category Posts Widget plugin, the category did not display the posts from the Custom Post Types. It only display posts that was from the "Posts".
I looked online and the Wordpress Codex in https://codex.wordpress.org/Function_Reference/register_post_type said that "When registering a post type, always register your taxonomies using the taxonomies argument".
I don't really get it. I thought that Taxonomy is same as Category, right? And that it's already called in register_post_type() when I added taxonomies' => array( 'category', 'post_tag' ) in this code below:
// Register Custom Post Type
function tvs_post_type() {
$labels = array(
'name' => _x( 'TV', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'TV', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'TV', 'text_domain' ),
'name_admin_bar' => __( 'Post Type', 'text_domain' ),
'archives' => __( 'Item Archives', 'text_domain' ),
'parent_item_colon' => __( 'Parent TV:', 'text_domain' ),
'all_items' => __( 'All TVs', 'text_domain' ),
'add_new_item' => __( 'Add New Post', 'text_domain' ),
'add_new' => __( 'Add New Post', 'text_domain' ),
'new_item' => __( 'New Item', 'text_domain' ),
'edit_item' => __( 'Edit Post', 'text_domain' ),
'update_item' => __( 'Update Post', 'text_domain' ),
'view_item' => __( 'View Post', 'text_domain' ),
'search_items' => __( 'Search Posts', 'text_domain' ),
'not_found' => __( 'No posts found', 'text_domain' ),
'not_found_in_trash' => __( 'Not posts 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' ),
);
$args = array(
'label' => __( 'TV', 'text_domain' ),
'description' => __( 'tv information pages', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'custom-fields', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'tv', $args );
}
add_action( 'init', 'tvs_post_type', 0 );
Help? So confused....
Never mind. I figured most of it out. What is need to call the category for the Custom Post Types is to add post_type to the query as in
<?php
$args = array (
'cat' => 3,
'post_type' => array( 'post', 'tv', 'print', 'radio' ), //defined the post type. The default is post.
'posts_per_page' => 3
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
wp_reset_postdata();
?>
As for the Category Posts Widget plugin, it looks like it only display the posts with the post_type => post in the query, I think? So I went with Ultimate Posts Widget plugin and it works.
I'm trying to separate the category lists for a specific post type "announcement" from the post categories.
I want my announcement post type to have its own category list and not mix with the post category list
Here's my code
function announcement_post_types() {
register_post_type(
'announcement',
array(
'labels' => array(
'name' => __( 'Announcements' ),
'singular_name' => __( 'announcement' ),
'menu_name' => __( 'Announcements' ),
'all_items' => __( 'All Announcements' ),
'view_item' => __( 'View Announcement' ),
'add_new_item' => __( 'New Announcement' ),
'add_new' => __( 'New Announcement' ),
'edit_item' => __( 'Edit Announcements' ),
'update_item' => __( 'Update' ),
'search_items' => __( 'Search' ),
'not_found' => __( 'Not Found' ),
'not_found_in_trash' => __( 'Not Found in Trash' ),
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'announcement'),
'menu_icon' => 'dashicons-admin-page',
'supports' => array('title', 'editor', 'custom-fields')
)
);
register_taxonomy(
'announcement_type',
'announcement', // this is the custom post type(s) I want to use this taxonomy for
array(
'hierarchical' => false,
'label' => 'News Types',
'query_var' => true,
'rewrite' => true
)
);
register_taxonomy_for_object_type('category', 'announcement');
}
add_action('init', 'announcement_post_types');
If this code is working, this is what links the regular categories to the announcement post type:
register_taxonomy_for_object_type('category', 'announcement');
We need to exchange it with the new taxonomy you created and registered, so exchange it with this:
register_taxonomy_for_object_type('announcement_type', 'announcement');
Let me know if this did the trick for you, there might be more issues here, but this might also be enough.
UPDATE:
I removed the register_taxonomy_for_object_type('category', 'announcement'); and changed into this
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' =>_x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Announcement Category' ),
'update_item' => __( 'Update Announcement Category' ),
'add_new_item' => __( 'Add New Announcement Category' ),
'new_item_name' => __( 'New Announcement Category' ),
'separate_items_with_commas' => __( 'Separate categories with commas' ),
'add_or_remove_items' => __( 'Add or remove Announcement categories' ),
'choose_from_most_used' => __( 'Choose from the most used categories' )
);
register_taxonomy(
'announcement_type',
'announcement', // this is the custom post type(s) I want to use this taxonomy for
array(
'hierarchical' => false,
'label' => 'News Types',
'query_var' => true,
'label' => __('Announcement Category'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'rewrite' => true
)
);
It works for me now,
If you have better answer for this please share :) I want to make this code shorter.
How to create a second category menu like Seminars >> seminars >> create new seminars >> seminar categories and then I want to duplicate seminar categories and make it Seminar venues with inside remaining the same.
See attached screenshot:
As per my understanding, you just want to create a new taxonomy under Seminars. If you make changes in theme files you can edit the function.php and add the following code
$labels = array(
'name' => _x( 'Seminar Venue', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Venue', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Venue', 'textdomain' ),
'all_items' => __( 'All Venues', 'textdomain' ),
'parent_item' => __( 'Parent Venue', 'textdomain' ),
'parent_item_colon' => __( 'Parent Venue:', 'textdomain' ),
'edit_item' => __( 'Edit Venue', 'textdomain' ),
'update_item' => __( 'Update Venue', 'textdomain' ),
'add_new_item' => __( 'Add New Venue', 'textdomain' ),
'new_item_name' => __( 'New VenueName', 'textdomain' ),
'menu_name' => __( 'Venue', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'seminars' ), $args );
Or you can use the plugin Custom Post Type UI. In this plugin go to taxonomy and add a new taxonomy under your post type "seminars"
I use a custom post type, which also should have a custom taxonomy. When I visit the archive page /kollektion/ I can see all items of this CPT, when I visit /kollektion/mytaxonomy1/ I can see all items which are assigned to this taxonomy.
When I click on the single item of the custom post type, I get a 404 error. I also tried to go into my WP dashboard and change and resave the permalinks again - with no success.
Does anybody see an error in my declaration which causes the 404 error on single pages of my custom post type?
Thanks.
functions.php: Taxonomy
// register two taxonomies to go with the post type
function sm_register_taxonomy() {
// set up labels
$labels = array(
'name' => 'Kollektionen Categories',
'singular_name' => 'Kollektion Category',
'search_items' => 'Search Kollektion Categories',
'all_items' => 'All Kollektion Categories',
'edit_item' => 'Edit Kollektion Category',
'update_item' => 'Update Kollektion Category',
'add_new_item' => 'Add New Kollektion Category',
'new_item_name' => 'New Kollektion Category',
'menu_name' => 'Kollektion Categories'
);
// register taxonomy
register_taxonomy( 'kollektionen', 'kollektion', array(
'hierarchical' => true,
'labels' => $labels,
'query_var' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'kollektion', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
),
) );
}
add_action( 'init', 'sm_register_taxonomy' );
functions.php: Custom post type
// Register Custom Post Type Kollektion
function post_type_kollektion() {
$labels = array(
'name' => _x( 'Kollektionen', 'Post Type General Name', 'genesis' ),
'singular_name' => _x( 'Kollektion', 'Post Type Singular Name', 'genesis' ),
'menu_name' => __( 'Kollektion', 'genesis' ),
'name_admin_bar' => __( 'Kollektion', 'genesis' ),
'parent_item_colon' => __( 'Parent Item:', 'genesis' ),
'all_items' => __( 'All Items', 'genesis' ),
'add_new_item' => __( 'Add New Item', 'genesis' ),
'add_new' => __( 'Add New', 'genesis' ),
'new_item' => __( 'New Item', 'genesis' ),
'edit_item' => __( 'Edit Item', 'genesis' ),
'update_item' => __( 'Update Item', 'genesis' ),
'view_item' => __( 'View Item', 'genesis' ),
'search_items' => __( 'Search Item', 'genesis' ),
'not_found' => __( 'Not found', 'genesis' ),
'not_found_in_trash' => __( 'Not found in Trash', 'genesis' ),
'items_list' => __( 'Items list', 'genesis' ),
'items_list_navigation' => __( 'Items list navigation', 'genesis' ),
'filter_items_list' => __( 'Filter items list', 'genesis' ),
);
$args = array(
'label' => __( 'Kollektion', 'genesis' ),
'description' => __( 'Kollektionen', 'genesis' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', ),
'taxonomies' => array( 'post_tag', 'kollektionen' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => 'kollektion',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'kollektion', $args );
}
add_action( 'init', 'post_type_kollektion');
}
function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'nav_menu_item', 'kollektion', 'kollektionen'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
It looks like you have the same slug for taxonomy and custom post type. Try to change slug of taxonomy (or CPT) and resave the permalinks - this should help.