具有自定义支柱类型的支柱滑块

时间:2014-03-19 作者:user1925151

我试图创建一个帖子滑块,但我在这一点上卡住了。

我创建了一个名为“slideshow”的分类法和一个自定义的帖子类型“slider”。在这个自定义帖子类型“slider”中,我指定了一个分类“slideshow”

enter image description here

这很好,但无论我尝试了什么查询,我仍然无法获得仅为homeslide分类法指定的图像。

有没有更好的方法来创建post滑块。如有任何建议,将不胜感激。

这是我的代码:

<?php
function wms_slider_init() {
    $labels = array(
        \'name\' => \'Slider\',
        \'singular_name\' => \'Slider\',
        \'add_new\' => \'Add Slider\',
        \'add_new_item\' => \'Add New Slider\',
        \'edit_item\' => \'Edit Slider\',
        \'new_item\' => \'New Slider\',
        \'all_items\' => \'All Sliders\',
        \'view_item\' => \'View Slider\',
        \'search_items\' => \'Search Sliders\',
        \'not_found\' =>  \'No Slider found\',
        \'not_found_in_trash\' => \'No Slider found in Trash\', 
        \'parent_item_colon\' => \'\',
        \'menu_name\' => \'Sliders\'
    );

    $args = array(
        \'labels\' => $labels,
        \'description\'   => \'Holds our Slider poste specific data\',
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true, 
        \'show_in_menu\' => true, 
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'slider\' ),
        \'capability_type\' => \'post\',
        \'has_archive\' => false, 
        \'hierarchical\' => true,
        \'menu_position\' => 5,
        \'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'page-attributes\')
    ); 
    register_post_type( \'slider\', $args );
}
add_action( \'init\', \'wms_slider_init\' );
add_action( \'init\', \'wms_create_slider_taxonomies\', 0 );

//create SlideShow Category for the post type "slider"
function wms_create_slider_taxonomies() {

    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        \'name\'                => _x( \'SlideShows\', \'taxonomy general name\' ),
        \'singular_name\'       => _x( \'SlideShow\', \'taxonomy singular name\' ),
        \'search_items\'        => __( \'Search Genres\' ),
        \'all_items\'           => __( \'All SlideShows\' ),
        \'parent_item\'         => __( \'Parent SlideShow\' ),
        \'parent_item_colon\'   => __( \'Parent SlideShow:\' ),
        \'edit_item\'           => __( \'Edit SlideShow\' ), 
        \'update_item\'         => __( \'Update SlideShow\' ),
        \'add_new_item\'        => __( \'Add New SlideShow\' ),
        \'new_item_name\'       => __( \'New SlideShow Name\' ),
        \'menu_name\'           => __( \'SlideShow\' )
    );    

    $args = array(
        \'hierarchical\'        => true,
        \'labels\'              => $labels,
        \'show_ui\'             => true,
        \'show_admin_column\'   => true,
        \'query_var\'           => true,
        \'rewrite\'             => array( \'slug\' => \'slideshow\' )
    );
    register_taxonomy( \'slideshow\', array( \'slider\' ), $args );
}
?>

2 个回复
SO网友:JohnG

我没有足够的代表发表评论,但您的实际查询是什么样的?

Edit: 我还无法测试它,但我认为这会起作用

    $args = array(
        \'post_type\' => \'slider\',
        \'meta_key\' => \'_thumbnail_id\', // only pull sliders with images
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'slideshow\',
                \'field\' => \'slug\',
                \'terms\' => \'homeslide\',
                \'operator\' => \'IN\'
            )
         )
    )

$query = new WP_Query( $args);

然后遵循正常循环

SO网友:MBL

要将自定义帖子类型显示为滑块,首先需要自定义WP_Query 为您的帖子类型编制索引,然后使用一些代码将其实际显示为滑块。下面是一个使用Unslider

    <!-- Include Unslider JS -->
    <!-- YOU SHOULD INCLUDE WP JQUERY -->
    <script src="//unslider.com/unslider.min.js"></script>

    <!-- Set up the necessary JS for the slider -->
    <script type="text/javascript">
    jQuery( document ).ready(function( $ ) {
        $(\'.banner\').unslider({
            // Add other arguments here
            speed: 500
        })
    });
    </script>


    <!-- Output the HTML for the slider -->
    <?php 
    // Do the custom WP_Query first
        $query = new WP_Query( 
            array (
                \'post_type\' => \'slider\',
                \'meta_key\' => \'_thumbnail_id\', // only pull sliders with images
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'slideshow\',
                        \'field\' => \'slug\',
                        \'terms\' => \'homeslide\',
                        \'operator\' => \'IN\'
                    )
                ),
            )
        );

    // if we have posts, display them:
    if ( $query->have_posts() ) : ?> 

    <!-- Some basic CSS for the slider -->
    <!-- Remember to add your own CSS styling -->
    <style type="text/css">
    .banner { position: relative; overflow: auto; }
        .banner li { list-style: none; }
            .banner ul li { float: left; }  
    </style>

    <!-- Some basic HTML for the slider
         Each slide is simply an <li>
         within a surrounding <ul>
         inside an encasing div -->
    <div class="banner">
        <ul>

        <?php // While we have posts, output them as <li>\'s
        while ( $query->have_posts() ) : $query->the_post(); ?>
            <li style="background-image: url(<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>);">
                    <h1><?php the_title(); ?></h1>
                    <p><?php the_content(); ?></p>          
            </li>
        <?php endwhile; wp_reset_postdata(); ?>
        </ul>
    </div>

    <?php else : ?>
        <!-- ENTER HTML IF THERE\'S NO SLIDES HERE -->
    <?php endif; ?> 
记住,为此,您应该做一些事情:

确保您至少有2/3个帖子用于滑块homeslide 分类法和havefeatured imageselse 如果你决定坚持Unslider, 查看全套选项注意:我测试了此代码,方法是使用您的代码注册自定义帖子类型和分类,并在homeslide 所以应该没问题

结束

相关推荐