wordpress 自定义幻灯片模型



常见的wordpress幻灯片插件有时候不能满足个性化的需求,或者修改起来稍麻烦。如果才能用上个性化定制版的幻灯呢,首先静态幻灯片代码实现以后,搭配 Advanced Custom Fields (自定义字段插件),在function中插入注册模型代码:

1:在function中注册幻灯片模型

add_action(‘init’, ‘vanfon_post_type’);
function vanfon_post_type() {
/**********幻灯片*****************/
register_post_type( ‘slider_type’,
array(
‘labels’ => array(
‘name’ => ‘首页幻灯片’,
‘singular_name’ => ‘幻灯片’,
‘add_new’ => ‘添加’,
‘add_new_item’ => ‘添加新幻灯片’,
‘edit_item’ => ‘编辑幻灯片’,
‘new_item’ => ‘新幻灯片’
),
‘public’ => true,
‘has_archive’ => false,
‘exclude_from_search’ => true,
‘menu_position’ => 5,
‘menu_icon’ => ‘dashicons-images-alt2’,
‘supports’ => array( ‘title’),
)
);
}

 

2:配置字段插件:

字段配置好以后 后台添加会出现下图图示:

然后为方便浏览,修改幻灯片列表显示,依然修改function文件

add_filter( ‘manage_edit-slider_type_columns’, ‘slider_type_custom_columns’ );
function slider_type_custom_columns( $columns ) {
$columns = array(
‘cb’ => ‘<input type=”checkbox” />’,
‘title’ => ‘幻灯片名’,
‘haslink’ => ‘链接到’,
‘thumbnail’ => ‘幻灯片预览’,
‘date’ => ‘日期’
);
return $columns;
}
add_action( ‘manage_slider_type_posts_custom_column’, ‘slider_type_manage_custom_columns’, 10, 2 );
function slider_type_manage_custom_columns( $column, $post_id ) {
global $post;
switch( $column ) {
case “haslink”:
// if(get_the_field(‘mypic’, get_the_ID())){
echo the_field(‘mylink’, get_the_ID());
// } else {echo ‘—-‘;}
break;
case “thumbnail”:
/// $slider_pic = get_post_meta($post->ID, “slider_pic”, true);
echo ‘<img src=”‘.get_field(‘mypic’, get_the_ID()).'” width=”95″ height=”41″ alt=”幻灯图片” />’;
break;
default :
break;
}
}

显示效果如下图:

最后修改模板调用即可:示例代码

<?php
$args = array(
‘post_type’=>’slider_type’,
);
query_posts($args);
$slidercount=0;
if( have_posts() ) : ?>

<?php
while( have_posts() ) : $slidercount++;the_post();
?>
<div id=”recommand_<?php echo $slidercount;?>”>
<a href=”<?php the_field(‘mylink’, get_the_ID()); ?>” id=”recm_pic_<?php echo $slidercount;?>” class=”recm_pic” style=”background:url(<?php the_field(‘mypic’, get_the_ID()); ?>)”></a>
<div class=”recm_info”>
<div style=”overflow: hidden;”>
<p class=”rec_lable”><?php the_field(‘mylabel’, get_the_ID()); ?></p>
</div>
<div>
<a href=”<?php the_field(‘mylink’, get_the_ID()); ?>” class=”rec_title”><?php the_title(); ?></a>
</div>
</div>
</div>

<?php endwhile; ?>

<?php endif; wp_reset_query(); ?>