Wordpress - How to dynamically add fixed data to meta boxes - php

I have a custom post type Event. I also have custom meta boxes (e.g. a dropdown list for the speaker of that event). However, the list of speakers are hard coded by me, and the client wants to be able to add, edit, and remove the speakers from the admin part.
How do I do this?

$items = get_posts( array (
'post_type' => YOUR_POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'publish'
));
<select name="get-posts" id="get-posts">
<option value="">Choose A Page</option>
<?php
foreach($items as $item) {
echo '<option value="'.$item->ID.'"',$meta == $item->ID ? ' selected="selected"' : '','>'.$item->post_title.'</option>';
} // end foreach ?>
</select>

you can do it by custom metabox, here is the code form codex
register_taxonomy(
'speakers',
array( 'YOUR_POST_TYPE' ),
array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Speakers', 'taxonomy general name' ),
'singular_name' => _x( 'Speaker', 'taxonomy singular name' ),
'search_items' => __( 'Search Speakers' ),
'all_items' => __( 'All Speakers' ),
'parent_item' => __( 'Parent Speakers' ),
'parent_item_colon' => __( 'Parent Speakers:' ),
'edit_item' => __( 'Edit Speakers' ),
'update_item' => __( 'Update Speakers' ),
'add_new_item' => __( 'Add New Speakers' ),
'new_item_name' => __( 'New SpeakersName' ),
'menu_name' => __( 'Speakers' ),
),
'rewrite' => true
)
);

Related

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 add individual genre archive

I am new to wordpress, and was wanting to know how I would add 22 individual genre types &/or archives to wordpress, as I am coding a movie website.
Also I have added a custom post type 'movies' and want to add the 22 genres as 22 tick boxes to the UI.
Thanks in advance
Steven
You should use register_taxonomy. But I don't think that you should register 22 deifferent categories. You can register one ( genre ) for example and add those 22 different genres. If can register as much as you want new categories/taxonomies. Once registered that taxonomy in edit movie page is right sidebar you will find your genres.
Here is the code:
register_taxonomy(
'genre', # Taxonomy name
array( 'movies' ), # Post Types
array( # Arguments
'labels' => array(
'name' => __( 'Genres', 'crb' ),
'singular_name' => __( 'Genre', 'crb' ),
'search_items' => __( 'Search Genres', 'crb' ),
'all_items' => __( 'All Genres', 'crb' ),
'parent_item' => __( 'Parent Genre', 'crb' ),
'parent_item_colon' => __( 'Parent Genre:', 'crb' ),
'view_item' => __( 'View Genre', 'crb' ),
'edit_item' => __( 'Edit Genre', 'crb' ),
'update_item' => __( 'Update Genre', 'crb' ),
'add_new_item' => __( 'Add New Genre', 'crb' ),
'new_item_name' => __( 'New Genre Name', 'crb' ),
'menu_name' => __( 'Genres', 'crb' ),
),
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
)
);

my custom post type not shown according to category in wordpress

