WordPress 閲覧履歴をプラグイン無しで

Filed under: welcart,wordpress — kdcs @ 2020年9月11日 金曜日

WordPress 閲覧履歴をプラグイン無しで行う
※cookieを利用

header.phpのheadタグ内に以下を記述
解説ではheadタグ直下に記述しないとエラーになると書いてあったが、タグ内下部に記述しても動作した。

<?php
global $rireki;

//記事ページのみcookieに登録
if(is_single()){

//閲覧履歴用のcookieが存在する場合
if( isset($_COOKIE['rireki']) ){

//配列にする
$rireki = explode(",", $_COOKIE['rireki']);

//cookieに現在の記事IDがあるかどうか調べる
$aruno = in_array($post->ID, $rireki);

//ある場合の処理
if($aruno == true){

//cookieにある現在の記事IDを削除(順番整理&表示除外用)
$rireki = array_diff($rireki,array($post->ID));
$rireki = array_values($rireki);
}

//cookieが5個以上ある場合、4個に減らす
if(count($rireki) >= 5 ){
$set_rireki = array_slice($rireki , 0, 4);
}else{
$set_rireki = $rireki;
}
//cookieに登録
$touroku = $post->ID.','.implode(",", $set_rireki);
setcookie( 'rireki', $touroku, time() + 7776000,'/');

//cookieに現在の記事IDが無い場合の処理
}else{
$touroku = $post->ID;
setcookie( 'rireki', $touroku, time() + 7776000,'/');
}

//記事ページ以外ならcookieの読み込みのみ
}else{
if( isset($_COOKIE['rireki']) ){
$rireki = explode(",", $_COOKIE['rireki']);
}
}
?>

閲覧履歴を出力したい場所に以下記述

<?php
global $rireki;
//履歴が現在の記事を除いて、一つでもある場合
if (!empty($rireki)){

$args = array(
'posts_per_page' => -1,
'post__in' => $rireki,
'orderby' => 'post__in',
);
$the_query = new WP_Query($args);

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

<!--ここに記事を表示させる処理-->
<?php the_title(); ?>

<?php
endwhile;
endif;
wp_reset_postdata();
}else{ ?>
//cookieが無い場合の処理
<?php } ?>

(続きを読む…)

welcart ユーザー権限のカスタマイズ

Filed under: functions.php,welcart — kdcs @ 2020年9月3日 木曜日

WordPressのユーザーを「投稿者」にすると商品マスターと新規商品登録しかできない。
このユーザー権限を変更してwelcartのメニューが使えるようにする。

function.phpに以下を記述
※必要のない項目は除外すればよい

    function remove_menus () {
      if (!current_user_can('administrator')) { // 管理者権限以外
        global $menu;
        remove_submenu_page('usc-e-shop/usc-e-shop.php', 'usces_initial'); // 基本設定
        remove_submenu_page('usc-e-shop/usc-e-shop.php', 'usces_delivery'); // 配送設定
        remove_submenu_page('usc-e-shop/usc-e-shop.php', 'usces_mail'); // メール設定
        remove_submenu_page('usc-e-shop/usc-e-shop.php', 'usces_cart'); // カートページ設定
        remove_submenu_page('usc-e-shop/usc-e-shop.php', 'usces_member'); // 会員ページ設定
        remove_submenu_page('usc-e-shop/usc-e-shop.php', 'usces_system'); // システム設定
        remove_submenu_page('usc-e-shop/usc-e-shop.php', 'usces_settlement'); // クレジット決済設定
      }
    }
    add_action('admin_menu', 'remove_menus');

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; ?>

サイト内検索

カテゴリー

最近の投稿

↑上に戻る