Woocommerce Product Archive Page for Custom Taxonomy - php

I registered a custom taxonomy for WooCommerce products. Additionally, I set up a page template to list up all entries of the custom taxonomy with taxonomy archive links. Now I have the problem, that the archive links are not working. I was expecting them to work similarly to the product category and product tag archive pages. Do I need to set up a custom archive page file or is there something missing in my code? Thanks for any hint or help in advance!
// Register Custom Taxonomy
function ffos_custom_taxonomy_Brand() {
$labels = array(
'name' => 'Brands',
'singular_name' => 'Brand',
'menu_name' => 'Brands',
'all_brands' => 'All Brands',
'parent_item' => 'Parent Brand',
'parent_item_colon' => 'Parent Brand:',
'new_item_name' => 'New Brand Name',
'add_new_item' => 'Add New Brand',
'edit_item' => 'Edit Brand',
'update_item' => 'Update Brand',
'separate_items_with_commas' => 'Separate Brand with commas',
'search_items' => 'Search Brands',
'add_or_remove_items' => 'Add or remove Brands',
'choose_from_most_used' => 'Choose from the most used Brands',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
);
register_taxonomy( 'product_brand', 'product', $args );
}
add_action( 'init', 'ffos_custom_taxonomy_Brand', 0 );
$args = array(
'orderby' => 'name',
'order' => ASC,
'hide_empty' => true,
);
$product_brand = get_terms( 'product_brand', $args );
if ( $product_brand && ! is_wp_error( $product_brand ) ) {
foreach ($product_brand as $brand) {
$brand_logo = get_field('brand_logo', $brand);
$brand_title = $brand->name;
$brand_link = get_term_link( $brand );
echo '<div class="small-12 medium-4 large-2 columns" >';
echo '<a href="'.$brand_link.'">';
echo '<div class="brand_logo" style="background-image:url('.$brand_logo.');" alt="'.$brand_title.'"></div>';
echo '</a>';
echo '</div>';
}
}

In the WP admin go to Settings>Permalinks and click on the button "Save Changes". Rewrite Rules had to be flushed. Now it works like a charm!

Related

Can't display posts using Custom Post Type - WordPress

