PHP Wordpress:管理员中用于编辑/发布视图的自定义列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Wordpress:管理员中用于编辑/发布视图的自定义列相关的知识,希望对你有一定的参考价值。

//custom columns
add_action("manage_posts_custom_column", "my_custom_columns_fields");
add_filter("manage_edit-mysite_listings_columns", "my_listing_columns");

add_action("admin_init", "admin_init");

//custom meta box below post description when editing 
function admin_init(){
  add_meta_box("my-custom-columns-meta", "Listing Details", "my_custom_columns", "mysite_listings", "normal", "low");
}

//define listing columns
function my_listing_columns($columns)
{
	$columns = array(
		"cb" => "<input type=\"checkbox\" />",
		"title" => "Listing Title",
		"description" => "Description",		
		"address" => "Address",
		"website" => "Website",
		"phone" => "Phone Number",
		"comments" => "Comments"
	);
	return $columns;
}
//show the custom columns per post->ID
function my_custom_columns_fields($column)
{
	global $post;
	$custom = get_post_custom($post->ID);

	if ("ID" == $column) echo $post->ID;
	elseif ("description" == $column) echo $post->post_content;
	elseif ("address" == $column) echo $custom['address'][0];
	elseif ("website" == $column) echo $custom['website'][0];	
	elseif ("phone" == $column) echo $custom['phone'][0];	
	elseif ("comments" == $column) echo $post->comments;	
}
//build basic form elements to reference custom column fields for input
function my_custom_columns($column)
{
	global $post;
	$custom = get_post_custom($post->ID);

	$address = $custom['address'][0];
	$website = $custom['website'][0];	
	$phone = $custom['phone'][0];	
?>
  <p><label>Address:</label><br />
  <textarea cols="50" rows="5" name="address"><?php echo $address; ?></textarea></p>
  <p><label>Website:</label><br />
  <input type="text" name="website" size="100" value="<?php echo $website; ?>" /></p>
  <p><label>Phone:</label><br />
  <input type="text" name="phone" size="100" value="<?php echo $phone; ?>" /></p>
<?php
}

// hook into save post with our meta columns
add_action('save_post', 'save_details');

// grab all the form inputs and save them associated by post->ID
function save_details(){
  global $post;
 
  update_post_meta($post->ID, "address", $_POST["address"]);
  update_post_meta($post->ID, "website", $_POST["website"]);
  update_post_meta($post->ID, "phone", $_POST["phone"]);

}

以上是关于PHP Wordpress:管理员中用于编辑/发布视图的自定义列的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress 5.4 主题编辑器适用于 LOCALHOST 但不适用于 LIVE 主题编辑器站点

WordPress 搜索仅适用于帖子,不适用于页面

wordpress上传主题错误解决方法

没有样式表在wordpress中正在工作

Wordpress - 抱歉,您无权编辑此项目

WordPress 编辑器未更新文件:无法与站点通信以检查致命错误