很多朋友在开发wordpress网站的时候总会遇到首页有slider的情况。。苦于如何后台能操作slider的图片、链接、和文字描述呢?其实很简单。。下面贴上我写的代码如下:
首先:functions里面需要加入如下的代码: register_post_type(‘slider’, array(
‘label’ => ‘slider’,
‘public’ => true,
‘show_ui’=> true,
‘capability_type’ => ‘page’,
‘hierarchical’ => false,
‘rewrite’ => array(‘slug’ => ‘slide-view’,'with_front’ => FALSE),
‘query_var’ => ‘slide’,
‘supports’ => array( ‘title’, ‘custom-fields’, ‘thumbnail’)
)
);
然后我们会在后台看到:可以在后台插入你需要的图片
前台调用代码:<?php
$posts_counter = 0;
query_posts(“post_type=slider&posts_per_page=-1&post_status=publish”);
while ( have_posts() ) : the_post();
?>
<?php
$custom = get_post_custom($post->ID);
$url = get_post_custom_values(“url”);
$title = get_post_custom_values(“value”);
$sl_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘slider-post-thumbnail’);
?>
<?php if(has_post_thumbnail($the_ID)){ ?>
<div data-caption=”#htmlCaption_<?php echo $post->ID;?>”>
<?php if(has_post_thumbnail()) { ?>
<a href=”<?php echo $url[0];?>”><?php echo the_post_thumbnail(‘slider-post-thumbnail’); ?></a>
<?php } ?>
<?php $posts_counter++;?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php wp_reset_query();?>
OK 你的slider可以显示了!