php

2024年11月26日

php日付時間曜日の操作 date

今日

$hoy = date('Y-m-d');

明日

$manana = date('Y-m-d', strtotime('+1 day'));

今月末日

$finaldeestemes = date('Y-m-t');

2つの日付の日数差

$time1 = strtotime('2019-04-01');
$time2 = strtotime('2018-04-01');
$differece = ($time1 - $time2) / (60 * 60 * 24);

日付を日本語で表示

$fecha = '1999-10-10';
$dia = date("m月d日", strtotime($fecha));

曜日を日本語で表示

$fecha = '1999-10-10';
$week = array( "日", "月", "火", "水", "木", "金", "土" );
$diadesemana = $week[date("w", strtotime($fecha))];

日付条件分岐

wordpressの場合はcurrent_timeを使う
そうでない場合はdateを使う
if (current_time('Y-m-d H:i:s') < '2024-11-29 17:00:00') {
//その日以降非表示
} else{
//その日以降表示
}                                 

SQL

今日以降

 `date` >= "'.$hoy.'"

昨日以前

`date` < "'.$hoy.'"
2022年6月7日

ループ内で連想配列を作る make associative array in the loop

ループ内でオリジナルの情報を連想配列に格納する
$id_date_array = array();
if ( have_posts() ): while ( have_posts() ) : the_post(); 
  $id_date_array[] = array(
        'id' => $get_the_ID(),
        'date' => $date,
  );
endwhile;endif;
var_dump($id_date_array);の結果

array(2) {
 [0]=> array(2) {
  ["id"]=> int(399)
  ["date"]=> string(8) "20220804"
 }
 [1]=> array(2) {
  ["id"]=> int(64)
  ["date"]=> string(8) "20220714"
 }date
}

2021年1月26日

wp_get_navを独自HTMLで出力してナビゲーションメニューをカスタマイズ

ナビゲーションメニューのIDに基づいて指定のナビゲーションメニューを取得

php

$menu_items = wp_get_nav_menu_items(21);//ナビゲーションメニューのID
foreach ( (array) $menu_items_news as $key => $menu_item ) {
   $title = $menu_item->title;
   $url = $menu_item->url;
   echo '<li><a href="' . $url . '">' . $title . '</a></li>';
}

正規表現でURL置換

URLのドメイン以下の文字列のみを取得(カテゴリーページで)

前:https://domain.com/news/

$wourl = str_replace("https://domain.com/","",$url);
$cate_slug = str_replace("/","",$wourl);

後:news

2020年7月16日

wordpress/php さまざまなエラーと対処

php

getimagesize で429 エラー

getimagesizeで429 Too Many Requests エラーが出るときの対処

悪い例

httpから指定する

$imagesize = getimagesize('https://domain.com/images/sample.jpg');

良い例

サーバーのルートから指定する
$imagesize = getimagesize('var/www/domain.com/public_html/images/sample.png');

URL文字列をサーバールートからパス置き換える

$eyecatchimgには画像URL
$root_path = $_SERVER['DOCUMENT_ROOT'];
$pattern = '/https:\/\/domain.com/';
$root_img = preg_replace($pattern , $root_path , $eyecatchimg);

$imagesize = getimagesize($root_img);

wordpress

記事内の画像をアイキャッチとして使うコード【エラー対策済み】

ネットに出回るコードをそのまま使うと出るエラーメッセージ

Notice: Undefined offset: 0 in [URL]functions.php on line

functions.php

function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    if (preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)){
        $first_img = $matches [1] [0];
    }else{
        $first_img = get_stylesheet_directory_uri().'/images/noimage.jpg';
    }
return $first_img;
}

Wordpressの外でデータを読み込むとnetwork statusが404になる

404がでる悪い例

require_once ('../wp-blog-header.php');

良い例

require_once ('../wp-load.php');
2020年5月17日

css 編集時のブラウザキャッシュ対策

CSSファイルが更新されたらユーザーに新たなCSSを読み込ませるブラウザキャッシュ対策 (WPでドメイン直下のCSSを読み込んでる場合もこれ)
<?php date_default_timezone_set('Asia/Tokyo'); ?>
<link href="style.css?<?php echo date( "Y-m-d-H-i-s", filemtime('./style.css')); ?>" rel="stylesheet"  type="text/css" />

ルートから指定する場合

<?php $path = $_SERVER['DOCUMENT_ROOT']; ?>
<link href="/style.css?<?php echo date( "Y-m-d-H-i-s", filemtime($path.'/style.css')); ?>" rel="stylesheet"  type="text/css" />

Wordpressの場合

function.php

functionからテーマcssを出力する場合
function theme_enqueue_styles() {
   wp_enqueue_style( 'style', get_stylesheet_directory_uri() . '/style.css', array(), date("Y-m-d-H-i-s", filemtime( get_stylesheet_directory().'/style.css')) );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );

header.php

headerに直接書く場合
<link href="<?php echo get_template_directory_uri().'/style.css?'; echo date( "Y-m-d-H-i-s", filemtime(get_template_directory().'/style.css')); ?>" rel="stylesheet">