如果您需要在帖子中显示国家的国旗,那么最好的选择是使用自定义字段和您自己的函数,该函数将知道如何将自定义字段的值转换为正确的国家图像。
您还可以使用自定义字段为其创建自定义分类法(世界上最好的教程:how to – WordPress Category Extra Fields
) 它可以很好地工作,并且可以很容易地将一篇文章连接到一个国家(就像选择一个类别一样简单),但您必须创建上传功能或上传为媒体,并将url保存为自定义分类字段。
但是,如果您正在寻找最简单的解决方案,那么创建自定义帖子类型将允许您使用帖子编辑面板中的特色图像功能和简单元框,并可以选择将国家/地区的自定义帖子类型id保存为自定义字段。
碰巧我有一个类,它设置了一个元盒来让你做到这一点,只要确保你设置了正确的帖子类型:
class post_to_post_by_id {
function __construct() {
/* Define meta box */
add_action( \'add_meta_boxes\', array($this,\'myplugin_add_custom_box\' ));
/* save meta box */
add_action( \'save_post\', array($this, \'myplugin_save_postdata\' ));
}
/* Define the custom box */
/* Adds a box to the main column on the Post edit screen */
function myplugin_add_custom_box() {
//add metabox to post edit screen
add_meta_box(
\'county_sectionid\',
__( \'Select Countries\', \'myplugin_textdomain\' ),
array($this,\'myplugin_inner_custom_box\'),
\'post\'
);
}
/* Prints the metabox content */
function myplugin_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\' );
//define your country field settings
$ptype = \'page\';
$field_id = \'post_countries\';
$field_desc = \'this is the field description\';
// The actual fields for data entry
$data = get_post_meta($post->ID,\'post_countries\',true);
if (empty($data)){
$data = array();
}
$args = array( \'numberposts\' => -1, \'post_type\'=>$ptype ,\'post_status\' => \'publish\' );
$posts = get_posts($args);
if ($posts){
foreach ( $posts as $p ){
$checked = \'\';
if (is_array($data)){
if ( in_array( $p->ID, $data ) ){
$checked = \' checked="checked"\';
}
}
echo \'<input value="\'.$p->ID.\'" \'.$checked .\' type="checkbox" name="\'.$field_id.\'[]" id="\'.$field_id.\'[]"> \'.$p->post_title.\' \';
}
echo \'<br /><span class="description">\'.$field_desc.\'</span>\';
}else{
echo \'No Posts\';
}
}
/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $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;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if (!isset($_POST[\'myplugin_noncename\']))
return;
if ( !wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) )
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[\'post_countries\'])){
update_post_meta($post_id,\'post_countries\',$_POST[\'post_countries\']);
}else{
delete_post_meta($post_id,\'post_countries\');
}
}
}
new post_to_post_by_id();