با سلام خدمت شما کاربران شرکت طراحی قالب وردپرس آنفایو.
بنا به درخواست شما، شرکت طراحی قالب وردپرس آنفایو تصمیم گرفته که نحوه افزودن فیلد به پست های قالب را آموزش دهد. همراه شرکت طراحی قالب وردپرس آنفایو باشید. امکان ساخت فیلدهای دلخواه در وردپرس را coustom fild می نامند که در وردپرس توابع آن تعریف شده اند اما به صورت عادی در اختیار عموم قرار نگرفته اند.
6 تابع وجود دارد که میتوان با آنها فیلد ها را در طراحی قالب وردپرس مدیریت کرد.

add_post_meta()
update_post_meta()
delete_post_meta()
get_post_custom()
get_post_custom_values()
get_post_custom_keys()

حال بیایید کار خود را ساده تر کنیم استفاده از اکشن و فانکشن و کلی چیز های دیگه خیلی ها رو گیج کرده و از ساخت کاستوم فیلد با وردپرس منصرف کرده. شرک طراحی قالب وردپرس آنفایو تمام این قسمت ها را کدنویسی کرده و به صورت ساده تری در اختیار شما قرار میدهد. کافیست کدهای زیر را در فانکشن قرار دهید سپس در همین مطلب بعد از کد ها نحوه افزودن کاستوم فیلد را مطالعه کنید. طراحی قالب وردپرس مهارت ماست.


 

