Skip to content

Instantly share code, notes, and snippets.

View kaoru-fukusato's full-sized avatar

かおちんサポート・メモ帳 kaoru-fukusato

View GitHub Profile
@kaoru-fukusato
kaoru-fukusato / Facebookページ
Created July 25, 2021 22:21
Facebookページとアプリを紐付け
https://www.facebook.com/dialog/pagetab?app_id=(App IDの入力)&display=popup&next=(Secure Page Tab URLの入力)
を直打ちでアドレスバーに入力し、アクセスします。
@kaoru-fukusato
kaoru-fukusato / 動画背景 全画面表示 スマホ対応
Last active April 19, 2020 22:14
動画背景 全画面表示 スマホ対応
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta charset="UTF-8">
<title>あれこれ</title>
<meta name="keywords" content="あれこれ" />
<meta name="description" content="あれこれ" />
<script src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
<script src="main.js"></script>
@kaoru-fukusato
kaoru-fukusato / Advanced Random Posts動作しない時の対処法
Last active March 16, 2020 04:08
Advanced Random Posts Widgetが動作しない時の対処法
function shortcode_random_posts( $param ) {
return random_posts($param[0] , $param[1]); //0:カテゴリーID(0はカテゴリ指定なし) 1:表示記事数
}
add_shortcode('random_posts','shortcode_random_posts');
動作方法(ショートコードの書き方)
表示したい箇所(本記事のように投稿内)にショートコードを下記のように記載します。
[random_posts カテゴリID 個数]
このように記載すると指定カテゴリIDの記事を指定個数分、リンク付きで、アイキャッチ画像とタイトルを表示します。
※カテゴリID=0を指定するとすべての投稿を対象とします。
プラグイン実装(ソースをほぼ加工なしで表示しています)
・class指定やCSS定義などは省略しています。
ショートコード実装
@kaoru-fukusato
kaoru-fukusato / index.phpでランダム表示
Created March 15, 2020 13:47
index.phpでランダム表示
//私の現在の作業コードの場合
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
/////////////////////////
//テーマのfunctions.phpファイルに次のコードを配置します。
function one_random_post_on_home_page( $query )
{
if ( ! ( $query->is_home() && $query->is_main_query() ) )
@kaoru-fukusato
kaoru-fukusato / ランダム表示
Created March 15, 2020 10:56
taxonomy.phpで一覧をランダム表示
<?php query_posts($query_string . "&orderby=rand"); ?>
@kaoru-fukusato
kaoru-fukusato / functions.php
Created March 15, 2020 10:00
記事ランダムをショートコードとして使う場合
[random_posts limit=5]
<?php
function random_posts($attrs = array()){
ob_start();
$limit = !empty($attrs['limit']) ? $attrs['limit'] : 0;
?>
<?php
$args = array(
'post_type' => 'post',
<?php query_posts(array('orderby' => 'rand', 'showposts' => 5));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
/* 繰り返し表示させる部分 */
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
<?php endwhile; endif; ?>
@kaoru-fukusato
kaoru-fukusato / functions.php記事ランダム②
Created March 15, 2020 09:57
記事ランダム②functions.php
//記事ランダム
session_start();
add_filter('posts_orderby', 'edit_posts_orderby');
function edit_posts_orderby($orderby_statement) {
$seed = $_SESSION['seed'];
if (empty($seed)) {
@kaoru-fukusato
kaoru-fukusato / 記事ランダム①
Created March 15, 2020 09:56
記事ランダム①
function category_rand_orderby( $orderby, $query ){
if ( !is_admin() && $query->is_main_query() ) {
if ( $query->is_category() ) {
$seed = strtotime( date( 'Y-m-d H:00:00' ) );
mt_srand( $seed );
$orderby = 'RAND(' . mt_rand() . ')';
}
}
remove_filter( current_filter(), __FUNCTION__ );
return $orderby;