wordpress 添加文章类型

<?php
global $meta_box_video;
$meta_box_video = array(
	'id' => 'f-meta-box-video',
	'title' =>  __('Video Settings'),
	'page' => 'post',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		array( "name" => __('Desciption'),
				"desc" => __('Input the video desciption.'),
				"id" => "video_desc1",
				"type" => "textarea"
			)
	)
	
);

add_action('admin_menu', 'add_video_box');
function add_video_box() {
	global $meta_box_video;
	add_meta_box($meta_box_video['id'], $meta_box_video['title'], 'show_box_video', $meta_box_video['page'], $meta_box_video['context'], $meta_box_video['priority']);
}


function show_box_video() {
	global $meta_box_video, $post;

	// Use nonce for verification
	echo '<input type="hidden" name="f_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
 
	echo '<table class="form-table">';
 
	foreach ($meta_box_video['fields'] as $field) {
		// get current post meta data
		$meta = get_post_meta($post->ID, $field['id'], true);
		switch ($field['type']) {
 
			
			//If Text		
			case 'text':
			
			echo '<tr>',
				'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style=" display:block; color:#999; margin:5px 0 0 0; line-height: 18px;">'. $field['desc'].'</span></label></th>',
				'<td>';
			echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? htmlentities( $meta ) : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '" size="30" style="width:75%; margin-right: 20px; float:left;" />';
			
			break;
 
			
			//If textarea		
			case 'textarea':
			
			echo '<tr>',
				'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style="line-height:18px; display:block; color:#999; margin:5px 0 0 0;">'. $field['desc'].'</span></label></th>',
				'<td>';
			echo wp_editor(($meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES))), $field['id'], array('dfw' => true, 'tabfocus_elements' => 'sample-permalink,post-preview', 'editor_height' => 360) );
			
// 			echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '" rows="8" cols="5" style="width:100%; margin-right: 20px; float:left;">', $meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '</textarea>';
			
			break;
			
			//If Select	
			case 'select':
			
				echo '<tr>',
				'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style=" display:block; color:#999; margin:5px 0 0 0;">'. $field['desc'].'</span></label></th>',
				'<td>';
			
				echo'<select name="'.$field['id'].'">';
			
				foreach ($field['options'] as $option) {
					
					echo'<option';
					if ($meta == $option ) { 
						echo ' selected="selected"'; 
					}
					echo'>'. $option .'</option>';
				
				} 
				
				echo'</select>';
			
			break;

		}

	}
 
	echo '</table>';
}

add_action('save_post', 'f_save_data');


/*-----------------------------------------------------------------------------------*/
/*	Save data when post is edited
/*-----------------------------------------------------------------------------------*/
 
function f_save_data($post_id) {
	global $meta_box_video;
 
	// verify nonce
	if (!wp_verify_nonce($_POST['f_meta_box_nonce'], basename(__FILE__))) {
		return $post_id;
	}
 
	// check autosave
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return $post_id;
	}
 
	// check permissions
	if ('page' == $_POST['post_type']) {
		if (!current_user_can('edit_page', $post_id)) {
			return $post_id;
		}
	} elseif (!current_user_can('edit_post', $post_id)) {
		return $post_id;
	}
	
	foreach ($meta_box_video['fields'] as $field) {
		$old = get_post_meta($post_id, $field['id'], true);
		$new = $_POST[$field['id']];
 
		if ($new && $new != $old) {
			update_post_meta($post_id, $field['id'], stripslashes($new));
		} elseif ('' == $new && $old) {
			delete_post_meta($post_id, $field['id'], $old);
		}
	}

}


/* wordpress 只支持下面几个 post formats
function get_post_format_strings() {
	$strings = array(
		'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
		'aside'    => _x( 'Aside',    'Post format' ),
		'chat'     => _x( 'Chat',     'Post format' ),
		'gallery'  => _x( 'Gallery',  'Post format' ),
		'link'     => _x( 'Link',     'Post format' ),
		'image'    => _x( 'Image',    'Post format' ),
		'quote'    => _x( 'Quote',    'Post format' ),
		'status'   => _x( 'Status',   'Post format' ),
		'video'    => _x( 'Video',    'Post format' ),
		'audio'    => _x( 'Audio',    'Post format' ),
	);
	return $strings;
}
*/
$formats = array('video');
add_theme_support( 'post-formats', $formats ); 

//得到当前Post type
//$format = get_post_format();
//调用
//$video_desc = get_post_meta(get_the_ID(), 'video_desc1', true);

function f_admin_scripts() {
	wp_register_script('f-post-formats',plugin_dir_url( __FILE__ ).'/f_post_formats.js' , array('jquery'));
	wp_enqueue_script('f-post-formats');
}

add_action('admin_print_scripts', 'f_admin_scripts');
?>


js的内容如下:

jQuery(function(){
	jQuery('#post-formats-select input').change(mataChange);
	
	function hidiAllPostMatas(){
		jQuery('#f-meta-box-video').css('display','none');
	}
	
	function displayVideoFormatMata(){
		hidiAllPostMatas();
		jQuery('#f-meta-box-video').css('display','block');
	}
	
	function mataChange(){
		hidiAllPostMatas();
		if( jQuery('#post-format-video').attr('checked') ){
			displayVideoFormatMata();
		}
	}
	mataChange();
});


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 WordPress 提供的 `wp_set_post_tags()` 函数来为文章添加标签。你可以在文章发布或更新时触发此函数,并将标签作为参数传递给它。 以下是一个简单的示例代码: ```php // 在文章发布或更新时触发 add_action('save_post', 'add_tags_to_post'); function add_tags_to_post($post_id) { // 检查是否为文章类型 if(get_post_type($post_id) != 'post') { return; } // 获取文章对象 $post = get_post($post_id); // 获取文章内容 $content = $post->post_content; // 将文章内容转换为标签数组 $tags = get_tags_from_content($content); // 为文章添加标签 if(!empty($tags)) { wp_set_post_tags($post_id, $tags, true); } } function get_tags_from_content($content) { // 从文章内容中提取标签 // 你可以编写自己的提取逻辑 // 这里只是一个示例 preg_match_all('/<tag>(.*?)<\/tag>/', $content, $matches); // 返回标签数组 return $matches[1]; } ``` 在上面的代码中,我们定义了一个 `add_tags_to_post()` 函数,它会在文章发布或更新时被触发。我们首先检查文章类型,然后获取文章对象和内容。接着,我们调用了 `get_tags_from_content()` 函数,该函数将文章内容转换为标签数组。最后,我们调用了 `wp_set_post_tags()` 函数,将标签数组添加文章中。 需要注意的是,`get_tags_from_content()` 函数中的正则表达式只是一个示例。你需要根据自己的需求编写自己的提取逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值