WordPress ページurlの取得

Filed under: php,wordpress — kdcs @ 20年11月20日 金曜日

通常の構成なら(ループ内で使用)

the_permalink()

カスタムタクソノミー

get_term_link($term,$taxonomy)

ループ外でhttps://~フルで取得

<?php echo (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; ?>

andoridスマホにGoogleFontを読み込ませる

Filed under: php,wordpress,スマートフォン関係 — kdcs @ 20年11月5日 木曜日

条件分岐で「モバイルでiOS以外」とし、グーグルフォントを読み込ませる
下の例は、明朝体フォントを読み込ませる記述

<?php if( wp_is_mobile() && !is_iOS()): ?>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+JP&display=swap">
<?php endif; ?>

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

Filed under: php,wordpress — kdcs @ 20年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「Favorites」のお気に入り一覧でACFの画像を出力する

Filed under: php,wordpressプラグイン — kdcs @ 20年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 Popular Posts 特定のカテゴリー記事のランキング

Filed under: php,wordpressプラグイン — kdcs @ 20年2月24日 月曜日

カテゴリーページやアーカイブページ他、条件分岐で指定したカテゴリー内でそのカテゴリー記事のランキングを表示させる

<?php $cat = get_the_category(); ?>
<?php $cat_id = isset( $cat[0] ) ? $cat[0]->term_id : ''; ?>
<?php if ( function_exists( 'wpp_get_mostpopular' ) ) : ?>
<?php $args = array( 
  'limit' => 12,
  'range' => 'all',
  'order_by' => 'views',
  'thumbnail_width' => 100,
  'thumbnail_height' => 100,
  'cat' => $cat_id,
  'wpp_start' => '<ul>',
  'wpp_end' => '</ul>',
  'stats_views' => 1,
  'post_html' => '<li><a href="{url}">{thumb}</a><a href="{url}">{text_title}</a><span>{stats}</span></li>' ); ?>
<?php wpp_get_mostpopular( $args ); ?>
<?php endif; ?>

サイト内検索

カテゴリー

最近の投稿

↑上に戻る