本文實例講述了WordPress實現的首頁幻燈片展示功能。分享給大家供大家參考,具體如下:
對于WordPress拓展性這么優秀的程序來說,是沒有什么不能實現的。很多在建站的時候,都會選擇在首頁使用幻燈片,可以展示比較醒目的內容。今天就來一個首頁幻燈片的制作教程,相信幻燈片在各種企業包括個人網站上面用處還是很大的,做完之后效果和本站首頁的一樣。
主要是使用了WordPress的自定義文章字段的功能來判斷是否需要顯示在首頁:
1.創建Post Meta Box
復制代碼
代碼如下:/* Fire our meta box setup function on the post editor screen. */
add_action( 'load-post.php', 'sola_post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'sola_post_meta_boxes_setup' );
/* 這是需要修改的兩處之一,本功能只需要一個checkbox,將checkbox的title、id等屬性填充到$fields數組中,
后面的代碼會自動根據數組填充的內容創建Post Meta Box */
$fields = array(
array(
'name' => __('是否在首頁幻燈顯示'),
'desc' => 'Check this box and make the post a slider',
'id' => 'sola-post-slider',
'type' => 'checkbox',
'default' => ''
)
);
/* Meta box setup function. */
function sola_post_meta_boxes_setup() {
/* Add meta boxes on the 'add_meta_boxes' hook. */
add_action( 'add_meta_boxes', 'sola_add_post_meta_boxes' );
add_action( 'save_post', 'sola_save_post_meta_boxes', 10, 2 );
}
/* Create one or more meta boxes to be displayed on the post editor screen. */
/* 這里也需要改一下,設置需要創建的Post Meta Box叫什么名字,顯示在什么位置 */
function sola_add_post_meta_boxes() {
add_meta_box(
'sola-post-slider-class', // Unique ID
__('首頁幻燈片'), // Title
'sola_seo_box_format', // Callback function
'post', // Admin page (or post type)
'side', // Context
'default' // Priority
);
}
function sola_seo_box_format(){
global $fields,$post;
// Use nonce for verification
echo '<input type="hidden" name="sola_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($fields as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>'.
'<th><label for="'. $field['id'] .'">'. $field['name']. '</strong></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%" />'. '
'. $field['desc'];
break;
case 'textarea':
echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . ''. '
'. $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"' : '' ) . ' />';
break;
}
echo '<td>'.'</tr>';
}
echo '</table>';
}
function sola_save_post_meta_boxes($post_id) {
global $fields, $post;
//Verify nonce
if (!wp_verify_nonce($_POST['sola_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
//Check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
//Get the post type object.
$post_type = get_post_type_object( $post->post_type );
//Check permissions
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
foreach ($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( 'load-post.php', 'sola_post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'sola_post_meta_boxes_setup' );
/* 這是需要修改的兩處之一,本功能只需要一個checkbox,將checkbox的title、id等屬性填充到$fields數組中,
后面的代碼會自動根據數組填充的內容創建Post Meta Box */
$fields = array(
array(
'name' => __('是否在首頁幻燈顯示'),
'desc' => 'Check this box and make the post a slider',
'id' => 'sola-post-slider',
'type' => 'checkbox',
'default' => ''
)
);
/* Meta box setup function. */
function sola_post_meta_boxes_setup() {
/* Add meta boxes on the 'add_meta_boxes' hook. */
add_action( 'add_meta_boxes', 'sola_add_post_meta_boxes' );
add_action( 'save_post', 'sola_save_post_meta_boxes', 10, 2 );
}
/* Create one or more meta boxes to be displayed on the post editor screen. */
/* 這里也需要改一下,設置需要創建的Post Meta Box叫什么名字,顯示在什么位置 */
function sola_add_post_meta_boxes() {
add_meta_box(
'sola-post-slider-class', // Unique ID
__('首頁幻燈片'), // Title
'sola_seo_box_format', // Callback function
'post', // Admin page (or post type)
'side', // Context
'default' // Priority
);
}
function sola_seo_box_format(){
global $fields,$post;
// Use nonce for verification
echo '<input type="hidden" name="sola_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($fields as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>'.
'<th><label for="'. $field['id'] .'">'. $field['name']. '</strong></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%" />'. '
'. $field['desc'];
break;
case 'textarea':
echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . ''. '
'. $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"' : '' ) . ' />';
break;
}
echo '<td>'.'</tr>';
}
echo '</table>';
}
function sola_save_post_meta_boxes($post_id) {
global $fields, $post;
//Verify nonce
if (!wp_verify_nonce($_POST['sola_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
//Check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
//Get the post type object.
$post_type = get_post_type_object( $post->post_type );
//Check permissions
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
foreach ($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);
}
}
}
這段代碼會在文章創建和編輯頁面創建如下所示的Post Meta Box :

寫文章時,勾選在首頁顯示幻燈片,這篇文章就會自動推送到首頁幻燈片中。
2.讀取幻燈片文章
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。