I am trying to add an excerpt option to my category page, to display instead of my description.
So basically, I need a box on this screen which will be able to be used as preview text.
The code used to create this taxonomy is:
add_action( 'init', 'create_product_cat_external' );
function create_product_cat_external() {
register_taxonomy(
'ExternalProducts',
'products',
array(
'label' => __( 'External Products' ),
'rewrite' => array( 'slug' => 'externalproducts' ),
'hierarchical' => true,
)
);
}
and the box needs to be here:
You can use CMB2 plugin and put this in your functions.php for example:
add_action( 'cmb2_admin_init', 'yourprefix_register_taxonomy_metabox' );
function yourprefix_register_taxonomy_metabox() {
$prefix = 'yourprefix_term_';
$cmb_term = new_cmb2_box( array(
'id' => $prefix . 'edit',
'object_types' => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
'taxonomies' => array( 'products'), // Tells CMB2 which taxonomies should have these fields
// 'new_term_section' => true, // Will display in the "Add New Category" section
) );
$cmb_term->add_field( array(
'name' => __('Excerpt', 'default'),
'id' => $prefix . 'excerpt',
'type' => 'wysiwyg',
'on_front' => false,
) );
}
Related
I am trying to create a filter in my custom post type- I have created a custom post type with the name of ourrecipes.. you can check the code below for custom post type-
function ourrecipes_posttype() {
register_post_type( 'ourrecipes',
// CPT Options
array(
'labels' => array(
'name' => __( 'Ourrecipes' ),
'singular_name' => __( 'Ourrecipes' )
),
'public' => true,
'taxonomies' => array('category','post_tag'),
'has_archive' => true,
'rewrite' => array('slug' => 'ourrecipes'),
'supports' => array( 'title','thumbnail' ,'editor','excerpt','custom-fields','page-attributes')
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'ourrecipes_posttype' );
For the reference please check the design as well-
https://previns.co.uk/our-recipes/
I'm creating custom ACF Gutenberg blocks my a site and have successfully managed to register my own blocks. Now, I have a custom post type called Blog. I do not want blog to show all my ACF Gutenberg blocks, I want to create a separate batch for custom post type use only. I have enabled show_in_rest, but even the default Gutenberg blogs do not show for me?
Here is my approach:
1. Registering the post type (theme/functions.php)
<?php
register_post_type('Blog', theme_build_post_args('Blog', 'Blog', 'Blog', array(
'show_in_rest' => true,
'menu_icon' => 'dashicons-edit',
'menu_position' => 20,
'has_archive' => true,
'public' => true,
'supports' => array(
'editor',
'title',
'author',
'revisions',
'excerpt',
'thumbnail'
) ,
)));
?>
2. Registering the ACF Gutenberg blocks for pages (theme/inc/acf-blocks/blocks.php)
Here are the blocks that I've registered for use on pages (not on the blog post type):
<?php
$hero = array(
'name' => 'hero',
'title' => __('Hero') ,
'description' => __('') ,
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero'
) ,
);
$blocks = [$hero];
return $blocks;
?>
Registering the ACF Gutenberg blocks for blog post type (theme/inc/acf-blocks/blog-blocks.php)
<?php
$blog_hero = array(
'name' => 'blog_hero',
'title' => __('Blog hero') ,
'description' => __('') ,
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero',
'blog'
) ,
);
$blog_blocks = [$blog_hero];
return $blog_blocks;
?>
Register all blocks (theme/inc/acf-blocks/functions.php)
<?php
/*
* loop though array and register each block type
*/
function block_acf_init(){
$path = get_template_directory().'/inc/acf-blocks/blocks.php';
$blocks = require($path);
foreach($blocks as $block) {
acf_register_block_type($block);
}
}
function blog_acf_init(){
$path = get_template_directory().'/inc/acf-blocks/blog-blocks.php';
$blog_blocks = require($path);
foreach($blog_blocks as $blog_block) {
acf_register_block_type($blog_block);
}
}
// Check if function exists, and hook into setup
if( function_exists('acf_register_block_type') ) {
add_action('acf/init', 'block_acf_init');
add_action('acf/init', 'blog_acf_init');
}
?>
Current results:
When creating a post on the blog custom post type, I do not have the ability to add any blocks, let alone see if blog_hero block appears:
On pages, I can see all my created blocks, however, the blog hero block shows on the page side, when I only want it for the custom post type:
Probably this way could solve the problem:
Specifying post_types param for Blog Hero block to blog
$blog_hero = array(
'name' => 'blog_hero',
'title' => __( 'Blog hero', 'Context' ),
'description' => __( '', 'Context' ),
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero',
'blog'
),
'post_types' => array( 'blog' ),
);
And analogically specifying all post types except blog for Hero block.
$all_post_types = get_post_types();
$hero_block_post_types = array_diff( $all_post_types, array( 'blog' ) );
$hero = array(
'name' => 'hero',
'title' => __( 'Hero', 'Domain' ),
'description' => __( '', 'Domain' ),
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero'
),
'post_types' => $hero_block_post_types
);
$blocks = [ $hero ];
return $blocks;
Notice:
Consider adding Domain for your __ function.
Good practice to use __ function with Domain.
I'd like to be able to add a subtitle to my products in Woocommerce only on the shop page. I have it set up as a catalog right now, since I'm not actively selling my products yet, so I have disabled the add to cart button and the prices. I would like to be able to say how many colors the product comes in as a subtitle. I have read up on adding meta boxes with Woocommerce, and found this solution and added it to my functions.php, but it doesn't appear to be working on my site (I can't find where to enter the information for the product!)
add_filter( 'cmb_meta_boxes', 'bhww_core_cpt_metaboxes' );
function bhww_core_cpt_metaboxes( $meta_boxes ) {
//global $prefix;
$prefix = '_bhww_'; // Prefix for all fields
// Add metaboxes to the 'Product' CPT
$meta_boxes[] = array(
'id' => 'bhww_woo_tabs_metabox',
'title' => 'Additional Product Information - <strong>Optional</strong>',
'pages' => array( 'product' ), // Which post type to associate with?
'context' => 'normal',
'priority' => 'default',
'show_names' => true,
'fields' => array(
array(
'name' => __( 'Colors', 'cmb' ),
'desc' => __( 'Anything you enter here will be displayed on the Colors tab.', 'cmb' ),
'id' => $prefix . 'ingredients_wysiwyg',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, ),
),
),
);
return $meta_boxes;
}
Hello I have a custom post type called results. I also created categories for that specific post type using a taxonomy. I'm not sure if I set it up correctly, but the code I have works so I'm sticking with it. If you see a better way or any errors please let me know.
I am able to create a custom post and set a category to it. Next I would like to create a categories page that will act like the regular archive.php but just for the category of the custom post types.
So say I have a custom post for results and I have its category set to car accidents I would like a way to display them all just like archive.php does for normal posts.
I tried going to a url like this but I get sent to a 404 page, even though I have an archive-results.php
www.myurl.com/results/categories/car-accidents
Here is the code I used to set up the custom post type and the taxonomy. Sorry If its long but I feel like its necessary to include everything.
// Create custom post type
function create_posttype() {
register_post_type( 'Results',
array(
'labels' => array(
'name' => __( 'Results' ),
'singular_name' => __( 'Results' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'results'),
'taxonomies' => array( 'results', 'result-category' ),
)
);
}
add_action( 'init', 'create_posttype' );
//Create category for specific post type
function tr_create_my_taxonomy() {
register_taxonomy(
'results-categories',
'results',
array(
'label' => __( 'Result Categories' ),
'rewrite' => array( 'slug' => 'result-category' ),
'hierarchical' => true,
'has_archive' => true
)
);
}
add_action( 'init', 'tr_create_my_taxonomy' );
Am I missing something that is preventing this url from working?
www.myurl.com/results/categories/car-accidents
Thanks in advance
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', 'cmc-description'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
// Create custom post type
function create_posttype() {
register_post_type( 'Results',
array(
'labels' => array(
'name' => __( 'Results' ),
'singular_name' => __( 'Results' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'results'),
'taxonomies' => array( 'results', 'result-category' ),
)
);
flush_rewrite_rules();
}
add_action( 'init', 'create_posttype' );
//Create category for specific post type
function tr_create_my_taxonomy() {
register_taxonomy(
'results-categories',
'results',
array(
'label' => __( 'Result Categories' ),
'rewrite' => array( 'slug' => 'result-category' ),
'hierarchical' => true,
'has_archive' => true
)
);
}
add_action( 'init', 'tr_create_my_taxonomy' );
i Just made this changes can you please copy this to your code and see it runs well or not
I am having some major issues with this plugin
https://wordpress.org/plugins/breadcrumb-navxt/installation/
I have this layout to my site. In my functions.php file, I have created some new categories inside 'Products' using the following code:
add_action( 'init', 'create_product_cat_scaffolding' );
function create_product_cat_scaffolding() {
register_taxonomy(
'ScaffoldingProducts',
'products',
array(
'label' => __( 'Scaffolding Products' ),
'rewrite' => array( 'slug' => 'scaffoldingproducts' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_product_cat_fencing' );
function create_product_cat_fencing() {
register_taxonomy(
'FencingHoardings',
'products',
array(
'label' => __( 'Fencing Hoardings' ),
'rewrite' => array( 'slug' => 'fencinghoardings' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_product_cat_groundworks' );
function create_product_cat_groundworks() {
register_taxonomy(
'Groundworks',
'products',
array(
'label' => __( 'Groundworks' ),
'rewrite' => array( 'slug' => 'groundworks' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_product_cat_Safety' );
function create_product_cat_Safety() {
register_taxonomy(
'Safety',
'products',
array(
'label' => __( 'Safety' ),
'rewrite' => array( 'slug' => 'safety' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_product_cat_access' );
function create_product_cat_access() {
register_taxonomy(
'Access',
'products',
array(
'label' => __( 'Access' ),
'rewrite' => array( 'slug' => 'access' ),
'hierarchical' => true,
)
);
}
Which creates the following:
From here, I have added sub categories to each of these, for example:
And then when I create products, I just select which sub category they apply to.
Now - my issue. When I click onto my Safety page, the plugin works fine, it goes:
My Site > Safety
But then If I click onto a sub category from Safety, such as Safety category, instead of the breadcrumb going to:
My Site > Safety > Safety Category
It goes to
My Site > Safety
Does anyone have any ideas?
In the plugin, the settings there is an option for Taxonomies, which shows this:
<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" title="Go to the %title% Safety archives." href="%link%">%htitle%</a></span>
Follow these steps
go to Setting > Permalinks > Select Custom Structure
add this into textbox /%category%/postname
hope this will help
Control what's happened in Breadcrumb-NavXT/class.bcn_breadcrumb_trail.php at fill() function line 855.
else if(is_archive())
{
$type = $wp_query->get_queried_object();
//For date based archives
if(is_date())
{
$this->do_archive_by_date();
}
else if(is_post_type_archive() && !isset($type->taxonomy))
{
$this->do_archive_by_post_type();
}
//For taxonomy based archives
else if(is_category() || is_tag() || is_tax())
{
$this->do_archive_by_term();
}
$this->type_archive($type);
}