Custom Meta In Template from Taxonomy
-
I created a set of functions inside functions.php to build out menus for photos, episodes and casts as shown here:
<?php function post_type_cast() { register_post_type( 'cast', array( 'labels' => array( 'name' => __('Cast'), 'singular_name' => __('Cast'), 'add_new' => __('Add New'), 'add_new_item' => __('Add New Cast Member'), 'edit' => __( 'Edit' ), 'edit_item' => __( 'Edit Cast Member' ), 'new_item' => __( 'New Cast Member' ), 'view' => __( 'View Cast Members' ), 'view_item' => __( 'View Cast Member' ), 'search_items' => __( 'Search Cast Members' ), 'not_found' => __( 'No cast members found' ), 'not_found_in_trash' => __( 'No cast members found in Trash' ), 'parent' => __( 'Parent Cast Members' ), ), 'public' => true, 'show_ui' => true, 'menu_position' => 4, 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ) ) ); } add_action('init', 'post_type_cast'); function post_type_episode() { register_post_type( 'episode', array( 'labels' => array( 'name' => __('Episode'), 'singular_name' => __('Episode'), 'add_new' => __('Add New'), 'add_new_item' => __('Add New Episode'), 'edit' => __( 'Edit' ), 'edit_item' => __( 'Edit Episode' ), 'new_item' => __( 'New Episode' ), 'view' => __( 'View Episodes' ), 'view_item' => __( 'View Episode' ), 'search_items' => __( 'Search Episodes' ), 'not_found' => __( 'No episodes found' ), 'not_found_in_trash' => __( 'No episodes found in Trash' ), 'parent' => __( 'Parent Episode' ), ), 'public' => true, 'show_ui' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields' ) ) ); } add_action('init', 'post_type_episode'); function post_type_photos() { register_post_type( 'photos', array( 'labels' => array( 'name' => __('Photos'), 'singular_name' => __('Photos'), 'add_new' => __('Add New'), 'add_new_item' => __('Add New Photo'), 'edit' => __( 'Edit' ), 'edit_item' => __( 'Edit Photo' ), 'new_item' => __( 'New Photo' ), 'view' => __( 'View Photos' ), 'view_item' => __( 'View Photo' ), 'search_items' => __( 'Search Photos' ), 'not_found' => __( 'No photos found' ), 'not_found_in_trash' => __( 'No photos found in Trash' ), 'parent' => __( 'Parent Photo' ), ), 'public' => true, 'show_ui' => true, 'menu_position' => 6, 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ) ) ); } add_action('init', 'post_type_photos'); add_action('init', 'build_taxonomies'); function build_taxonomies() { register_taxonomy( 'series', array( 'episode', 'cast', 'photos' ), array( 'hierarchical' => true, 'rewrite' => true, 'label' => __('Series'), 'rewrite' => array( 'slug' => 'show' ) ) ); } add_action("template_redirect", 'my_template_redirect'); // Template selection function my_template_redirect() { global $wp; global $wp_query; if ($wp->query_vars["post_type"] == "cast") { // Let's look for the property.php template file in the current theme if (have_posts()) { require(TEMPLATEPATH . '/category-cast.php'); die(); } else { $wp_query->is_404 = true; } } if ($wp->query_vars["post_type"] == "episode") { // Let's look for the property.php template file in the current theme if (have_posts()) { include(TEMPLATEPATH . '/episode.php'); die(); } else { $wp_query->is_404 = true; } } if ($wp->query_vars["post_type"] == "photos") { // Let's look for the property.php template file in the current theme if (have_posts()) { include(TEMPLATEPATH . '/photos.php'); die(); } else { $wp_query->is_404 = true; } } } add_action("admin_init", "add_social"); add_action('save_post', 'update_social'); function add_social(){ add_meta_box("social_details", "Social Options", "social_options", "cast", "normal", "low"); } function social_options(){ global $post; $custom = get_post_custom($post->ID); $twitter_url = $custom["twitter_url"][0]; $twitter_widget_code = $custom["twitter_widget_code"][0]; $facebook_url = $custom["facebook_url"][0]; $facebook_widget_code = $custom["facebook_widget_code"][0]; $website_url = $custom["website_url"][0]; $thumbnail_url = $custom["thumbnail_url"][0]; $main_photo_url = $custom["main_photo_url"][0]; $contestant_spotlight = $custom["contestant_spotlight"][0]; $ischecked = ($contestant_spotlight == yes)? "checked" : ""; ?> <div id="portfolio-options"> <p><label><strong>Twitter URL:</strong></label><br /> <input name="twitter_url" value="<?php echo $twitter_url; ?>" style="width: 400px;" /></p> <p><label><strong>Twitter Widget Code:</strong></label><br /> <textarea rows="6" cols="48" name="twitter_widget_code" style="border: 1px solid #848484;"><?php echo $twitter_widget_code; ?></textarea></p> <p><label><strong>Facebook URL:</strong></label><br /> <input name="facebook_url" value="<?php echo $facebook_url; ?>" style="width: 400px;" /></p> <p><label><strong>Facebook Widget Code:</strong></label><br /> <textarea rows="6" cols="48" name="facebook_widget_code" style="border: 1px solid #848484;"><?php echo $facebook_widget_code; ?></textarea></p> <p><label><strong>Website URL:</strong></label><br /> <input name="website_url" value="<?php echo $website_url; ?>" style="width: 400px;" /></p> <p><label><strong>Thumbnail URL:</strong></label><br /> <input name="thumbnail_url" value="<?php echo $thumbnail_url; ?>" style="width: 400px;" /></p> <p><label><strong>Main Photo URL:</strong></label><br /> <input name="main_photo_url" value="<?php echo $main_photo_url; ?>" style="width: 400px;" /></p> <p><label><strong>Contestant Spotlight:</strong></label><br /> <input type="checkbox" name="contestant_spotlight" value="yes" <?php echo $ischecked; ?> /> (note: checking this box enabled the spotlight page)</p> </div><!--end portfolio-options--> <?php } function update_social(){ global $post; update_post_meta($post->ID, "twitter_url", $_POST["twitter_url"]); update_post_meta($post->ID, "twitter_widget_code", $_POST["twitter_widget_code"]); update_post_meta($post->ID, "facebook_url", $_POST["facebook_url"]); update_post_meta($post->ID, "facebook_widget_code", $_POST["facebook_widget_code"]); update_post_meta($post->ID, "website_url", $_POST["website_url"]); update_post_meta($post->ID, "thumbnail_url", $_POST["thumbnail_url"]); update_post_meta($post->ID, "main_photo_url", $_POST["main_photo_url"]); update_post_meta($post->ID, "contestant_spotlight", $_POST["contestant_spotlight"]); } ?>
As you can see, there are options that are set inside the social_options function.
Inside my_template_redirect() I look at each post type and specify a custom template to use to be cleaner.
Since I am loading off of my custom taxonomy “show”, the url would be something like this:
/show/showname/?post_type=cast
My issue is this. I can’t seem to get the custom post meta to appear on the category pages.
Here is category-cast.php:
<?php get_header(); ?> <div id="content"> <h3>Archive ~ <?php the_time('F Y'); ?></h3> <?php $my_query = new WP_Query( array( 'post_type' => 'cast', 'posts_per_page' => 10 ) ); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?> <?php if (get_post_meta($post->ID, thumbnail_url)) { ?> <p><img src="<?php echo get_post_meta($post->ID, thumbnail_url, true); ?>" alt="" title="" /></p> <?php } ?> <?php if (get_post_meta($post->ID, contestant_spotlight)) { ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><br /> <?php } else { ?> <p><?php the_title(); ?><br /> <?php } ?> <?php endwhile; ?> <div id="pagination"> <?php if ( $paged < 1 ) { // For the first page ?><a href="#top">Back to the top</a><?php } ?><?php posts_nav_link(' ~ ', __('← Previous page'), __('Next page →')); ?> </div> </div> <?php get_footer(); ?>
Anyone have any experience with this issue where the custom data doesn’t render?
- The topic ‘Custom Meta In Template from Taxonomy’ is closed to new replies.