Created
March 3, 2023 05:52
-
-
Save mono96/385455194145e05b33f88691616b00ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Cat Slider mono96 | |
Description: 同じカテゴリー記事のカルーセルスライダー [cat_slider] | |
Version: 1.0 | |
*/ | |
/* 参考 https://byacco.work/wordpress-post-slider-no_plugin/ */ | |
if ( ! defined( 'ABSPATH' ) ) exit; | |
define( 'MY_CAT_PLUGIN_URL', plugins_url( '/', __FILE__ ) ); | |
// ヘッダーに追加するコード | |
function add_cat_slider_files() { | |
//スタイルシートの読み込み | |
if ( ! wp_style_is( 'swiper-style', 'enqueued' ) ) { | |
wp_enqueue_style( 'swiper-style', 'https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.css'); | |
} | |
wp_enqueue_style( 'swiper-cat-mono96-style', MY_CAT_PLUGIN_URL .'css/swiper-cat-style.css'); | |
//JavaScript の読み込み | |
if ( ! wp_style_is( 'swiper-js', 'enqueued' ) ) { | |
wp_enqueue_script( 'swiper-js', 'https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.js', '', '', true); | |
} | |
if ( ! wp_style_is( 'swiper-setting-js', 'enqueued' ) ) { | |
wp_enqueue_script( 'swiper-setting-js', MY_CAT_PLUGIN_URL . 'js/swiper_setting.js', '', '', true); | |
} | |
} | |
add_action('wp_enqueue_scripts', 'add_cat_slider_files'); | |
// カルーセルスライダー ショートコード | |
function mono96_cat_shortcode_slider() { | |
global $post; | |
//投稿かどうか判定する | |
/* | |
if ( ! is_single() ){ | |
return ; | |
} | |
*/ | |
//カテゴリースラッグ取得 | |
$categories = get_the_category(); | |
if ( ! empty( $categories ) ) { | |
$category_slug = $categories[0]->slug; | |
} | |
ob_start(); | |
echo '<div class="swiper categorie_swiper">'; | |
echo '<div class="swiper-wrapper">'; | |
$loop = new WP_Query(array( | |
'orderby' => 'rand', | |
'post_type' => 'post', // ポストタイプを設定 デフォルト投稿はそのまま | |
'posts_per_page' => 10, // 記事数を設定 | |
'category_name' => $category_slug | |
)); | |
/* Start the Loop */ | |
while ($loop->have_posts()) : | |
$loop->the_post(); | |
echo '<div class="swiper-slide">'; | |
echo '<div class="swiper-slide__inner">'; | |
echo '<div class="swiper-slide__inner--item">'; | |
if (has_post_thumbnail()) { | |
echo '<figure class="post__thumb--img">'."\n"; | |
echo ' <!-- アイキャッチ画像 有り -->'."\n"; | |
echo '<a href="'; | |
echo the_permalink(); | |
echo '" style="background-image: url(\''; | |
echo the_post_thumbnail_url('large'); | |
echo '\')"></a>'."\n"; | |
}else{ | |
echo '<figure class="post__thumb--img">'."\n"; | |
echo ' <!-- アイキャッチ画像がない場合 -->'."\n"; | |
echo '<a href="'; | |
echo the_permalink(); | |
echo '" style="background-image: url(\''; | |
echo MY_CAT_PLUGIN_URL .'img/no-image.jpg'; | |
echo '\')"></a>'."\n"; | |
echo '</figure>'."\n"; | |
} | |
echo '<div class="text-block">'; | |
echo '<div class="meta-block">'; | |
echo '<span class="date">' . the_time('Y.m.d') . '</span>'; | |
echo '</div>'; | |
echo '<a href="'; | |
echo the_permalink() ; | |
echo '" rel="bookmark" title="'; | |
echo $post->post_title; | |
echo '">'; | |
echo$post->post_title; | |
echo '</a>'; | |
echo '</div>'; | |
echo '</div>'; | |
echo '</div>'; | |
echo '</div>'; | |
endwhile; | |
wp_reset_query(); | |
echo '</div>'; | |
echo '<!-- If we need pagination -->'; | |
echo '<div class="swiper-pagination"></div>'; | |
echo '<!-- Add Arrows -->'; | |
echo '<div class="swiper-button-prev swiper-button-white"></div>'; | |
echo '<div class="swiper-button-next swiper-button-white"></div>'; | |
echo ' <!-- If we need scrollbar -->'; | |
echo '<div class="swiper-scrollbar"></div>'; | |
echo '</div>'; | |
return ob_get_clean(); | |
} | |
add_shortcode( 'cat_slider', 'mono96_cat_shortcode_slider' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment