WordPress 4.4からのレスポンシブイメージ機能を無効化する

Filed under: functions.php,wordpress — kdcs @ 2018年1月10日 水曜日

WordPress 4.4から実装された機能でimgタグに「srcset属性・sizes属性」が付く

この機能は投稿の編集画面には出ないのでテキストエディタ上には表示されない。
公開したページが表示されるタイミングで自動的に付加される仕様

この機能を無効化するにはfunction.phpに以下を記述

// レスポンシブイメージ機能の無効化(WordPress4.4以降)-----------------------------------
add_filter( 'wp_calculate_image_srcset', '__return_false' );

WordPress カテゴリーの選択をチェックボックスからラジオボタンに変更する

Filed under: functions.php,wordpress — kdcs @ 2018年1月9日 火曜日

WordPress カテゴリーの選択をチェックボックスからラジオボタンに変更すると同時に
「新規カテゴリーを追加」と「よく使うもの」を非表示にする

function.php内に記述 例:カスタム投稿タイプは「information」タクソノミーは「information_category」
以下はデフォルト投稿とカスタム投稿を併用する場合。

// お知らせカテゴリーの選択をラジオボタンに変更-------------------------------------------
function change_category_to_radio() {
echo "<script>
jQuery(function($) {
    // カテゴリーをラジオボタンに変更
    $('#categorychecklist input[type=checkbox]').each(function() {
        $(this).replaceWith($(this).clone().attr('type', 'radio'));
    });
    $('#information_categorychecklist input[type=checkbox]').each(function() {
        $(this).replaceWith($(this).clone().attr('type', 'radio'));
    });
    // 「新規カテゴリーを追加」を非表示
    $('#category-adder').hide();
    $('#information_category-adder').hide();
    // 「よく使うもの」を非表示
    $('#category-tabs').hide();
    $('#information_category-tabs').hide();
});
</script>"; }

add_action( 'admin_head-post-new.php', 'change_category_to_radio' );
add_action( 'admin_head-post.php', 'change_category_to_radio' );

WordPress 投稿の必須入力アラート

Filed under: functions.php,wordpress — kdcs @ 2018年1月9日 火曜日

デフォルトでは未入力でも下書き保存・公開ができてしまうので
必須入力の項目を指定し、未入力の場合はアラートを出す
function.php内に記述

デフォルトの投稿

function post_edit_required() {
?>
<script>
jQuery(function($) {
  if( 'post' == $('#post_type').val() ) {
    $('#post').submit(function(e) {
      // タイトル
      if ( '' == $('#title').val() ) {
        alert('タイトルを入力してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#title').focus();
        return false;
      }
      // コンテンツ(エディタ)
      if ( $('.wp-editor-area').val().length < 1 ) {
        alert('コンテンツを入力してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        return false;
      }
      // 抜粋
      if ( '' == $('#excerpt').val() ) {
        alert('抜粋を入力してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#excerpt').focus();
        return false;
      }
      // カテゴリー
      if ( $('#taxonomy-category input:checked').length < 1 ) {
        alert('カテゴリーを選択してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#taxonomy-category a[href="#category-all"]').focus();
        return false;
      }
      // タグ
      if ( $('#tagsdiv-post_tag .tagchecklist span').length < 1 ) {
        alert('タグを選択してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#new-tag-post_tag').focus();
        return false;
      }
      // アイキャッチ
      if ( $('#set-post-thumbnail img').length < 1 ) {
        alert('アイキャッチ画像を設定してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#set-post-thumbnail').focus();
        return false;
      }
    });
  }
});
</script>
<?php
}
add_action( 'admin_head-post-new.php', 'post_edit_required' );
add_action( 'admin_head-post.php', 'post_edit_required' );

(続きを読む…)

WordPress カスタム投稿タイプの月別アーカイブ・カテゴリーアーカイブ

Filed under: wordpress — kdcs @ 2018年1月8日 月曜日

WordPress カスタム投稿タイプの月別アーカイブ

アーカイブページ

<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'> 
  <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> 
  <?php wp_get_archives('post_type=カスタム投稿名&type=monthly&format=option&show_post_count=1'); ?>
</select>

カテゴリーページ(未検証)

<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'> 
  <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> 
  <?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?>
</select>

WordPress カスタム投稿でカテゴリー名取得とcssクラスにスラッグ付ける

Filed under: wordpress — kdcs @ 2017年12月29日 金曜日

カスタム投稿タイプの記事でカテゴリー(ターム)名を取得
さらに、ccsのクラスにスラッグ名を付けて色分けする

タクソノミー名は’information_category’

<?php
    if ($terms = get_the_terms($post->ID, 'information_category')) {
        foreach ( $terms as $term ) {
            $term_slug = $term -> slug;
            echo ('<span class="') ;
            echo esc_html($term_slug) ;
            echo ('">') ;
            echo esc_html($term->name)  ;
            echo ('</span>') ;
        }
    }
?>

出力はこちら

<span class="スラッグ名">ターム名</span>

サイト内検索

カテゴリー

最近の投稿

↑上に戻る