WordPress Popular postsのカスタマイズ

Filed under: functions.php,wordpressプラグイン — kdcs @ 2020年9月26日 土曜日

ランキングプラグイン「WordPress Popular posts」の出力にカスタムフィールド(Advanced Custom Fields)の値や画像を含めたい。

function.phpの記述

/* ---------------------------------------------------------------------------------------
   ◆◆◆ WordPress Popular posts カスタマイズ ###########################################
------------------------------------------------------------------------------------------*/
function my_custom_single_popular_post( $post_html, $p, $instance ){
    $attachment_id =  get_field('02item-image1',$p->id);//カスタムフィールド画像取得
    $size = "img320"; //画像サイズ (thumbnail, medium, large, full or custom size) img320はカスタムサイズ
    $image = wp_get_attachment_image_src( $attachment_id, $size );
$acf1 = get_post_meta($p->id, 'field_name', true);//カスタムフィールドその1
$acf2 = get_post_meta($p->id, 'field_name', true);//カスタムフィールドその2
$custom_id= $p->id;//id番号出力
$output = '
<li><a href="' . get_the_permalink($p->id) . '"><img src="'. $image[0] .'" alt=""/><br />
' . esc_attr($p->title) . '<br />'. $acf1 .'</a>
</li>
';
return $output;
}
add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 );

ランキング表示
例:カスタム投稿タイプ items カスタムタクソノミー items_category タームid 28

<div id="rankingS" class="inner clearfix">
<?php
    if (function_exists('wpp_get_mostpopular')){
      $arg = array (
        'range' => 'all',//集計期間
        'order_by' => 'views',
        'stats_views' => '0',
        'post_type' => 'items',//投稿タイプ
         'taxonomy' => 'items_category',//カスタムタクソノミー
         'term_id' => ' 28',タームid
        'limit' => 4,//表示件数
        'wpp_start' => '<ul class="ranking">',//開始タグ
        'wpp_end' => '</ul>',//終了タグ
      );
      wpp_get_mostpopular($arg);
    }
    ?>
</div><!--/rankingS-->

(続きを読む…)

カスタムフィールドを利用したカスタム投稿タイプのタームでの絞り込み

Filed under: php,wordpress — kdcs @ 2020年9月20日 日曜日

Advanced Custom Fieldsのカスタムフィールドを利用してカスタム投稿タイプのタームで記事を絞り込む例
tax_queryのrelationをANDにしてブランドとタイプで絞り込み、WP_Queryでループさせる

カスタム投稿タイプ:items
カスタム投稿タイプのタクソノミー:items_category
カスタムフィールド:00brand , item_type , 02item-image1(メイン画像)

<h3>関連商品</h3>
<div id="recommend">
<?php
  $brand = get_field('00brand');
  $type = get_field('item_type');
    $args = array(
  'post_type' => 'items', //カスタム投稿タイプ名
	'tax_query' => array(
        'relation' => 'AND',
		array(
			'taxonomy' => 'items_category',
			'field'    => 'slug',
			'terms'    => $brand,
		),
		array(
			'taxonomy' => 'items_category',
			'field'    => 'slug',
			'terms'    => $type,
		),
	),
  'numberposts' => 12, //12件表示(デフォルトは5件)
  'orderby' => 'abc', //ランダム表示 rand
  'post__not_in' => array($post->ID) //表示中の記事を除外
 );
?>
<p><?php echo $type; ?></p>
<div class="item">
<div class="itemList inner clearfix">
<?php $recommend = new WP_Query( $args ); ?>
<?php if($recommend -> have_posts()): while($recommend -> have_posts()): $recommend -> the_post(); ?>
<?php
    $attachment_id1 = get_field('02item-image1');
    $size1 = "thumbnail"; // (thumbnail, medium, large, full or custom size)
    $image1 = wp_get_attachment_image_src( $attachment_id1, $size1 );
    $attachment_id1f = get_field('02item-image1');
    $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;
?>
<div class="itemBox4"><a href="<?php the_permalink(); ?>"><img src="<?php echo $image1f[0]; ?>" alt="<?php echo $alt1; ?>" title="<?php echo $image_title1; ?>"><h4><?php echo nl2br(post_custom('01model')); ?></h4><p><?php echo nl2br(post_custom('03price')); ?></p></a></div>
<?php endwhile; ?>
</div><!--/itemList-->
</div><!--/item-->
<?php else : ?>
 <p>関連アイテムはまだありません。</p>
</div><!--/itemList-->
</div><!--/item-->
<?php endif; wp_reset_postdata(); ?>
</div><!--/#recommend-->

WordPress Popular Postsでwelcartの商品メイン画像を使えるようにする

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

アイキャッチ画像を呼び出す部分をwelcart商品メイン画像に差し替える

//商品画像を取得してPopular postのアイキャッチ画像にする
function my_custom_single_popular_post( $post_html, $p, $instance ){
	global $usces;
	$post_id = $p->id;
	$code =  get_post_meta($post_id, '_itemCode', true);
	$pictid = (int)$usces->get_mainpictid($code);
	$image_src = wp_get_attachment_image_src($pictid);
	$output = '<li class="wpp_list_item cf"><a href="' . get_the_permalink($p->id) . '" class="my-custom-title-class"><img src="' .$image_src[0]. '"/>' . esc_html( $p->title ) . '</a></li>' ;  
	return $output;
}
add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 );

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');

サイト内検索

カテゴリー

最近の投稿

↑上に戻る