Web制作

【備忘録・メモ】個人的にWordPressでよく使うけど忘れがちな項目まとめ【コピペ】

Written by o-saka

すんません今回もWordPress関連の記事です。コピペして使ってください。

カテゴリー・タクソノミー関連

特定のカスタムタクソノミーのターム一覧表示

※登録記事がないものも含む、'hide_empty' => false,がその部分、親タームのみ、'parent' => 0,がその部分

<?php
$taxonomy_name = '【タクソノミー名】';
$args = array(
'hide_empty' => false,
'parent' => 0,
);
$terms = get_terms( $taxonomy_name, $args );
foreach ( $terms as $term ) {
?>
<a href="<?php echo get_term_link($term); ?>"><?php echo esc_html($term->name); ?> </a>
<?php
}
?>

Smart Custom Fields で設定したタームの画像表示

<?php
$cat_img = SCF::get_term_meta( $term->term_id, $taxonomy_name, '画像' );
$img_url = wp_get_attachment_image_src( $cat_img, 'thumb' );
?>
<img src="<?php echo $img_url[0] ?>" alt="<?php echo esc_html($term->name); ?>" class="img-fluid" />

上記二つを組み合わせて画像付きタームの一覧表示

<?php
$taxonomy_name = 'item_cat';
$terms = get_terms( $taxonomy_name, 'hide_empty=0' );
foreach ( $terms as $term ) {
?>
<a href="<?php echo get_term_link($term); ?>">
<?php
$cat_img = SCF::get_term_meta( $term->term_id, $taxonomy_name, '画像' );
$img_url = wp_get_attachment_image_src( $cat_img, 'thumb' );
?>
<img src="<?php echo $img_url[0] ?>" alt="<?php echo esc_html($term->name); ?>" /> <span><?php echo esc_html($term->name); ?></span> </a>
<?php } ?>

親タームの下に子タームの一覧表示

<?php
$taxonomy_name = 'item_cat';
$args = array(
'hide_empty' => false,
'parent' => 0,
);
$terms = get_terms( $taxonomy_name, $args );
foreach ( $terms as $term ) {
?>
<!--親ターム-->
<a href="<?php echo get_term_link($term); ?>"><?php echo esc_html($term->name); ?></a> 
<!--//親ターム--> 
<!--子ターム-->
<?php
$args_child = array(
'hide_empty' => false,
'parent' => $term->term_id,
);
$terms = get_terms( $taxonomy_name, $args_child );
foreach ( $terms as $term ) {
?>
<a href="<?php echo get_term_link($term); ?>"><?php echo esc_html($term->name); ?></a>
<?php } ?>
<!--//子ターム--> 
<?php } ?>

親カテゴリ、投稿画面の新規カテゴリ追加、よく使うものを非表示

functions.php

<?php
function admin_style_category() { 
echo ' <style>
div.term-parent-wrap{ display:none; } 
tr.term-parent-wrap{ display:none; }
#item_cat-adder{ display:none; }
#item_cat-tabs li:nth-child(2){ display:none; }
</style>'
.PHP_EOL; } add_action('admin_print_styles', 'admin_style_category');
?>

関連記事:【WORDPRESS】新規カテゴリーの親カテゴリー登録を非表示にする方法

タクソノミーアーカイブページで現在表示しているページのタームIDを取得

<?php
$taxonomy_id = get_queried_object_id();
?>

single.phpで登録されているタームの情報を取得

<?php
$taxonomy_name = 'item_cat';
$terms = get_the_terms($post->ID,$taxonomy_name);
foreach( $terms as $term ) {
$term_id = $term->term_id; //タームID
$term_name = $term->name; //名前
$term_slug = $term->slug; //スラッグ
}
?>

親タームの情報のみ取得

<?php
$taxonomy_name = 'item_cat';
$term = array_pop(get_the_terms($post->ID, $taxonomy_name));
$term_p = $term->parent;
if ( ! $term_p == 0 ){
$term = array_shift(get_the_terms($post->ID, $taxonomy_name));
}
$term_id = $term->term_id; //タームID
$term_name = $term->name; //名前
$term_slug = $term->slug; //スラッグ
?>

カテゴリ説明の表示

この説明はデフォルトではあまり重要な意味を持ちませんが、これを表示するテーマも中にはあります。

↑これを表示させる方法

<?php echo category_description(); ?>

いろんな分岐

特定の固定ページを分岐する方法

<?php if ( is_page('【ページのスラッグ】') ) : ?>
ここにかく
<?php endif; ?>

特定のカスタム投稿を分岐する方法

<?php if ( is_post_type_archive('【カスタム投稿名】') or is_singular('【カスタム投稿名】') ) : ?>
ここにかく
<?php endif; ?>

トップページの1ページ目と2ページ目以降を分岐

<?php if(is_home() && !is_paged()): ?>
1ページ目
<?php else: ?>
2ページ目以降
<?php endif; ?>

タクソノミーアーカイブページで親タームと子タームを分岐

<?php
$taxonomy = $wp_query->get_queried_object();
?>
<?php if($taxonomy->parent == 0): ?>
親
<?php else: ?>
子
<?php endif; ?>

テーマファイル関連

アイキャッチ画像のURL取得

thumbnail サムネイル (デフォルト 150px x 150px :最大値)
medium 中サイズ (デフォルト 300px x 300px :最大値)
large 大サイズ (デフォルト 640px x 640px :最大値)
full フルサイズ (アップロードした画像の元サイズ)

<?php the_post_thumbnail_url("medium"); ?>

ショートコードをテーマファイルに記述する方法

関数do_shortcodeを使います。

<?php echo do_shortcode('[任意のショートコード]'); ?>

Breadcrumb NAVXTをテーマファイルに記述

<div class="breadcrumbs" typeof="BreadcrumbList" vocab="https://schema.org/">
    <?php if(function_exists('bcn_display'))
    {
        bcn_display();
    }?>
</div>

Smart Custom Fields の出力

<?php echo scf::get('【フィールド名】'); ?>

本文からディスクリプションを生成

functions.php

function get_meta_description() {
global $post;
$description = "";
if ( is_home()or is_front_page() ) { //トップページ
$description = get_bloginfo( 'description' );
} elseif ( is_category() ) {
$description = category_description();
}
elseif ( is_tag() ) {
$description = tag_description();
}
elseif ( is_single()or is_page() ) {
if ( $post->post_excerpt ) {
$description = $post->post_excerpt;
} else {
$description = strip_tags( $post->post_content );
$description = str_replace( "\n", "", $description );
$description = str_replace( "\r", "", $description );
$description = mb_substr( $description, 0, 150 ) . "...";
}
}
elseif ( is_archive() ) {
$description = get_bloginfo( 'description' );
}
else {
$description = get_bloginfo( 'description' );
}
$description = strip_tags( $description );
return $description;
}
function echo_meta_description_tag() {
echo get_meta_description();
}

header.php

<meta name="description" content="<?php echo_meta_description_tag(); if(get_query_var('paged')) echo ' | Page'.get_query_var('paged');?>"/>

メールフォーム関連

Contact Form 7 のフォームにclassを付与する方法

<label> お名前
[text your-name class:form-control] </label>
[submit class:btn class:btn-primary "送信"]

【Contact Form 7】フォームにclassを付与する方法

 

この記事を書いた人

o-saka(@abiko41)

フリーランスでWEB作ったりロゴ作ったりしてます。
お仕事のご依頼等は下記フォームより承っております 。お気軽にお問い合わせください。

お問い合わせ