自定义多张图片/滑块 post [single.php]

Multiple Images/ Slider with custom post [single.php]

我正在寻找一种在 wordPress 自定义 post 类型的特色图像部分下添加多个图像选项的方法。我将 integrate/implement 这些图像放在 single.php 文件中,我想在其中显示滑块图像。我想使用 Codex / coding 方法来做到这一点,请不要使用插件。任何 Idea/help 将不胜感激。

编辑

我找到了这段代码,必须将其粘贴到function.php or plugin.php。我已经创建了一个新文件 plug-slider.php 并在那里添加了下面的代码 & 它不起作用,但问题是我必须分别调用每个图像而不是像创建一个数组并调用数组,如果没有 second/third 图像设置为其他 post 然后滑块显示为空..我不知道为什么,可能是我遗漏了什么...有人可以检查一下。

代码参考:

//init the meta box
add_action( 'after_setup_theme', 'custom_postimage_setup' );
function custom_postimage_setup(){
    add_action( 'add_meta_boxes', 'custom_postimage_meta_box' );
    add_action( 'save_post', 'custom_postimage_meta_box_save' );
}

function custom_postimage_meta_box(){

    //on which post types should the box appear?
    $post_types = array('portfolios','page');
    foreach($post_types as $pt){
        add_meta_box('custom_postimage_meta_box',__( 'More Featured Images', 'yourdomain'),'custom_postimage_meta_box_func',$pt,'side','low');
    }
}

function custom_postimage_meta_box_func($post){

    //an array with all the images (ba meta key). The same array has to be in custom_postimage_meta_box_save($post_id) as well.
    $meta_keys = array('second_featured_image','third_featured_image');

    foreach($meta_keys as $meta_key){
        $image_meta_val=get_post_meta( $post->ID, $meta_key, true);
        ?>
        <div class="custom_postimage_wrapper" id="<?php echo $meta_key; ?>_wrapper" style="margin-bottom:20px;">
            <img src="<?php echo ($image_meta_val!=''?wp_get_attachment_image_src( $image_meta_val)[0]:''); ?>" style="width:100%;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" alt="">
            <a class="addimage button" onclick="custom_postimage_add_image('<?php echo $meta_key; ?>');"><?php _e('add image','yourdomain'); ?></a><br>
            <a class="removeimage" style="color:#a00;cursor:pointer;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" onclick="custom_postimage_remove_image('<?php echo $meta_key; ?>');"><?php _e('remove image','yourdomain'); ?></a>
            <input type="hidden" name="<?php echo $meta_key; ?>" id="<?php echo $meta_key; ?>" value="<?php echo $image_meta_val; ?>" />
        </div>
    <?php } ?>
    <script>
    function custom_postimage_add_image(key){

        var $wrapper = jQuery('#'+key+'_wrapper');

        custom_postimage_uploader = wp.media.frames.file_frame = wp.media({
            title: '<?php _e('select image','yourdomain'); ?>',
            button: {
                text: '<?php _e('select image','yourdomain'); ?>'
            },
            multiple: false
        });
        custom_postimage_uploader.on('select', function() {

            var attachment = custom_postimage_uploader.state().get('selection').first().toJSON();
            var img_url = attachment['url'];
            var img_id = attachment['id'];
            $wrapper.find('input#'+key).val(img_id);
            $wrapper.find('img').attr('src',img_url);
            $wrapper.find('img').show();
            $wrapper.find('a.removeimage').show();
        });
        custom_postimage_uploader.on('open', function(){
            var selection = custom_postimage_uploader.state().get('selection');
            var selected = $wrapper.find('input#'+key).val();
            if(selected){
                selection.add(wp.media.attachment(selected));
            }
        });
        custom_postimage_uploader.open();
        return false;
    }

    function custom_postimage_remove_image(key){
        var $wrapper = jQuery('#'+key+'_wrapper');
        $wrapper.find('input#'+key).val('');
        $wrapper.find('img').hide();
        $wrapper.find('a.removeimage').hide();
        return false;
    }
    </script>
    <?php
    wp_nonce_field( 'custom_postimage_meta_box', 'custom_postimage_meta_box_nonce' );
}

