metaプロパティの出力(カスタム投稿タイプ対応)

Filed under: 未分類 — kdcs @ 25年12月24日 水曜日

カスタム投稿タイプの「meta property=”og:description”」などのmetaプロパティをシングルページで出力させる方法。

まずは、デフォルトの投稿タイプ(post)を「&&is_singular(‘post’)」で設定する必要がある

if (is_single()&&is_singular('post')){
if(have_posts()): while(have_posts()): the_post();
echo '<meta property="og:title" content="'; the_title(); echo '">';echo "\n";
echo '<meta property="og:description" content="'.mb_substr(get_the_excerpt(), 0, 120).'">';echo "\n";
echo '<meta property="og:url" content="'; the_permalink(); echo '">';echo "\n";
echo '<meta property="og:type" content="article">';echo "\n";
endwhile; endif;
}

続けて、カスタム投稿タイプ
※descriptionを投稿内の文章から取得する場合はデフォルトと同じです。
例:カスタム投稿タイプが「catering_plan」で、descriptionをカスタムフィールド「delivery_note」から取得し、100文字以内で区切りのいいところでカットするという方法。(区切りとは「、」や「。」の句読点)

if (is_single() && is_singular('catering_plan')) {
    if (have_posts()): while (have_posts()): the_post();

        // 元のテキスト取得
        $text_items = get_field('delivery_note');

        // 改行削除 → タグ削除 → 連続スペース削除
        $text_items = wp_strip_all_tags($text_items);
        $text_items = preg_replace('/\s+/', ' ', $text_items);
        $text_items = trim($text_items);

        // 100文字で一旦切る
        $short = mb_substr($text_items, 0, 100);

        // 最後の句読点以降を削除して自然な文章にする
        $description = preg_replace('/[^、。..!?!?]*$/u', '', $short);

        // もし句読点が見つからず空になった場合は100文字そのまま
        if ($description === '') {
            $description = $short;
        }

        echo '<meta property="og:title" content="'; the_title(); echo '">' . "\n";
        echo '<meta property="og:description" content="' . esc_attr($description) . '">' . "\n";
        echo '<meta property="og:url" content="'; the_permalink(); echo '">' . "\n";
        echo '<meta property="og:type" content="article">' . "\n";

    endwhile; endif;
}

favicon(ファビコン)やスマホなどのお気に入りアイコン

Filed under: 未分類 — kdcs @ 25年12月15日 月曜日

推奨記述例

<link rel="icon" href="/favicon.png" type="image/png" sizes="32x32">
<link rel="icon" href="/favicon.ico" type="image/x-icon">

.png は高解像度対応に便利。
.ico は古い環境や一部の場面でまだ使われることがあるので、両方用意すると安心。

従来の「shortcut icon」は Internet Explorer 独自の拡張で、標準仕様ではない。
現在の主要ブラウザ(Chrome, Firefox, Safari, Edge)はすべて 「icon」を解釈。
W3C仕様でも正式に定義されているのは「icon」。

WordPress カテゴリーとタグの選択にかんするカスタマイズ

Filed under: 未分類 — kdcs @ 25年8月7日 木曜日

カテゴリーとタグに表示されているタブ「よく使いもの」を非表示にする。

functions.phpに記述

function my_admin_style() {
echo '<style>
div.categorydiv li.hide-if-no-js{
display:none;
}
</style>'.PHP_EOL;
}
add_action('admin_print_styles', 'my_admin_style');

タグをチェックボックス化する

functions.phpに記述

'hierarchical'            => true,    //trueでタグをチェックボックス化

チェックボックスで1つしか選択できないようにする
※以下の記述はカスタム投稿タイプ「restaurant」の場合でカテゴリーとタグそれぞれ制御させている

functions.php

// カテゴリー・タグ(チェックボックス化)を1つだけ選択可能にする -------------------------
function limit_restaurant_taxonomy_selection() {
    echo '<script>
        jQuery(function($) {
            // restaurant_category の制御
            $("#restaurant_categorychecklist input[type=checkbox]").click(function() {
                $("#restaurant_categorychecklist input[type=checkbox]").not(this).prop("checked", false);
            });

            // restaurant_tag の制御
            $("#restaurant_tagchecklist input[type=checkbox]").click(function() {
                $("#restaurant_tagchecklist input[type=checkbox]").not(this).prop("checked", false);
            });
        });
    </script>';
}
add_action('admin_print_footer_scripts', 'limit_restaurant_taxonomy_selection');

WordPress内jQueryのバージョン情報

Filed under: JavaScript,wordpress,未分類 — kdcs @ 24年9月17日 火曜日

WordPressとjQueryのバージョン
※基本的にテーマをカスタマイズしているのでWordPress内部のjQueryは使用しない。

jQueryは公式からダウンロードするかcdnjsやgoogleなどから取得する
【cdnjs】
jQueryはこちら
jQuery-migrateはこちら

WordPress バージョン jQuery バージョン migrate リリース日
6.4 3.7.1 3.4.1 2023/11/07
6.3 3.7.0 未調査 2023/08/08
6.2 3.6.4 未調査 2023/03/29
6.1 3.6.1 未調査 2022/11/01
5.8 3.6.0 未調査 2021/07/20
5.6 3.5.1 未調査 2020/12/08
4.5 1.12.4 未調査 2016/04/12
4.3 1.11.3 未調査 2015/08/18
4.2 1.11.2 未調査 2015/08/23
4.0 1.11.1 未調査 2014/09/04
3.9 1.11.0 未調査 2014/04/16

WordPress pdfファイルのリンクを別ウインドで開く

Filed under: JavaScript,wordpress,未分類 — kdcs @ 22年3月28日 月曜日

WordPressで投稿にpdfファイルをアップロードしてリンクを貼る際に
別ウインドで開くようにするために「target=”blank”」を付ける方法。

但し、セキュリティーの観点から「rel=”noopener nofollow”」を付けることが推奨される。

jQueryの記述

$(document).ready(function () {
	// PDFファイルリンクに target="_blank"を付ける
	$("a[href*='.pdf']").attr('target','_blank');
	$("a[href*='.pdf']").attr('rel','noopener nofollow');
});

サイト内検索

カテゴリー

最近の投稿

↑上に戻る