I created a custom post type in the name of 'phones'. In category I have 4 brand name that I want each phones have one category in category page.
my function code:
function create_phone_post_type() {
register_post_type( 'phones',
array(
'labels' => array(
'name' => 'phones',
'singular_name' => 'phones',
'add_new' => 'Add New',
'add_new_item' => 'Add New phone',
'edit_item' => 'Edit phone',
'new_item' => 'New phone',
'view_item' => 'View phone',
'search_items' => 'Search phone',
'not_found' => 'Nothing Found',
'not_found_in_trash' => 'Nothing found in the Trash',
'parent_item_colon' => ''
),
'taxonomies' => array('category',),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'has_archive' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title',
'editor',
'excerpt',
'trackbacks',
'custom-fields',
'comments',
'revisions',
'thumbnail',
'author',
'page-attributes',)
)
);
}
add_action( 'init', 'create_phone_post_type' );
and in category page:
<?php if(have_posts()) : ?>
<div class="title_page_category">
<h5> <?php printf( __( 'Category: %s', 'anaximander' ), '<span>' . single_cat_title( '', true ) . '</span>' );?></h5>
</div>
<?php
$args = array('post_type' => 'phones',
'posts_per_page' => 20);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="col-lg-3">
<div class="phone_thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" >
<?php the_post_thumbnail(); ?>
</a>
</div>
<div class="phone_title">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" >
<?php the_title(); ?>
</a>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
But when I added several phone in different category, all of phones going inside in one category.
I'm really confused. Please help me :(
We have checked your custom post type and custom taxonomy code and i think issue in your custom taxonomy code, Your custom taxonomy name is "category" it has create issue because "category" is WordPress default post taxonomy name.
Please add below code in Your functions.php file to create custom post type and hist custom texonomy in wordpress.
Custom Post type
<?php
function my_custom_phones() {
$labels = array(
'name' => _x( 'phones', 'post type general name' ),
'singular_name' => _x( 'phones', 'post type singular name' ),
'add_new' => _x( 'Add New', 'phones' ),
'add_new_item' => __( 'Add New phones' ),
'edit_item' => __( 'Edit phones' ),
'new_item' => __( 'New phones' ),
'all_items' => __( 'All phones' ),
'view_item' => __( 'View phones' ),
'search_items' => __( 'Search phones' ),
'not_found' => __( 'No phones found' ),
'not_found_in_trash' => __( 'No phones found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Phones'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our phones and phones specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
);
register_post_type( 'phones', $args );
}
add_action( 'init', 'my_custom_phones' ); ?>
Custom taxonomy Code
<?php
function my_taxonomies_Phones() {
$labels = array(
'name' => _x( 'Phones Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Phones Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Phones Categories' ),
'all_items' => __( 'All Phones Categories' ),
'parent_item' => __( 'Parent Phones Category' ),
'parent_item_colon' => __( 'Parent Phones Category:' ),
'edit_item' => __( 'Edit Phones Category' ),
'update_item' => __( 'Update Phones Category' ),
'add_new_item' => __( 'Add New Phones Category' ),
'new_item_name' => __( 'New Phones Category' ),
'menu_name' => __( 'Phones Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'Phones_cat', 'Phones', $args );
}
add_action( 'init', 'my_taxonomies_Phones', 0 );
?>
Use Below Query code Instead of Your query code, Below query display category wise post list.
<?php
$catid = $wp_query->get_queried_object_id(); /* get category id of current category */
$args = array(
'posts_per_page' => 20,
'offset' => 0,
'tax_query' => array(
array(
'taxonomy' => 'Phones_cat', /* Your custom post type category name*/
'field' => 'term_id',
'terms' => $catid,
),
),
'orderby' => 'rand',
'post_type' => 'phones',
'post_status' => 'publish'
);
$queryall = new WP_Query($args);
?>

WordPress - Need consistent URLs for Custom Posts

I have created a custom posts with the following code:
// 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'),
'hierarchical' => true
)
);
}
// 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,
//'rewrite' => array('slug' => 'tours')
);
//register_taxonomy( 'tour_category', 'tours', $args );
register_taxonomy( 'tour_category', 'tours', $args );
}
add_action( 'init', 'my_taxonomies_tours', 0 );
In my permalink settings I have setup:
http://mydomainname.com.au/%category%/%postname%/
I want to display the URLs like so:
display posts in categories
http://mydomainname.com.au/tours/gbr/
http://mydomainname.com.au/tours/whitehaven-tours/
http://mydomainname.com.au/tours/day-tours/
Display posts
http://mydomainname.com.au/tours/gbr/name-of-post
http://mydomainname.com.au/tours/whitehaven-tours/name-of-post
http://mydomainname.com.au/tours/day-tours/name-of-post
But I'm currently getting the following URLs:
display posts in categories
http://mydomainname.com.au/tour_category/gbr/
http://mydomainname.com.au/tour_category/whitehaven-tours/
http://mydomainname.com.au/tour_category/day-tours/
Display posts
http://mydomainname.com.au/tours/name-of-post
http://mydomainname.com.au/tours/name-of-post
http://mydomainname.com.au/tours/name-of-post
I am not familiar enough with URL rewrites to make the necessary changes. I have read many articles but getting nowhere fast. Any help in the right direction would be greatly appreciated.

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