WordPress 投稿(デフォルト)のタグをチェックボックス化

Filed under: functions.php,wordpress — kdcs @ 2020年8月20日 木曜日

カスタム投稿タイプのカスタムタクソノミー(カテゴリー・タグ)を作成時、
タグをチェックボックス化する場合、「’hierarchical’」をtrueにすればよいが、
デフォルトの投稿の場合は以下をfunctions.phpに記述する

2~7行の記述は必要ないかも・・・

function post_tag_checkbox() {
  global $wp_rewrite;
  $rewrite = array(
    'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
    'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),
    'ep_mask' => EP_TAGS,
  );
 
  $labels = array(
    'name' => _x( 'Tags', 'taxonomy general name' ),
    'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
    'search_items' => __( 'Search Tags' ),
    'popular_items' => __( 'Popular Tags' ),
    'all_items' => __( 'All Tags' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Tag' ),
    'view_item' => __( 'View Tag' ),
    'update_item' => __( 'Update Tag' ),
    'add_new_item' => __( 'Add New Tag' ),
    'new_item_name' => __( 'New Tag Name' ),
    'separate_items_with_commas' => __( 'Separate tags with commas' ),
    'add_or_remove_items' => __( 'Add or remove tags' ),
    'choose_from_most_used' => __( 'Choose from the most used tags' ),
    'not_found' => __( 'No tags found.' )
  );
 
  register_taxonomy( 'post_tag', 'post', array(
    'hierarchical' => true,
    'query_var' => 'tag',
    'rewrite' => $rewrite,
    'public' => true,
    'show_ui' => true,
    'show_admin_column' => true,
    '_builtin' => true,
    'labels' => $labels
  ));
}
add_action( 'init', 'post_tag_checkbox', 1 );

WordPress カスタム投稿タイプの作成 2013年版

Filed under: functions.php,wordpress — kdcs @ 2020年8月18日 火曜日

WordPress カスタム投稿タイプとカスタムタクソノミー(カテゴリー・タグ)の作成

functions.php

// カスタム投稿タイプを作成する
add_action('init', 'add_websites_post_type');
function add_websites_post_type() {
    $params = array(
            'labels' => array(
                    'name' => 'サイト',
                    'singular_name' => 'サイト',
                    'add_new' => '新規追加',
                    'add_new_item' => 'サイトを新規追加',
                    'edit_item' => 'サイトを編集する',
                    'new_item' => '新規サイト',
                    'all_items' => 'サイト一覧',
                    'view_item' => 'サイトの説明を見る',
                    'search_items' => '検索する',
                    'not_found' => 'サイトが見つかりませんでした。',
                    'not_found_in_trash' => 'ゴミ箱内にサイトが見つかりませんでした。'
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array(
                    'title',
                    'editor',
                    'author',
                    'custom-fields',
            ),
            'taxonomies' => array('websites_category','websites_tag')
    );
    register_post_type('websites', $params);
}
// カスタム投稿タイプ(websites)用のカテゴリ&タグを作成する
add_action('init', 'create_websites_taxonomies');
function create_websites_taxonomies() {
    // カテゴリを作成
    $labels = array(
            'name'                => 'Webカテゴリ',        //複数系のときのカテゴリ名
            'singular_name'       => 'Webカテゴリ',        //単数系のときのカテゴリ名
            'search_items'        => 'Webカテゴリを検索',
            'all_items'           => '全てのWebカテゴリ',
            'parent_item'         => '親カテゴリ',
            'parent_item_colon'   => '親カテゴリ:',
            'edit_item'           => 'Webカテゴリを編集',
            'update_item'         => 'Webカテゴリを更新',
            'add_new_item'        => '新規Webカテゴリを追加',
            'new_item_name'       => '新規Webカテゴリ',
            'menu_name'           => 'Webカテゴリ'        //ダッシュボードのサイドバーメニュー名
    );
    $args = array(
            'hierarchical'        => true,
            'labels'              => $labels,
            'rewrite'             => array( 'slug' => 'websites_cat' )
    );
    register_taxonomy( 'websites_category', 'websites', $args );

    // タグを作成
    $labels = array(
            'name'                => 'Webタグ',        //複数系のときのタグ名
            'singular_name'       => 'Webタグ',        //単数系のときのタグ名
            'search_items'        => 'Webタグを検索',
            'all_items'           => '全てのWebタグ',
            'parent_item'         => null,
            'parent_item_colon'   => null,
            'edit_item'           => 'Webタグを編集',
            'update_item'         => 'Webタグを更新',
            'add_new_item'        => '新規Webタグを追加',
            'new_item_name'       => '新規Webタグ',
            'separate_items_with_commas'   => 'Webタグをコンマで区切る',
            'add_or_remove_items'          => 'Webタグを追加or削除する',
            'choose_from_most_used'        => 'よく使われているWebタグから選択',
            'not_found'                    => 'アイテムは見つかりませんでした',
            'menu_name'                    => 'Webタグ'        //ダッシュボードのサイドバーメニュー名
    );
    $args = array(
            'hierarchical'            => false,    //trueでタグをチェックボックス化
            'labels'                  => $labels,
            'update_count_callback'   => '_update_post_term_count',    //タグの動作に必要なCallback設定
            'rewrite'                 => array( 'slug' => 'websites_tag' )
    );

    register_taxonomy( 'websites_tag', 'websites', $args );
}

WordPress「Favorites」のお気に入り一覧でACFの画像を出力する

Filed under: php,wordpressプラグイン — kdcs @ 2020年8月12日 水曜日

WordPress お気に入りプラグイン「Favorites」のお気に入り一覧ページに
ACF(アドバンスドカスタムフィールド)を出力させる

一覧ページにACFの画像を出力
カスタムフィールド名:02item-image1

<?php 

$favorites = get_user_favorites();

    if (isset($favorites) && !empty($favorites)) :
      foreach ($favorites as $post_id) :
$p = get_post($post_id);

    $attachment_id1f = get_field('02item-image1' , $post_id );
    $size1f = "large"; // (thumbnail, medium, large, full or custom size)
    $image1f = wp_get_attachment_image_src( $attachment_id1f, $size1f );
    $attachment = get_post( get_field('02item-image1') );
    $alt1 = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
    $image_title1 = $attachment->post_title;

        echo '<div>' . get_the_title($post_id) . get_favorites_button($post_id) . '</div>';
        echo '<img src=' . $image1f[0] . '>';
      endforeach;
    else :
      // No Favorites
      echo '<p class="text-center">お気に入りがありません。</p>';
    endif; ?>

WordPress カスタム投稿タイプのカテゴリー取得表示

Filed under: wordpress — kdcs @ 2020年7月10日 金曜日

カスタム投稿タイプで複数のカテゴリーを使用する場合のカテゴリー名取得と表示
※ループの中に記述する

<?php
  if($terms = get_the_terms($post->ID, 'staffblog_category')) {
     foreach ( $terms as $term ) {
         echo esc_html($term->name);
        }
}
?>

スクロールでサイドバーを「ぬるっと」固定する

Filed under: JavaScript — kdcs @ 2020年6月23日 火曜日

jQuery使います。

<div id="content">
<div id="contentArea" class="inner">
<div id="main">
メインエリア
</div><!--/#main-->
<div id="aside">
<div id="asideBox">
サイドバー
</div><!--/#asideBox-->
</div><!--/#aside-->
</div><!--/#contentArea-->
</div><!--/#content-->

(続きを読む…)

サイト内検索

カテゴリー

最近の投稿

↑上に戻る