I'm feeling close to finishing this. In short, I can't seem to display posts.
For example I have:
http://somedomain.org/tours //Displays all categories
http://somedomain.org/tours/gbr //Displays all posts in category
http://somedomain.org/tours/gbr/my-post //Should display post in category
http://somedomain.org/tours/gbr/my-post doesn't display, I get a there's nothing here, 404 error. But if I go to:
http://somedomain.org/toursgbr/my-post
It does in fact, display the post ("toursgbr" has no forward slash if you're looking for the difference)
I am using the latest WordPress. I am using the plugin:
Custom Post Type Permalinks
I have the following code in the functions.php file:
// Our custom post type function
function create_posttype() {
register_post_type( 'tours',
// CPT Options
array(
'labels' => array(
'name' => __( 'Tours' ),
'singular_name' => __( 'Tour' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'tours', 'with_front' => TRUE),
'cptp_permalink_structure' => '%tour_category%/%postname%',
'hierarchical' => true //A added
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
function my_taxonomies_tours() {
$labels = array(
'name' => _x( 'Tour Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Tour Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Tour Categories' ),
'all_items' => __( 'All Tour Categories' ),
'parent_item' => __( 'Parent Tour Category' ),
'parent_item_colon' => __( 'Parent Tour Category:' ),
'edit_item' => __( 'Edit Tour Category' ),
'update_item' => __( 'Update Tour Category' ),
'add_new_item' => __( 'Add New Tour Category' ),
'new_item_name' => __( 'New Tour Category' ),
'menu_name' => __( 'Tour Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'rewrite' => array('slug' => 'tours')
);
register_taxonomy( 'tour_category', 'tours', $args );
}
add_action( 'init', 'my_taxonomies_tours', 0 );
The line of code:
'cptp_permalink_structure' => '%tour_category%/%postname%',
If I add the forward slash, so it's:
'cptp_permalink_structure' => '/%tour_category%/%postname%',
That's when I can't view posts. Any help in the right direction would be fantastic. I do have other categories other than gbr and posts can have multiple categories. I thought that might be important information

Can't view all posts in a category - Wordpress

I'm having issues viewing all posts in a category.
I have created a custom post and categories to go with it in the functions.php, like so:
function create_posttype() {
register_post_type( 'tours',
// CPT Options
array(
'labels' => array(
'name' => __( 'Tours' ),
'singular_name' => __( 'Tour' )
),
'public' => true,
//'has_archive' => true,
'rewrite' => array('slug' => 'tours'),
//'taxonomies' => array('category'), //A added
'hierarchical' => true //A added
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
function my_taxonomies_tours() {
$labels = array(
'name' => _x( 'Tour Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Tour Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Tour Categories' ),
'all_items' => __( 'All Tour Categories' ),
'parent_item' => __( 'Parent Tour Category' ),
'parent_item_colon' => __( 'Parent Tour Category:' ),
'edit_item' => __( 'Edit Tour Category' ),
'update_item' => __( 'Update Tour Category' ),
'add_new_item' => __( 'Add New Tour Category' ),
'new_item_name' => __( 'New Tour Category' ),
'menu_name' => __( 'Tour Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'tour_category', 'tours', $args );
}
add_action( 'init', 'my_taxonomies_tours', 0 );
I have created a template to display all categories in the hopes you can click on a category and have it list all the posts. In my template I have the following:
$custom_terms = get_terms('tour_category');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'tours',
'tax_query' => array(
array(
'taxonomy' => 'tour_category',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
}
}
What I have currently lists the categories, but I don't know how to link it in such a way so you can view all posts within that particular category. Any help would be greatly appreciated.
Using Wordpress 4.4
Try adjusting your $args variable like this (add posts_per_page):
$args = array('post_type' => 'tours',
'tax_query' => array(
array(
'taxonomy' => 'tour_category',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
'posts_per_page' => -1,
);
Otherwise it use default posts per page value.

Custom taxonomy page template not found

How can I list all posts from a custom taxonomy category into a custom template? Below is the code.
So I`ve created a custom taxonomy like this:
register_taxonomy('miniblog_category', 'post', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'MiniBlog Categories', 'taxonomy general name' ),
'singular_name' => _x( 'MiniBlog Category', 'taxonomy singular name' ),
'search_items' => __( 'Search MiniBlog Categories' ),
'all_items' => __( 'All MiniBlog Categories' ),
'parent_item' => __( 'Parent MiniBlog Category' ),
'parent_item_colon' => __( 'Parent MiniBlog Category:' ),
'edit_item' => __( 'Edit MiniBlog Category' ),
'update_item' => __( 'Update MiniBlog Category' ),
'add_new_item' => __( 'Add New MiniBlog Category' ),
'new_item_name' => __( 'New MiniBlog Category Name' ),
'menu_name' => __( 'MiniBlog Categories' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'miniblog',
'with_front' => false,
),
));
After that I also created a custom widgets that is displayed on the author`s page (right sidebar). This widget displays all current author's categories with the post numbers (the code below):
...
$current_author_id = get_the_author_meta('ID');
$categories_by_post_no = array();
foreach (get_terms('miniblog_category') as $mbc) {
// metadata term( blogger_id )
$taxonomy_blogger_id = get_term_meta($mbc->term_id, 'blogger_id', true) ;
if ($taxonomy_blogger_id == $current_author_id) {
$arg_author_posts = array(
'post_type' => 'post',
'post_status' => 'published',
'miniblog_category' => $mbc->slug,
'numberposts' => -1,
);
$term_link = get_term_link($mbc->term_id, $mbc->slug);
$categories_by_post_no[] = array(
'name' => $mbc->name,
'post_count' => count( get_posts( $arg_author_posts ) ),
'term_link' => get_term_link($mbc),
);
}
}
if (! empty($categories_by_post_no) ) {
echo '<ul class="menu">';
foreach ($categories_by_post_no as $cbpn) {
echo "<li><a href='" . $cbpn['term_link'] . "'>$cbpn[name] ($cbpn[post_count])<a/></li>";
};
echo'</ul>';
} else {
echo '<span>No articles!</span>';
}
...
In my case the links ($cbpn['term_link']) are like this:
- miniblog/culture
- miniblog/nature
- miniblog/south-america
...
but when I click on those links I received a 404 not found error.
I tried to create in the theme folder files like this (taxonomy-miniblog.php, taxonomy-miniblog-category.php) but it doesn't work. Do I have to create files for each category, e.g. taxonomy-miniblog-culture.php, taxonomy-miniblog-nature.php ...(no way!!)?

Taxonomy link not showing in custom admin menu wordpress

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');

How to display meta_box field in listing

I am creating one post_type using register_post_type so i am getting add_new option there i am using meta_box to use my custom field.But in my listing it is showing only title column,How i can use my meta_box field as a column in listing page.
my code for register_post_type is -
// Create Brand Management
add_action('init', 'manage_brand');
function manage_brand() {
register_post_type('brand', array(
'labels' => array(
'name' => 'Manage Brand',
'singular_name' => 'Manage Brand',
'add_new' => 'Add New',
'add_new_item' => 'Add New Brand',
'edit' => 'Edit',
'edit_item' => 'Edit Brand',
'new_item' => 'New Brand',
'view' => 'View',
'view_item' => 'View Brand',
'search_items' => 'Search Brand',
'not_found' => 'No Brand',
'not_found_in_trash' => 'No Brand found in Trash',
'parent' => 'Parent News Brand'
),
'public' => true,
'menu_position' => 100,
'supports' => array('','thumbnail'),
'taxonomies' => array('project-cat'),
'menu_icon' => plugins_url('images/adv-.png', __FILE__),
'has_archive' => true,
)
);
}
and my code to add field using meta_box is -
//add meta data for brand
add_action('admin_init', 'brand_register_meta_boxes');
function brand_register_meta_boxes() {
if (!class_exists('RW_Meta_Box'))
return;
$prefix = 'brand_';
$meta_boxes[] = array(
'title' => 'Add Brand',
'pages' => array('brand'),
'fields' => array(
array(
'name' => __( 'Brand Name', 'rwmb' ),
'desc' => __( 'Add Brand Name', 'rwmb' ),
'id' => "{$prefix}code",
'type' => 'text',
'required' => true,
),
)
);
foreach ($meta_boxes as $meta_box) {
new RW_Meta_Box($meta_box);
}
}
my list page is coming like this with tile only -
how can i add my meta_box field also in this listing.
Assuming the value of the meta box is stored as post meta, you should (a) register a custom column using the dynamic manage_{$post_type}_posts_columns filter hook:
add_filter( 'manage_brand_posts_columns', 'so20352744_manage_brand_posts_columns', 25, 1 );
function so20352744_manage_brand_posts_columns( $cols )
{
$cols['brand'] = __( 'Brand', 'txt_domain' );
return $cols;
}
and (b) add content to the column using the manage_posts_custom_column action hook:
add_action( 'manage_posts_custom_column', 'so20352744_manage_posts_custom_column', 2, 1 );
function so20352744_manage_posts_custom_column( $col )
{
global $post;
switch ( $col )
{
case "brand" :
if( '' != get_post_meta( $post->ID, 'brand_code', true ) )
echo get_post_meta( $post->ID, 'brand_code', true );
else
echo __( 'N/A', 'txt_domain' );
break;
default :
break;
}
}
Both functions go in functions.php. More info: http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column

Categories