php Wordpress:自定义帖子模板
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Wordpress:自定义帖子模板相关的知识,希望对你有一定的参考价值。
<?php
/* Custom Post Template */
add_action('init', 'register_custom_post');
function register_custom_post() {
$labels = array(
'name' => _x('Custom Post Name', 'post type general name'),
'singular_name' => _x('Custom Post Name', 'post type singular name'),
'add_new' => _x('Add New', 'Custom Post Name'),
'add_new_item' => __('Add New Custom Post Name'),
'edit_item' => __('Edit Custom Post Name'),
'new_item' => __('New Custom Post Name'),
'view_item' => __('View Custom Post Name'),
'search_items' => __('Search Custom Post Name'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'my_custom_post' , $args );
}
/* register taxonomy */
function register_custom_post_taxonomy() {
register_taxonomy( 'taxonomy_name', 'custom_post_name', array(
'label'=> 'Menu Categories',
'singular_label' => 'Menu Category',
'show_ui' => 'radio',
'rewrite'=>array('slug'=>'custom_post_name/taxonomy_name'),
'hierarchical' => true, // Makes for a better selection box on write screen
)
);
}
add_action('init', 'register_custom_post_taxonomy');
/* if you need multiple featured items, use "MultiPostThumbnails" plugin */
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(array('label' => 'Map', 'id' => 'post-meta-name', 'post_type' => 'my_custom_post') );
}
function custom_post_values() {
global $post;
// job title => post title
// job description => post description
$custom = get_post_custom($post->ID);
/* NOTE: DON'T DO cleaning here, i.e. esc_attr, strip_tags, esc_textarea, wp_kses */
/* some examples show
$item = $custom['item'][0]? esc_attr(strip_tags($custom['item'])): '';
But this just turns '&' into '&' inside of the input box and then it displays like that on the web page */
$item = $custom['item'][0]; // longer description
// We'll use this nonce field later on when saving.
wp_nonce_field( 'my_meta_box_custom_post_nonce', 'custom_post_nonce' );
?>
<script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function() {
// easier way to include javascript OR do an admin hook
});
</script>
<table>
<tr>
<td><p><label for="item">Item</label></p>
<p><input type="text" name="item" id="item" value="<?php echo $item; ?>"><p>
</td>
</tr>
</table>
<?php
}
add_action( 'save_post', 'save_custom_post_meta' );
/* When the post is saved, saves our custom data */
function save_custom_post_meta( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['custom_post_nonce'] ) || !wp_verify_nonce( $_POST['custom_post_nonce'], 'my_meta_box_custom_post_nonce' ) ) return;
// Check permissions
if ( 'page' == $_POST['post_type'] )
{
if ( !current_user_can( 'edit_page', $post_id ) )
return;
}
else
{
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
// OK, we're authenticated: we need to find and save the data
if (isset($_POST['item'])) {
/* do cleaning here, i.e. esc_attr, strip_tags, esc_textarea, wp_kses */
update_post_meta($post_id, 'item', esc_attr(strip_tags($_POST['item'])));
}
}
add_action('manage_edit-my_custom_post_columns', 'my_custom_post_edit_columns');
add_action('manage_posts_custom_column', 'my_custom_post_custom_columns');
function my_custom_post_edit_columns($columns) {
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "title",
"image"=> "Image",
"item" => "Item"
);
return $columns;
}
function my_custom_post_custom_columns($column) {
global $post;
switch($column) {
case "item":
$custom = get_post_custom();
echo $custom['item'][0];
break;
}
}
?>
以上是关于php Wordpress:自定义帖子模板的主要内容,如果未能解决你的问题,请参考以下文章
Wordpress ACF 字段如何从自定义帖子类型中获取选项