How to display meta_box field in listing - php

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

Related

WordPress Custom Post Types with own lists

I have in my theme a custom post type called "recipes". I can add text, a thumbnail and add categories. I want to add a ingredients list to this post type. I need a global list of ingredients which I can use for a single recipe. How can i create a global list for the custom post type like categories but just for ingredients?
Here's my code for the post type:
<?php
add_action( 'init', 'my_recipes' );
add_filter( 'post_updated_messages', 'my_recipes_messages' );
add_action( 'admin_head', 'my_recipes_help' );
function my_recipes() {
$labels = array(
'name' => 'Recipes',
'singular_name' => 'Recipe',
'menu_name' => 'Recipes',
'name_admin_bar' => 'Recipe',
'add_new' => 'Add New',
'add_new_item' => 'Add New Recipe',
'new_item' => 'New Recipe',
'edit_item' => 'Edit Recipe',
'view_item' => 'View Recipe',
'all_items' => 'All Recipes',
'search_items' => 'Search Recipes',
'parent_item_colon' => 'Parent Recipes:',
'not_found' => 'No recipes found.',
'not_found_in_trash' => 'No recipes found in Trash.'
);
$args = array(
'labels' => $labels,
'public' => true,
'rewrite' => array( 'slug' => 'recipe' ),
'has_archive' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-carrot',
'taxonomies' => array( 'post_tag', 'category' ),
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt')
);
register_post_type( 'my_recipe', $args );
}
function my_recipes_messages( $messages ) {
$post = get_post();
$messages['recipe'] = array(
0 => '',
1 => 'Recipe updated.',
2 => 'Custom field updated.',
3 => 'Custom field deleted.',
4 => 'Recipe updated.',
5 => isset( $_GET['revision'] ) ? sprintf( 'Recipe restored to revision from %s',wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => 'Recipe published.',
7 => 'Recipe saved.',
8 => 'Recipe submitted.',
9 => sprintf(
'Recipe scheduled for: <strong>%1$s</strong>.',
date_i18n( 'M j, Y # G:i', strtotime( $post->post_date ) )
),
10 => 'Recipe draft updated.'
);
return $messages;
}
function my_recipes_help() {
$screen = get_current_screen();
if ( 'recipe' != $screen->post_type ) {
return;
}
$basics = array(
'id' => 'recipe_basics',
'title' => 'Recipe Basics',
'content' => 'Content for help tab here'
);
$formatting = array(
'id' => 'recipe_formatting',
'title' => 'Recipe Formatting',
'content' => 'Content for help tab here'
);
$screen->add_help_tab( $basics );
$screen->add_help_tab( $formatting );
}
If you're wanting to have a global list of ingredients that you can add, as you mentioned, like categories or tags, then you're looking for a custom taxonomy.
Try this tool:
https://generatewp.com/taxonomy/

Woocommerce Product Archive Page for Custom Taxonomy

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!

How to create function like the_tags() or the_category() in wordpress?

I created a meta box for post, named as "Resource" where I can select a value from predefined select element in the backend. And after saving it it shows in frontend. Please see the images.
Now I want this to work like tags, author and categories. When I click it will show all the posts selected with the same value. But do not know where to start.
I used Meta Box plugin and did the following codes in function.php
add_filter( 'rwmb_meta_boxes', 'resource_meta_box' );
function resource_meta_box( $meta_boxes ) {
$meta_boxes[] = array(
'id' => 'resource_box',
'title' => esc_html__( 'Resource' ),
'post_types' => array( 'post' ),
'context' => 'side',
'priority' => 'high',
'autosave' => true,
// List of meta fields
'fields' => array(
// RESOURCE BOX
array(
'name' => esc_html__( '' ),
'id' => "resource",
'type' => 'select',
// Array of 'value' => 'Label' pairs for select box
'options' => array(
'Item 1' => esc_html__( 'Item 1' ),
'Item 2' => esc_html__( 'Item 2' ),
'Item 3' => esc_html__( 'Item 3' ),
'Item 4' => esc_html__( 'Item 4' ),
'Item 5' => esc_html__( 'Item 5' ),
),
// Placeholder of Select.
'placeholder' => esc_html__( 'None' )
)
)
);
return $meta_boxes;
}
and the following code where it needed to show <?php echo rwmb_meta( 'resource'); ?>
Thanks in advance.
you should go for custom taxonomy, cause it have the features required by you inbuilt with it. paste the code given bellow in your theme functions.php file.enter code here
$labels = array(
'name' => _x( 'Resources', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Resource', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Resources', 'textdomain' ),
'popular_items' => __( 'Popular Resources', 'textdomain' ),
'all_items' => __( 'All Resources', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Resource', 'textdomain' ),
'update_item' => __( 'Update WResource', 'textdomain' ),
'add_new_item' => __( 'Add New Resource', 'textdomain' ),
'new_item_name' => __( 'New Resource Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate Resources with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove Resources', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used Resources', 'textdomain' ),
'not_found' => __( 'No Resources found.', 'textdomain' ),
'menu_name' => __( 'Resources', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'resource' ),
);
register_taxonomy( 'resource', 'your-post-type-slug', $args );
replace the text 'your-post-type-slug' with the post-type slug (e.g. 'post' for Posts or 'page' for Pages etc) of the post type, with which you want to associate the taxonomy.
then use bellow code to retrieve all terms created under 'resource' taxonomy
get_terms( 'resource', array(
'hide_empty' => false,
) );
create template file, taxonomy-resource.php from archive.php to show filtered (by the newly added taxonomy) post list.
use the_terms(get_the_ID(),'resource', $before, $sep, $after ); to show selected resources for a specific post inside loop.

Wordpress same archive template for custom post type and taxonomy

I created a custom taxonomy for tags
function create_topics_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'Topics', 'taxonomy general name' ),
'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
'search_items' => __( 'Search Topics' ),
'popular_items' => __( 'Popular Topics' ),
'all_items' => __( 'All Topics' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Topic' ),
'update_item' => __( 'Update Topic' ),
'add_new_item' => __( 'Add New Topic' ),
'new_item_name' => __( 'New Topic Name' ),
'separate_items_with_commas' => __( 'Separate topics with commas' ),
'add_or_remove_items' => __( 'Add or remove topics' ),
'choose_from_most_used' => __( 'Choose from the most used topics' ),
'menu_name' => __( 'Topics' )
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('topics','post',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'show_in_nav_menus' => true,
'rewrite' => array( 'slug' => 'topic' ),
));
}
and connected it to my custom post type
register_post_type( 'video', array(
...
'taxonomies' => array( 'topics' ),
...
) );
The custom post type uses archive-video.php as the archive template. Is it possible to use the same archive template for the custom taxonomy? Without duplicating the file and renaming it.
You can make use of the template_include filter to set the template you need for a specific condition
You can try something like this
add_filter( 'template_include', function ( $template ) {
if ( is_tax( 'topics' ) ) {
$new_template = locate_template( array( 'archive-video.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}, PHP_INT_MAX, 2 );
I'd create the taxonomy file and have it load the archive file. This avoids having two copies of the same file which need to be kept in sync. WooCommerce uses the same approach for product categories and tags.
Create taxonomy-topics.php and add the following code:
<?php
// Find and load the archive template.
locate_template( 'archive-video.php', true );

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!!)?

Categories