WordPress - yagisawatakuya/Wiki GitHub Wiki

デバッグ

define('WP_DEBUG', false);

記事のタイトルを出力する

<?php the_title(); ?>           //htmlタグを含む
<?php the_title_attribute(); ?> //htmlタグを含まない

スラッグ取得

$post->post_name

ショートコード

function getTest() {
  $post_num = get_field('test');
  return $post_num_now;
}
add_shortcode('viewTest', 'getTest');

カスタム投稿のアーカイブかどうか確認

is_post_type_archive()

カテゴリー子カテゴリー取得サンプル

foreach(get_the_terms(get_the_ID(),'○○○') as $cat){
  if(count(get_the_terms(get_the_ID(),'○○○')) != 1){
    echo $cat->name;
  }
}

「前の記事」「次の記事」特定カテゴリー除外

// カテゴリーのID指定
$exclusion = '32, 33, 35, 36, 37, 38, 41, 43, 44, 45, 46, 47, 48, 143, 154, 155, 158';
previous_post_link( '%link', _x('<span></span> 前へ', 'Previous post link'), false, $exclusion);
next_post_link( '%link', _x('<span></span>次へ', 'Next post link'), false, $exclusion);

phpファイルのロードする

// テーマフォルダ内の「content-product.php」をロードする
// 引数は渡せない
get_template_part( 'content', 'product' );
// 引数を渡す方法
include locate_template( 'xxxx.php' );
include get_template_directory() . '/xxxx.php';
include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/xxxx/exxxx.php';

ユーザーエージェント切り替え
参考:http://millkeyweb.com/if-mobile/

<?php if ( wp_is_mobile() ) : ?>
  // モバイル用コンテンツ
<?php else: ?>
  // PC用コンテンツ
<?php endif; ?>

管理バー非表示

add_filter( 'show_admin_bar', '__return_false' );

バージョンアップ表示を非表示に

add_filter('pre_site_transient_update_core', '__return_zero');
remove_action('wp_version_check', 'wp_version_check');
remove_action('admin_init', '_maybe_update_core');

アップロードされた画像をリサイズ

add_image_size('img',750, 450, true);

WordPressのURLの末尾にスラッシュを付ける

function add_slash_uri_end($uri, $type) {
if ($type != 'single') {
$uri = trailingslashit($uri);
}
return $uri;
}
add_filter('user_trailingslashit', 'add_slash_uri_end', 10, 2);

先頭表示を使用した場合の表示件数のズレ対応(無限スクロール)

<?php
  $page_list = 10; //1ページあたりの表示件数

  // 先頭固定表示の投稿を表示があった場合の表示数変更
  // 1ページ目だった場合
  if(is_home() && !is_paged()):
    $sticky = get_option( 'sticky_posts' );
    if ( isset($sticky[0]) ):
      $list_cnt = $page_list - 1; // 先頭固定表示がある場合
    else:
      $list_cnt = $page_list; // 先頭固定表示がない場合
    endif;
  // 2ページ以降の場合
  else :
    $list_cnt = $page_list;
  endif;

  $paged = (int) get_query_var('paged');
  $args = array(
    'posts_per_page' => $list_cnt,
    'paged' => $paged,
    'orderby' => 'post_date',
    'post_type' => 'カスタム投稿',
    'post_status' => 'publish'
  );
  $the_query = new WP_Query($args);

  if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();

      echo the_title();

    endwhile;
  endif;
  wp_reset_postdata();

?>

アイキャッチ画像
function.php

//アイキャッチ画像を有効化
add_theme_support('post-thumbnails');

//サイズを指定して切り抜きをする(縦:100px 横100px)
set_post_thumbnail_size(100, 100, true);

//サイズを指定してリサイズさせる(縦:100px 横100px)
set_post_thumbnail_size(100, 100);
function get_thumb_img($size = 'full') {
  
  $thumb_id = get_post_thumbnail_id();                         // アイキャッチ画像のIDを取得
  
  $thumb_img = wp_get_attachment_image_src($thumb_id, $size);  // $sizeサイズの画像内容を取得
  
  $thumb_src = $thumb_img[0];    // 画像のurlだけ取得
  
  $thumb_alt = get_the_title();  //alt属性に入れるもの(記事のタイトルにしています)

  return '<img src="'.$thumb_src.'" alt="'.$thumb_alt.'">';
}

https://wemo.tech/162

WP 自動更新の有効化・無効化

functions.php
// 有効化
add_filter('automatic_updater_disabled', '__return_true');
// 無効化
add_filter('automatic_updater_disabled', '__return_false');

https://www.nxworld.net/wordpress/wp-automatic-background-updates.html

自動更新の有無確認
Background Update Tester
https://ja.wordpress.org/plugins/background-update-tester/

wp_enqueue_style()を使った外部ファイルの管理

wp_enqueue_style( 'theme-style', get_stylesheet_uri(), false, date('YmdHis', filemtime(get_template_directory() . '/style.css')) );

http://www.02320.net/tech/clear-cache-css-add-date/
http://dim5.net/wordpress/stylesheet-js-browser-cache.html

get_permalinkで取得したURLを相対パスに変換

function page_relative_path () {
        $home_url 	= get_home_url();
        $permalink 	= get_permalink($value);
        $page_url 	= str_replace( $home_url, "/lab", $permalink);
        return $page_url;
}
⚠️ **GitHub.com Fallback** ⚠️