这是一项有趣的任务。我们可以在仪表板中创建一个自定义的滑块菜单,然后可以从那里上传特色图像,以便可以在滑块中动态调用这些图像。
First 我们必须Theme Support and register Post Type 在函数中。php
add_theme_support( \'post-thumbnails\', array( \'post\', \'slider\' ) );
add_image_size( \'slider-image\', 1024, 550, true );
function create_post_type() {
register_post_type( \'slider\',
array(
\'labels\' => array(
\'name\' => __( \'Slides\' ),
\'singular_name\' => __( \'Slide\' ),
\'add_new\' => __( \'Add New\' ),
\'add_new_item\' => __( \'Add New Slide\' ),
\'edit_item\' => __( \'Edit Slide\' ),
\'new_item\' => __( \'New Slide\' ),
\'view_item\' => __( \'View Slide\' ),
\'not_found\' => __( \'Sorry, we couldn\\\'t find the Slide you are looking for.\' )
),
\'public\' => true,
\'publicly_queryable\' => false,
\'exclude_from_search\' => true,
\'menu_position\' => 14,
\'has_archive\' => false,
\'hierarchical\' => false,
\'capability_type\' => \'page\',
\'rewrite\' => array( \'slug\' => \'Slide\' ),
\'supports\' => array( \'title\', \'thumbnail\' )
)
);
}
add_action( \'init\', \'create_post_type\' );
现在,在运行滑块图像的文件中,我们需要进行循环,并在循环内演示查询,以从数据库中获取图像并调用查询结果。
<?php
if(!is_paged())
{
$args = array(\'post_type\' => \'slider\', \'posts_per_page\' => 4);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() )
{
while ( $the_query->have_posts() )
{
$the_query->the_post();
$image = wp_get_attachment_url( get_post_thumbnail_id($post->ID), \'slider-image\' );
?>
<div data-src="<?php echo $image; ?>"> </div>
<?php }
}
}
?>
就这样,你完了!现在,从幻灯片菜单上载的图像将显示在滑块中。