맞아, 나는 최근에 내가 한 일인 것처럼 당신의 대답을 가지고 있습니다. 이것은 정말 맞춤 대사에 관한 질문입니다. 여기에 약간의 자료가 있습니다 - 나는 이것을 추천하는 친구에게서 그것에 대한 링크를 보냈습니다;
http://www.deluxeblogtips.com/meta-box/
그리고 뼈 테마에서 저자는 이것을 권장
; 당신이 빨리와 가야 할 경우
https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
나는 여기에 몇 가지 코드를 게시 할 예정입니다. 나는 그것의 파일에 다음 코드를 넣고 functions.php에서 그것을 포함시킬 것이다.
require_once('includes/metabox-post.php');
테마 디렉토리에 includes 디렉토리를 만들고이 코드가 포함 된 파일을 만듭니다.
<?php
/* CUSTOM METABOX -----------------------------------------------------*/
//We create an array called $meta_box and set the array key to the relevant post type
$meta_box_post['post'] = array(
//This is the id applied to the meta box
'id' => 'post-format-meta',
//This is the title that appears on the meta box container
'title' => 'My Custom Metabox',
//This defines the part of the page where the edit screen section should be shown
'context' => 'normal',
//This sets the priority within the context where the boxes should show
'priority' => 'high',
//Here we define all the fields we want in the meta box
'fields' => array(
array(
'name' => 'Home Slider Link',
'desc' => 'You can create a custom link for the home slider image (ie to link to the shop). If left blank, it will by default link through to this post.',
'id' => 'home-slide-link',
'type' => 'text',
'default' => ''
)
)
);
add_action('admin_menu', 'meta_add_box_post');
//Add meta boxes to post types
function meta_add_box_post() {
global $meta_box_post;
foreach($meta_box_post as $post_type => $value) {
add_meta_box($value['id'], $value['title'], 'meta_format_box_post', $post_type, $value['context'], $value['priority']);
}
}
//Format meta boxes
function meta_format_box_post() {
global $meta_box_post, $post;
// Use nonce for verification
echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box_post[$post->post_type]['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>'.
'<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" />'. '<br />'. $field['desc'];
break;
case 'textarea':
echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
break;
case 'select':
echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
foreach ($field['options'] as $option) {
echo '<option '. ($meta == $option ? ' selected="selected"' : '') . '>'. $option . '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ($meta == $option['value'] ? ' checked="checked"' : '') . ' />' . $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ($meta ? ' checked="checked"' : '') . ' /<br /> '. $field['desc'];
break;
}
echo '<td>'.'</tr>';
}
echo '</table>';
}
// Save data from meta box
function meta_save_data_post($post_id) {
global $meta_box_post, $post;
//Verify nonce
if (!wp_verify_nonce($_POST['plib_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_post[$post->post_type]['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'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
add_action('save_post', 'meta_save_data_post');
?>
이렇게하면 대체 url에 입력 할 수있는 게시물에 새로운 맞춤 대사가 추가됩니다. 이 사용자 정의 옵션의 id는 홈 슬라이드 링크입니다. 해당 URL을 사용하려면 Nivoslider 이미지 목록을 작성하는 동안 템플릿 루프에 다음을 포함시킵니다. 포스트 슬라이더 링크에 대해 설정된 URL이있는 경우
<?php
if (get_post_meta($post->ID, 'home-slide-link', true)) :
$slideLink = get_post_meta($post->ID, 'home-slide-link', true);
else :
$slideLink = get_permalink();
endif;
echo '<a href="'. $slideLink .'"><img src="image link in here" /></a>';
?>
그래서 그것은 영구 링크에 해당하지 않을 경우 기본값을 사용합니다.
희망이 조금 도움이 되었어요!
매우 고맙습니다. –
아름답게 일했습니다! –