LoginSignup
1
1

More than 1 year has passed since last update.

 phpでWordpressの動画をランダム再生する方法

Posted at

基本環境
wordpressでカスタムページを作成
ランダム表示php
動画再生htmlタグ
ランダム表示と動画再生組み合わせ
動画アップロード
カスタムページをカスタマイズ
表示確認
参考記事

基本環境

WordPressで子テーマ設定済み

wordpressでカスタムページを作成

親テーマのpage.phpをコピペしてカスタムページvideo-random.phpを作成

# cp -p /var/www/html/example.com/wp-content/themes/親テーマ/page.php /var/www/html/example.com/wp-content/themes/子テーマ/video-random.php

video-random.php最上部に以下を追記

# vim /var/www/html/example.com/wp-content/themes/子テーマ/video-random.php
<?php
/*
Template Name: video-randomページ
*/
?>

WordPressで新規固定ページを作成、テンプレートで「video-randomページ」を選択

ランダム表示php

<?php
$random = array(
    "<HTML_option1>",
    "<HTML_option1>",
    "<HTML_option1>",
);

$random = $random[mt_rand(0, count($random)-1)];
echo $random;
?>

画像表示の場合、HTMLタグをまとめるとシンプルなコードに

<?php
$random = array(
    "<画像URL_1.jpg>",
    "<画像URL_2.jpg>",
    "<画像URL_3.jpg>",
);

$random = $random[mt_rand(0, count($random)-1)];
echo '<img src="'.$random.'" alt="">';
?>

動画再生htmlタグ

<video src="動画URL_1.mp4"></video>

属性

自動再生

<video src="動画URL_1.mp4" autoplay ></video>

無音声再生

<video src="動画URL_1.mp4" muted ></video>

繰り返し再生

<video src="動画URL_1.mp4" loop ></video>

その他の属性
https://webliker.info/52510/#toc_2

ランダム表示と動画再生組み合わせ

<?php
$random = array(
    "<動画URL_1.mp4>",
    "<動画URL_2.mp4>",
    "<動画URL_3.mp4>",
);

$random = $random[mt_rand(0, count($random)-1)];
echo '<video src="'.$random.'" autoplay muted loop >';
?>

動画アップロード

WordPressメディアに動画をアップロード

カスタムページをカスタマイズ

video-random.php<main> </main>間をカスタマイズ
自動再生、無音声再生、繰り返し再生を設定

# vim /var/www/html/example.com/wp-content/themes/子テーマ/video-random.php
<main>

<?php
$random = array(
    "<動画URL_1.mp4>",
    "<動画URL_2.mp4>",
    "<動画URL_3.mp4>",
);

$random = $random[mt_rand(0, count($random)-1)];
echo '<video src="'.$random.'" autoplay muted loop >';
?>
</main>

表示確認

WordPress固定ページで表示確認


参考記事

https://webnetamemo.com/coding/php/201608313448
https://webliker.info/52510/#toc_2

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1