WP Bakery - Displaying taxonomy in grid - wordpress

I have a taxonomy which I have created as follows:
function understrap_custom_taxonomies() {
$post_types_index = array('cs_publications', 'cs_projects');
$taxs = array(
'cs_themes' => array(
'menu_title' => 'Themes',
'plural' => 'Themes',
'singular' => 'Theme',
'hierarchical' => true,
'slug' => 'theme',
'post_type' => array( 'post', 'cs_publications', 'cs_projects', 'cs_events')
)
);
foreach( $taxs as $tax => $args ) {
$labels = array(
'name' => _x( $args['plural'], 'taxonomy general name' ),
'singular_name' => _x( $args['singular'], 'taxonomy singular name' ),
'search_items' => __( 'Search '.$args['plural'] ),
'all_items' => __( 'All '.$args['plural'] ),
'parent_item' => __( 'Parent '.$args['plural'] ),
'parent_item_colon' => __( 'Parent '.$args['singular'].':' ),
'edit_item' => __( 'Edit '.$args['singular'] ),
'update_item' => __( 'Update '.$args['singular'] ),
'add_new_item' => __( 'Add New '.$args['singular'] ),
'new_item_name' => __( 'New '.$args['singular'].' Name' ),
'menu_name' => __( $args['menu_title'] )
);
$tax_args = array(
'hierarchical' => $args['hierarchical'],
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => true,
'with_front' => false,
'rewrite' => array( 'slug' => $args['slug'] ),
);
register_taxonomy( $tax, $args['post_type'], $tax_args );
}
}
add_action('init', 'understrap_custom_taxonomies');
I am now trying to get the value for "cs_themes" to display in a WP Bakery grid using the custom field element:
However no matter whether I type "cs_themes", "taxonomy_cs_themes" or any other logical combination, it doesn't seem to pull through the values. Any advice would be much appreciated.

Related

Taxonomy returns invalid taxonomy error when using get_terms()

I have registered a custom taxonomy.Please find the code below
function register_taxonomy_state() {
$labels = array(
'name' => _x( 'States', 'taxonomy general name' ),
'singular_name' => _x( 'State', 'taxonomy singular name' ),
'search_items' => __( 'Search State' ),
'all_items' => __( 'All States' ),
'parent_item' => __( 'Parent States' ),
'parent_item_colon' => __( 'Parent States:' ),
'edit_item' => __( 'Edit State' ),
'update_item' => __( 'Update State' ),
'add_new_item' => __( 'Add New State' ),
'new_item_name' => __( 'New State Name' ),
'menu_name' => __( 'State' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'public' =>true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'alumns-reps-states' ],
);
register_taxonomy( 'alumns-reps-states', [ 'alumns-reps' ], $args );
}
add_action( 'init', 'register_taxonomy_state' );
I would like to display all terms added under this taxonomy, and for that i used following code
$taxonomies = get_terms( array(
'taxonomy' => 'alumns-reps-states',
'hide_empty' => false
) );
var_dump($taxonomies);
It is always returning 'Invalid Taxonomy' error. WHat is wrong with this code ?

wordpress - query custom post type by category slug

I'm trying to query a custom post type by its category slug, but its not working at all, it always return blank - like no results
my custom post type definitions:
add_action( 'init', 'custom_post_albuns' );
// The custom function to register a movie post type
function custom_post_albuns() {
// Set the labels, this variable is used in the $args array
$labels = array(
'name' => __( 'Albuns' ),
'singular_name' => __( 'Album' ),
'add_new' => __( 'Add New Album' ),
'add_new_item' => __( 'Add New Album' ),
'edit_item' => __( 'Edit Album' ),
'new_item' => __( 'New Album' ),
'all_items' => __( 'All Albuns' ),
'view_item' => __( 'View Album' ),
'search_items' => __( 'Search Albuns' ),
'featured_image' => 'Featured Image',
'set_featured_image' => 'Add Featured Image'
);
// The arguments for our post type, to be entered as parameter 2 of register_post_type()
$args = array(
'labels' => $labels,
'description' => 'm6 Records Albuns',
'public' => true,
'menu_position' => 6,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
'has_archive' => true,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-album',
'query_var' => 'album'
);
// Call the actual WordPress function
// Parameter 1 is a name for the post type
// Parameter 2 is the $args array
register_post_type( 'album', $args);
}
add_action( 'init', 'create_album_taxonomies', 0 );
function create_album_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 Name' ),
'menu_name' => __( 'Categories' ),
);
$args = array(
'hierarchical' => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'albuns' ),
);
register_taxonomy( 'albuns_categories', array( 'album' ), $args );
}
and my query:
$argsPost = array(
'post_type'=>'album',
'orderby'=>'date',
'order' => 'DESC',
'posts_per_page'=>'4',
'tax_query' => array(
array(
'taxonomy' => 'albuns_categories',
'field' => 'slug',
'terms' => array( 'ANYTERM' )
)
)
);
and I can't bring a list of albuns (if I remove the tax_query part, it returns to me all the "albuns" custom post type).
Do someone have any ideias???
Thanks!!
You can try
$argsPost = array(
'post_type'=>'album',
'orderby'=>'date',
'order' => 'DESC',
'posts_per_page'=>'4',
'tax_query' => array(
array(
'taxonomy' => 'albuns_categories',
'field' => 'slug',
'terms' => array( 'ANYTERM' ),
'operator' => 'IN'
)
)
);

WordPress - Custom taxonomies not showing in Appearance > Menu

I have a custom post type called Resources:
register_post_type(
'resources',
build_post_args(
'resources', 'Resource', 'Resources',
array(
'menu_icon' => 'dashicons-welcome-write-blog',
'menu_position' => 20,
'has_archive' => true,
'public' => true,
'supports' => array('editor', 'title','author','thumbnail', 'revisions'),
'taxonomies' => array('sector', 'subject', 'type'),
)
)
);
I have also created the taxonomy of type which is assigned to Resources:
register_taxonomy(
'type',
'type',
array(
'hierarchical' => true,
'label' => 'Type',
'query_var' => true,
'rewrite' => array(
'slug' => 'type',
'with_front' => false
)
)
);
In the WordPress backend, here are all my type's:
However, when I go to Appearance > Menus > and click on the Categories dropdown, only these options show:
Why is this?
Hello Please add below code and check as you are missing lots of things when creating resources post type. (Note: Please remove all your code added for creating resources post and type taxonomy)
function ro_resources_custom_post_type() {
$labels = array(
'name' => __( 'Resources' ),
'singular_name' => __( 'Resource'),
'menu_name' => __( 'Resources'),
'parent_item_colon' => __( 'Parent Resource'),
'all_items' => __( 'All Resources'),
'view_item' => __( 'View Resource'),
'add_new_item' => __( 'Add New Resource'),
'add_new' => __( 'Add New'),
'edit_item' => __( 'Edit Resource'),
'update_item' => __( 'Update Resource'),
'search_items' => __( 'Search Resource'),
'not_found' => __( 'Not Found'),
'not_found_in_trash' => __( 'Not found in Trash')
);
$args = array(
'label' => __( 'resources'),
'description' => __( 'Best Resources'),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields'),
'public' => true,
'hierarchical' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'has_archive' => true,
'can_export' => true,
'exclude_from_search' => false,
'yarpp_support' => true,
'publicly_queryable' => true,
'capability_type' => 'page'
);
register_post_type( 'resources', $args );
}
add_action( 'init', 'ro_resources_custom_post_type', 0 );
add_action( 'init', 'ro_create_resources_custom_taxonomy', 0 );
//create a custom taxonomy name it "type" for your posts
function ro_create_resources_custom_taxonomy() {
$labels = array(
'name' => _x( 'Types', 'taxonomy general name' ),
'singular_name' => _x( 'Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Types' ),
'all_items' => __( 'All Types' ),
'parent_item' => __( 'Parent Type' ),
'parent_item_colon' => __( 'Parent Type:' ),
'edit_item' => __( 'Edit Type' ),
'update_item' => __( 'Update Type' ),
'add_new_item' => __( 'Add New Type' ),
'new_item_name' => __( 'New Type Name' ),
'menu_name' => __( 'Types' ),
);
register_taxonomy('types',array('resources'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'type' ),
));
}
Tested and works well

Trouble with Custom Post Type