function custom_postimage_meta_box_save($post_id){

    if ( ! current_user_can( 'edit_posts', $post_id ) ){ return 'not permitted'; }

    if (isset( $_POST['custom_postimage_meta_box_nonce'] ) && wp_verify_nonce($_POST['custom_postimage_meta_box_nonce'],'custom_postimage_meta_box' )){

        //same array as in custom_postimage_meta_box_func($post)
        $meta_keys = array('second_featured_image','third_featured_image');
        foreach($meta_keys as $meta_key){
            if(isset($_POST[$meta_key]) && intval($_POST[$meta_key])!=''){
                update_post_meta( $post_id, $meta_key, intval($_POST[$meta_key]));
            }else{
                update_post_meta( $post_id, $meta_key, '');
            }
        }
    }
}

My Callback code in single.php

<div class="project-carousel owl-carousel js-project-carousel">
  <?php
                   
                   while(have_posts()) {
                       the_post(); ?>
    <!-- <div class="project-detail-item">
    <?php //the_post_thumbnail('portfolio-slider') ?>
    </div> -->
    
    <?php 
    $slider_img=wp_get_attachment_image(get_post_meta(get_the_ID(),'second_featured_image', true),'full');
    if($slider_img !='')
                   {?>
                    <div class="project-detail-item">
                  <?php
          echo wp_get_attachment_image(get_post_meta(get_the_ID(),'second_featured_image', true),'full');
                   ?>
                </div>   
                  <?php }
                   else
                   {
                    the_post_thumbnail('portfolio-slider');
                   }
    ?>
    
  </div>

同样,if/else 块适用于空检查,但仅适用于单个实例,就像我必须检查每张图片...或者我搞砸了?

请进行以下更改,然后尝试...

在您的插件文件中更改此行。

$meta_keys = array('second_featured_image','third_featured_image');

$meta_keys = array('second_featured_image','third_featured_image','fourth_featured_image','fifth_featured_image');

将您的回调 single.php 文件代码更改为如下所示。遍历每个文件并检查是否为空。 我修改了代码以接受最多 5/6 个图像,因此您可以在滑块中使用它。

<div class="project-carousel owl-carousel js-project-carousel">
  <?php
                   
                   while(have_posts()) {
                       the_post();
    $slider_img=the_post_thumbnail('portfolio-slider');
    $slider_img1=wp_get_attachment_image(get_post_meta(get_the_ID(),'second_featured_image', true),'full');
    $slider_img2=wp_get_attachment_image(get_post_meta(get_the_ID(),'third_featured_image', true),'full');
    $slider_img3=wp_get_attachment_image(get_post_meta(get_the_ID(),'fourth_featured_image', true),'full');
    $slider_img4=wp_get_attachment_image(get_post_meta(get_the_ID(),'fifth_featured_image', true),'full');
    $slider_img5=wp_get_attachment_image(get_post_meta(get_the_ID(),'six_featured_image', true),'full');
    $slider_img6=wp_get_attachment_image(get_post_meta(get_the_ID(),'svn_featured_image', true),'full');
    if($slider_img !='')
                   {?>
                    <div class="project-detail-item">
                  <?php echo $slider_img;?>
                   </div> 
                   <?php }?>
                   <?php if($slider_img1 !=''){ ?>
                    <div class="project-detail-item">
               <?php echo $slider_img1; ?>
                </div>  
                <?php }?> 
                <?php if($slider_img2 !=''){ ?>
                    <div class="project-detail-item">
               <?php echo $slider_img2; ?>
                </div>  
                <?php }?> 
                <!--image-->
                <?php if($slider_img3 !=''){ ?>
                    <div class="project-detail-item">
               <?php echo $slider_img3; ?>
                </div>  
                <?php }?> 

                <!--image end-->
                <!--image-->
                <?php if($slider_img4 !=''){ ?>
                    <div class="project-detail-item">
               <?php echo $slider_img4; ?>
                </div>  
                <?php }?> 
                <!--image end-->
                <!--image-->
                <?php if($slider_img5 !=''){ ?>
                    <div class="project-detail-item">
               <?php echo $slider_img5; ?>
                </div>  
                <?php }?> 
                <!--image end-->
                <!--image-->
                <?php if($slider_img6 !=''){ ?>
                    <div class="project-detail-item">
               <?php echo $slider_img6; ?>
                </div>  
                <?php }?> 
                <!--image end-->
                
    
  </div>