goldis
10/28/2019 - 12:19 PM

Slider-wp

Код для functions.php темы : 

function create_slides() {
 register_post_type('slides', array(
  'labels' => array(
   'name'            => __( 'Slides' ),
   'singular_name'   => __( 'Slide' ),
   'add_new'         => __( 'Add Slide' ),
   'add_new_item'    => __( 'Add Slide item' ),
   'edit'            => __( 'Edit Slide' ),
   'edit_item'       => __( 'Edit Slide item' ),
   'new_item'        => __( 'Single Slide' ),
   'all_items'       => __( 'All Slide' ),
   'view'            => __( 'View Slide' ),
   'view_item'       => __( 'View single Slide' ),
   'search_items'    => __( 'Search Slide' ),
   'not_found'       => __( 'Slide not found' ),
  ),
  'public' => true,
  'menu_position' => 5,
  'supports' => array( 'title', 'thumbnail', 'excerpt', 'custom-fields'),
  'taxonomies' => array( '' ),
  'has_archive' => true,
  'capability_type' => 'post',
  'menu_icon'   => 'dashicons-admin-site',
  'rewrite' => array('slug' => 'slides'),
 ));
}
add_action( 'init', 'create_slides' );




Код для page-home-page.php(нужно заменить div карусели) :

<div id="myCarousel" class="carousel slide" data-ride="carousel">
                <div class="carousel-inner" role="listbox">
                    <?php
                    $temp = $wp_query;
                    global $status;
                    $wp_query = null;
                    $wp_query = new WP_Query();
                    $wp_query->query(array( 'post_type' => 'slides','order'     => 'ASC', ) );
                    while ($wp_query->have_posts()) : $wp_query->the_post();
                    ?>
                    <div class="item<? if($status!=1){echo' active';$status=1;}?>">
                        <div class="col-lg-6 col-md-6 col-sm-6">
                            <? the_field('left-part')?>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-6">
                            <img src="<?= get_field('right-photo')['url']?>" alt="slide">
                        </div>

                    </div>
                    <?php endwhile;
                    $wp_query = null;
                    $wp_query = $temp;
                    ?>
                    <?php wp_reset_query(); ?>
                </div>
                    <ol class="carousel-indicators">
                        <?php
                        $temp = $wp_query;
                        global $i;
                        $i = 0;
                        $wp_query = null;
                        $wp_query = new WP_Query();
                        $wp_query->query(array( 'post_type' => 'slides' ) );
                        while ($wp_query->have_posts()) : $wp_query->the_post();
                            ?>
                        <li data-target="#myCarousel" class="<? if($i==0) echo ' active'?>" data-slide-to="<?=$i++?>" ></li>
                        <?php endwhile;
                        $wp_query = null;
                        $wp_query = $temp;
                        ?>
                        <?php wp_reset_query(); ?>
                    </ol>
</div> 



Так же нужно создать custom field group с такими настройками(название не важно) : 

*******************************************
Field Label Field Name Field Type

left-part       left-part Text Area

right-photo     right-photo Image

-------------------------------------------

Rules :

Post type     is equal to       slides

*******************************************


Теперь можно создавать слайды из админ панели.