I'm creating a custom post type with taxonomies in WordPress 4.7.3, but for some reason the taxonomy pages aren't working properly. My custom post type is "events", but the archive-events.php template file isn't working when visiting "mysite.com/events/event-category" or when visiting "mysite.com/events"
Below is my code from functions.php, what am I doing wrong here??? lol
add_action( 'init', 'register_events', 20 );
function register_events() {
$labels = array(
'name' => _x( 'All Events', 'events','sonal' ),
'singular_name' => _x( 'Event', 'events', 'sonal' ),
'add_new' => _x( 'Add New', 'events', 'sonal' ),
'add_new_item' => _x( 'Add New Event', 'events', 'sonal' ),
'edit_item' => _x( 'Edit Event', 'events', 'sonal' ),
'new_item' => _x( 'New Event', 'events', 'sonal' ),
'view_item' => _x( 'View Event', 'events', 'sonal' ),
'search_items' => _x( 'Search Events', 'events', 'sonal' ),
'not_found' => _x( 'No Events found...', 'events', 'sonal' ),
'not_found_in_trash' => _x( 'No Events found in Trash', 'events', 'sonal' ),
'parent_item_colon' => _x( 'Parent Event:', 'events', 'sonal' ),
'menu_name' => _x( 'Events', 'events', 'sonal' ),
);
$args = array(
'labels' => __( $labels, 'local' ),
'hierarchical' => true,
'description' => 'events',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'revisions' ),
'taxonomies' => array( 'events_category'),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-tickets-alt',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => 'events/%events_category%','with_front' => FALSE),
'public' => true,
'has_archive' => 'events_category',
'capability_type' => 'post'
);
register_post_type( 'events', $args );
}
//Create Taxonomies (Categories)
add_action( 'init', 'create_events_taxonomies', 20 );
function create_events_taxonomies() {
$labels = array(
'name' => _x( 'Event Categories', 'taxonomy general name', 'sonal' ),
'singular_name' => _x( 'Event Category', 'taxonomy singular name', 'sonal' ),
'search_items' => __( 'Search Event Categories', 'sonal' ),
'all_items' => __( 'All Event Categories', 'sonal' ),
'parent_item' => __( 'Parent Event Category', 'sonal' ),
'parent_item_colon' => __( 'Parent Event Category:', 'sonal' ),
'edit_item' => __( 'Edit Event Category', 'sonal' ),
'update_item' => __( 'Update Event Category', 'sonal' ),
'add_new_item' => __( 'Add New Event Category', 'sonal' ),
'new_item_name' => __( 'New Event Category Name', 'sonal' ),
'menu_name' => __( 'Event Categories', 'sonal' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'events', 'with_front' => false ),
);
register_taxonomy( 'events_category', array( 'events' ), $args );
}
//Set Permalinks
function wpa_events_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'events' ){
$terms = wp_get_object_terms( $post->ID, 'events_category' );
if( $terms ){
return str_replace( '%events_category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_events_permalinks', 1, 2 );
Remove the /%events_category% from the rewrite argument of the events custom post type
Before:
'rewrite' => array('slug' => 'events/%events_category%','with_front' => FALSE),
After:
'rewrite' => array('slug' => 'events','with_front' => FALSE),
Again in the args of the events custom post type, set "has_archive" to true
Before
'has_archive' => 'events_category',
After:
'has_archive' => true,
Refresh permalink in Setting -> permalinks
Be sure that you chose custom structure with
/%category%/%postname%/
Then go to
yoursite.com/events/
the archive-events.php file should work

Custom taxonomy slug before post name

I want that specific custom post url will look like:
http://www.foo.bar/{taxonomy-term-slug}/{postname}
Im using this code to create a custom post type and custom taxonomy
function create_post_types(){
$labels = array(
'name' => _x( "name_plural", 'Post Type General Name', 'WP_TEXTDOMAIN' ),
'singular_name' => _x( "name_singular", 'Post Type Singular Name', 'WP_TEXTDOMAIN' ),
'menu_name' => __( "name_plural", 'WP_TEXTDOMAIN' ),
'parent_item_colon' => __( "name_plural - Parent:', 'WP_TEXTDOMAIN' ),
'all_items' => __( 'All name_plural", 'WP_TEXTDOMAIN' ),
'view_item' => __( 'View name_plural', 'WP_TEXTDOMAIN' ),
'add_new_item' => __( 'New name_singular', 'WP_TEXTDOMAIN' ),
'add_new' => __( 'New name_singular', 'WP_TEXTDOMAIN' ),
'edit_item' => __( 'Edit name_singular', 'WP_TEXTDOMAIN' ),
'update_item' => __( 'Update name_singular', 'WP_TEXTDOMAIN' ),
'search_items' => __( 'Search name_singular', 'WP_TEXTDOMAIN' ),
'not_found' => __( 'Not Found', 'WP_TEXTDOMAIN' ),
'not_found_in_trash' => __( 'Not Found In Trash', 'WP_TEXTDOMAIN' ),
);
$args = array(
'label' => __( "name_plural", 'WP_TEXTDOMAIN' ),
'menu_icon' => "icon",
'description' => __( 'Description', 'WP_TEXTDOMAIN' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', '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,
'has_archive' => true,
'exclude_from_search' => false,
'public' => true,
'capability_type' => 'page',
'query_var' => true,
'rewrite' => array('slug' => '%product_cat%','with_front' => false)
);
register_post_type( 'product', $args );
$labels_cat = array(
'name' => _x( "name_plural", 'Taxonomy General Name', 'WP_TEXTDOMAIN' ),
'singular_name' => _x( "name_singular", 'Taxonomy Singular Name', 'WP_TEXTDOMAIN' ),
'menu_name' => __( 'Categories', 'WP_TEXTDOMAIN' ),
'all_items' => __( 'All Categories', 'WP_TEXTDOMAIN' ),
'parent_item' => __( 'Category - Parent', 'WP_TEXTDOMAIN' ),
'parent_item_colon' => __( 'Category - Parent:', 'WP_TEXTDOMAIN' ),
'new_item_name' => __( 'New name_singular Category', 'WP_TEXTDOMAIN' ),
'add_new_item' => __( 'New name_singular Category', 'WP_TEXTDOMAIN' ),
'edit_item' => __( 'Edit Category', 'WP_TEXTDOMAIN' ),
'update_item' => __( 'Update Category', 'WP_TEXTDOMAIN' ),
'view_item' => __( 'View Category', 'WP_TEXTDOMAIN' ),
'separate_items_with_commas' => __( 'Saparate Category With Commas', 'WP_TEXTDOMAIN' ),
'add_or_remove_items' => __( 'Add / Remove Category', 'WP_TEXTDOMAIN' ),
'choose_from_most_used' => __( 'Most Used', 'WP_TEXTDOMAIN' ),
'popular_items' => __( 'Populars', 'WP_TEXTDOMAIN' ),
'search_items' => __( 'Search Category', 'WP_TEXTDOMAIN' ),
'not_found' => __( 'Not Found', 'WP_TEXTDOMAIN' ),
);
$args_cat = array(
'labels' => $labels_cat,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => 'product_cat',
'rewrite' => array('slug' => 'product_cat' ),
'_builtin' => false
);
register_taxonomy( 'product_cat', 'product', $args_cat );
}
// Hook into the 'init' action
add_action( 'init', 'create_post_types', 0);
add_filter('post_link', 'product_permalink', 1, 3);
add_filter('post_type_link', 'product_permalink', 1, 3);
function product_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%product_cat%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'product_cat_cat');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'none';
return str_replace('%product_cat_cat%', $taxonomy_slug, $permalink);
}
The link structure that im getting is correct,
But once i try to click the link i get a 404 Page
Any thoughts ?
Please use this code
// post type product
function custom_post_products() {
$labels = array(
'name' => _x( 'Products', 'post type general name' ),
'singular_name' => _x( 'Product', 'post type singular name' ),
'add_new' => _x( 'Add Product','News' ),
'add_new_item' => __( 'Add Product' ),
'edit_item' => __( 'Edit' ),
'new_item' => __( 'New Product' ),
'all_items' => __( 'All products' ),
'view_item' => __( 'View' ),
'search_items' => __( 'Search' ),
'not_found' => __( 'No record found' ),
'not_found_in_trash' => __( 'No record found in the Trash' ),
'menu_name' => 'Products'
);
$args = array(
'labels' => $labels,
'description' => 'Mcarthur products',
'public' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'menu_position' => 15
);
register_post_type( 'products', $args );
}
add_action( 'init', 'custom_post_products' );
add_action( 'init', 'build_taxonomies_products', 0 );
//custom texonomy
function build_taxonomies_products() {
register_taxonomy( 'products-cat', 'products', array( 'hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' => true ) );
}

Resources