<?php
add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args );
*/
if ( ! function_exists('add_ht_post_options_cb') ) {
function add_ht_post_options_cb() {
add_meta_box(
'Javad_id',
'تنظیمات پست',
'show_ht_post_options_cb',
'post',
'normal',
'high');
}
}
add_action('add_meta_boxes', 'add_ht_post_options_cb');
$prefix = "_";
$custom_post_meta_fields = array(
/*    array(
'label'=> __('Slideshow Status', 'highthemes'),
'desc'  => __('You can override the slideshow status for this page/post.', 'highthemes'),
'id'    => $prefix.'slideshow_status',
'type'  => 'select',
'options' => array (
'default' => array (
'label' => __('Default Settings', 'highthemes'),
'value' => ''
),
'enable' => array (
'label' => __('Enable Slideshow', 'highthemes'),
'value' => 1
),
'disable' => array (
'label' => __('Disable Slideshow', 'highthemes'),
'value' => 'false'
)
)
),
----------------------------------
*/
/*
array (
'label' => __('titr', 'highthemes'),
'desc'  => __('discription', 'highthemes'),
'id'    => $prefix.'slideshow_category',
'type'  => 'text',
'options' => $sterms_array
)  ,
array(
'label' =>__('Disable Sidebar?', 'highthemes'),
'desc'  =>__('Check to disable sidebar', 'highthemes'),
'id'    =>$prefix.'disable_sidebar',
'type'  =>'checkbox'
),
array(
'label' =>__('Featured image on single page', 'highthemes'),
'desc'  =>__('You can enable/disable featured image on this page.', 'highthemes'),
'id'    =>$prefix.'disable_post_image',
'type'  =>'select',
'options'=>array(
'select' => array(
'label' => __('Select An Option', 'highthemes'),
'value' =>''
),
'true' => array (
'label' => __('Enable', 'highthemes'),
'value' => 'true'
),
'false' => array (
'label' => __('Disable', 'highthemes'),
'value' => 'false'
)
)
),
array(
'label'=> __('Embed Video ', 'highthemes'),
'desc'  => __('To embed a video, you can paste its link here.  what type? (http://www.no-margin-for-errors.com/projects/prettyphoto-jquery-lightbox-clone/)', 'highthemes'),
'id'    => $prefix.'video_link',
'type'  => 'text'
),
array(
'label'=> __('External Link for Link Post Format ', 'highthemes'),
'desc'  => __('Paste an external link address, if you\'ve chosen "link" post format type. ', 'highthemes'),
'id'    => $prefix.'link_post_format',
'type'  => 'text'
),
array(
'label'=> __('Video Background URL', 'highthemes'),
'desc'  => __('Enter the URL of the Background Video(Youtube/Vimeo/Dailymotion or hosted MP4, flv) here. It will override the background slideshow. ', 'highthemes'),
'id'    => $prefix.'page_video',
'type'  => 'text'
),
array(
'label' =>__('Autoplay Video?', 'highthemes'),
'desc'  =>__('If checked video will autoplay.', 'highthemes'),
'id'    =>$prefix.'video_autoplay',
'type'  =>'checkbox'
),
array(
'label' =>__('Repeat Video?', 'highthemes'),
'desc'  =>__('If checked video will repeat. (if you embedded a dailymotion video, you need to check autoplay option too.)', 'highthemes'),
'id'    =>$prefix.'video_repeat',
'type'  =>'checkbox'
),
array(
'label' =>__('Controlbar Video?', 'highthemes'),
'desc'  =>__('If checked  hide the controlbar. (only for self-hosted videos)', 'highthemes'),
'id'    =>$prefix.'video_controlbar',
'type'  =>'checkbox'
),
array(
'label' =>__('Hide content', 'highthemes'),
'desc'  =>__('If you want to hide the page content, check this box.', 'highthemes'),
'id'    =>$prefix.'content_hide',
'type'  =>'checkbox'
)*/
);
if ( ! function_exists('show_ht_post_options_cb') ) {
function show_ht_post_options_cb() {
global $custom_post_meta_fields, $post;
echo '<input type="hidden" name="custom_post_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
echo '<table class="form-table">';
foreach ($custom_post_meta_fields as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
<br /><span class="description">'.$field['desc'].'</span>';
break;
// checkbox
case 'checkbox':
echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/>
<label for="'.$field['id'].'">'.$field['desc'].'</label>';
break;
// checkbox_group
case 'checkbox_group':
foreach ($field['options'] as $option) {
echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />
<label for="'.$option['value'].'">'.$option['label'].'</label><br />';
}
echo '<span class="description">'.$field['desc'].'</span>';
break;
// select
case 'select':
echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
}
echo '</select><br /><span class="description">'.$field['desc'].'</span>';
break;
}
echo '</td></tr>';
}
echo '</table>';
}
}
// Save the Data
if ( ! function_exists('save_custom_post_meta') ) {
function save_custom_post_meta($post_id) {
global $custom_post_meta_fields;
if(isset($_POST['custom_post_meta_box_nonce'])){
if (!wp_verify_nonce($_POST['custom_post_meta_box_nonce'], basename(__FILE__)))
return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
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;
}
// loop through fields and save the data
foreach ($custom_post_meta_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', 'save_custom_post_meta');
?>

در هنگام کپی پیست کد ها حتماً متوجه کدهای کامنت شده شده اید، برای افزودن یکی از آنها را بر حسب نیاز انتخاب کرده و سپس آنها را از کامنت خارج کرده و تنظیمات دلخواه را بر آن اعمال کنید.
تنظیماتی که شما بر این کد ها اعمال میکنید. عبارتند از نوع، عبارتی برای شناسایی، نامی برای استفاده در طراحی قالب وردپرس ( کد نویسی ).
انواع فیلد ها در کدهای بالا ایجاد شده اند اما باز هم با تغیر type شما میتوانید نوع فیلد ها را تغیر دهید.
عبارات های شناسایی دو نوع هستند تیتر و توضیحات، تیتر فیلد شما همان label می باشد و توضیحات نیز در desc قرار میگیرد.
در انتها شما کدی شبیه به کد زیر را از کامنت ( توضیحات ) خارج کرده و اطلاعات دلخواه را درج میکنید.

array (
'label' => __('روتیتر پست', 'highthemes'),
'desc'  => __('روتیتر پست را وارد کنید', 'highthemes'),
'id'    => $prefix.'slideshow_category',
'type'  => 'text',
'options' => $sterms_array
)  ,

حال شما کاستوم فیلد خود را در طراحی قالب وردپرس خود ایجاد کرده اید. اما چگونه می توان آن را دریافت کرد. برای دریافت آن نیز تابعی به نام (get_post_custom_values() ) وجود دارد که با دادن id به آن مقدار کاستوم فیلد ساخته می شود.
توجه: به خاطر عدم نمایش کاستوم فیلد های ساخته شده به کاربر به اول آنها با استفاده از $prefix یک آندرلاین اضافه میگردد پس برای دریافت به اول کاستوم فیلد های خود یک “_” اضافه کنید.

/**/
$tmp1 = get_post_custom_values('_part');
<?php if($tmp1[0]){ echo $tmp1[0] } ?>
/**/

 


در خط اول کدها ما آرایه ای را دریافت کرده و در خط دوم اندیس 0 آن را چاپ میکنیم

منتظر آموزش های بعدی شرکت طراحی قالب وردپرس آنفایو باشید.

نظر خود را بگذارید

-- بارگیری کد امنیتی --