カテゴリー
JavaScript

同じclass場合rowspanで列を結合するjQuery

テーブルの列で、同じclassの番号の場合、rowspanで結合するjQuery。配列に入れたcate-xxをfindして、番号込で同じ数値の時は結合する。

こちらのサイトを参考にしました。ありがとうございます。
http://study-upup.blogspot.com/2014/03/jqueryjquery.html

<table id="testTable-1">

<tr>
<td class="cate01-894">test</td>
<td class="cate02-894">user</td>
<td class="cate03-894">test</td>
<td class="cate04-894">完了</td>
</tr>

<tr>
<td class="cate01-894">test</td>
<td class="cate02-894">user</td>
<td class="cate03-894">test</td>
<td class="cate04-894">完了</td>
</tr>

</table>

<script>
var arr = ['cate01','cate02','cate03','cate04'];
var num = 0;
$.each(arr, function(i, value){
var cate = arr[num];
$(document).ready(function(){
$('#testTable-1').each(function () {
var pre_element = null;
var col_num = 0;
$(this).find('tr').each(function () {
var now_td = $(this).find('td[class^='+cate+']').eq( col_num );
if (pre_element == null) {
pre_element = now_td;
} else if (now_td.attr('class') == pre_element.attr('class')) {
now_td.remove();
if (pre_element.attr('rowspan') == null) pre_element.attr('rowspan', 1);
pre_element.attr('rowspan', parseInt(pre_element.attr('rowspan'),10) + 1);
} else {
pre_element = now_td;
}
});
});
});
num++;
})
</script>

 

カテゴリー
WordPress

ACFでタクソノミータームのフィールド値を取得

カテゴリ(タクソノミーターム)ごとにWebアイコンを変えたかったので、ACFでタクソノミータームごとにフィールドを設置できる設定を行ったが、表示方法がよくわからなかった。

こちらのブログに習って設置しました。ありがとうございます。

https://hirashimatakumi.com/blog/1365.html
Advanced Custom Fields で登録したタームのフィールド値を取得する方法

 

実際に設置したコードはこちら。Webアイコンの名前を出力している。

$terms = get_terms('タクソノミー名');
foreach ( $terms as $term ) {
$terms_id = $term->term_id; //termIDを取得

//省略

$icon_name = get_field('フィールド名','タクソノミー名_'.$terms_id);
echo $icon_name;

}

 

カテゴリー
WordPress

見出しと所属する記事の一覧表示

人は忘れる。そして同じところで躓く。今回もそうだった。従って忘れないうちにメモをしておく。

以下はカスタム投稿で固定ページテンプレートを使用する際、見出し(タクソノミーターム)と所属する記事を一覧表示させたいときに使用した。2つのループを使用している。説明不足だが、自分への忘備録なのでご理解いただきたい。

<!-- パラメータ取得&見出しループスタート -->
<?php $terms = get_terms('タクソノミー名'); foreach ( $terms as $term ) { $terms_name = $term->name;
$terms_slug = $term->slug;
?>

<h2><?php echo $terms_name;?></h2>

<!-- 入れ子 -->
<?php $args = array( 'post_type' => 'ポストタイプ名',
	'tax_query' => array(
		array(
			'taxonomy' => 'タクソノミー名',
			'field'    => 'slug',
			'terms'    => $terms_slug,
		),
	),
);
$the_query = new WP_Query( $args ); ?>

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

		
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>


	<?php endwhile; ?>
	<?php wp_reset_postdata(); ?>
<?php else : ?>
	

<?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?>

<?php endif; ?>
</ul>

<!-- 入れ子ここまで -->
<?php } ?>
<!-- 見出しループここまで -->

カテゴリー
WordPress

Contact Form7にショートコードを登録

Contact Form7に投稿記事を挿入したかったので、ショートコードを使って記事を挿入することにした。ネット上にあまり情報がなかったので、忘備録として記録する。

Functions.phpに下記を記述。

// Contact Form 7 にショートコードを追加
function wpcf7_description( $tag ) {
$tag = new WPCF7_Shortcode( $tag );
$count = $tag->get_option( 'count', '', true );
$content = get_post( $count );
return $content->post_content;
}
wpcf7_add_form_tag( 'description', 'wpcf7_description' );

Contact Form7管理画面のフォームには下記のように記述。

[description count:10]

無事、Post=10の記事を抽出することができた。

下記サイトを参考にしました。感謝。

Contact Form 7用のショートコードを作成する

以前、ショートコードを登録する際使われていたwpcf7_add_shortcodeタグは非推奨になっているようだ。公式によると、wpcf7_add_form_tagの使用を推奨している。

Contact Form 7 4.6リリースノートより

Contact Form 7 4.6

カテゴリー
WordPress

グーテンバーガーは語らない

(この投稿はGutenbergエディタのテストです)

昔、まだパソコンがマイコンと呼ばれていた時代の話である。場所は埼玉県の片田舎に「ダザニーランド」という名前の遊園地があった。地元でも知る人は少なく、インターネットもなかった時代であるから、どんな遊園地は知る由もなかった。春男がダザニーランドへ来ることになったのは、新聞の折り紙広告に清掃員募集の求人広告を見て、応募したからである。それまで春男はダザニーランドを知らなかったが、片田舎の遊園地の清掃員はおそらく暇であろうということと、思いのほか報酬がよかったことから、なんとなく応募したのであった。

(つづく)

Gutenbergの第一印象は「Mediumみたいなだな」です。エディタはすっきりして好み、このエディタがそのまま表のデザインとして出力するようなテーマが欲しいと思いました。おわり。

カテゴリー
WordPress

httpからhttpsへリダイレクト

Chrome68からhttpでアクセスすると、アドレスバーに「保護されていません」とアナウンスが出るようになった。まだ灰色の文字なので目立たない(?)が、Chrome70からは赤文字になるらしい。Googleちょっとやりすぎじゃないか…?

WordPressでhttpでのアクセスをhttpsへリダイレクトの一例です。
.htaccessの最初に下記を記述する。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

# BEGIN WordPress
...

SSL認証のシールが貼ってあってもhttpで通信してるサイトとか、結構あるんだと思った。お金がもったいない。
HSTSが使えるなら、そっちのほうがいいとのことです。

4月に同じ内容でブログを書いていた…。

カテゴリー
WordPress

.msgファイルをメディアにアップロードさせたいという謎案件

という案件に遭遇。
.msgはOUTLOOKのメッセージファイルのようです。

MIMEタイプがよくわからん…!
で調べたら、「application/vnd.ms-outlook」とのこと。
下記をFunctions.phpに書いてみる。

function allow_upload_msg( $mimes ) {
    $mimes['msg'] = 'application/vnd.ms-outlook';
    return $mimes;
}
add_filter( 'upload_mimes', 'allow_upload_msg' );

これでOK(未確認)。

参考サイト
http://www.whatisfileextension.com/jp/msg/

カテゴリー
その他

.htaccessでアクセス制限(Apache2.4系)

先日も似た記事を書いたばかりなのだが、他の方より自分が書いた.htaccessの記述がおかしいと指摘があった。また同じ間違いをおかしてしまったので、自戒をこめて、ここに記す。

Apache2.4系でxmlrpc.phpのアクセスを制限したいとき。

<Files "xmlrpc.php">
    Require all denied
</Files>

  
  
Apache2.2系の書き方
<Files "xmlrpc.php">
order deny,allow
deny from all
</Files>

カテゴリー
WordPress

WordPressのログイン画面にFavicon出したり

「WordPressのログイン画面にFavicon出せ」という依頼があった。
ログイン画面のheaderにhtmlを表示させるには、アクションフックを使うとよいらしい。

忘れないうちに以下略。

Functions.php

/* ログイン画面にfaviconとapple-touch-iconを表示 */
function admin_favicon() {
echo '<link rel="shortcut icon" href="' . get_stylesheet_directory_uri() . '/favicon.png" />';
echo '<link rel="apple-touch-icon" size="152x152" href="' . get_stylesheet_directory_uri() . '/apple-touch-icon.png" />'; 
}
add_action('login_head', 'admin_favicon');

ちなみに、
add_action(‘admin_head’, ‘admin_favicon’);にすると、
管理画面のheaderに記述できる。

codexにアクションフック一覧がある。
色々ありすぎて読むのが大変。

プラグイン API/アクションフック一覧
https://wpdocs.osdn.jp/%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3_API/%E3%82%A2%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3%E3%83%95%E3%83%83%E3%82%AF%E4%B8%80%E8%A6%A7

カテゴリー
WordPress

よくわかんないけどget_postsで表示

Advanced Custom Fields のアドオン Repeater Fieldのループ内で表示がうまくできない、と相談があり。

よくわかんないんだけど、get_postsで表示させた。リピーターフィールドの値をタームに入れて、最新の日付を表示。

$my_postsの中身みて、必要なものを取り出す。取り出した日付の値を、strtotimeで整形しています。

これで良かったのだろうか…。

<?php
$category = get_sub_field('xxxx');
$args = array(
  'post_type' => 'xxxx',
  'posts_per_page' => 1,
  'tax_query' => array(
    array(
      'taxonomy' => 'xxxxxx',
      'field'    => 'slug',
      'terms'    => $category,
    ),
  ),
);
$my_posts = get_posts( $args );
if( $my_posts ) {
	$date = $my_posts[0]->post_date;
	?>
	 <p class="pickup">更新日:<?php echo date('Y.m.d', strtotime($date));?><time datetime="<?php echo date('Y.m.d', strtotime($date));?>"></time></p>
<?php
}
?>

テンプレートタグ/get posts
https://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/get_posts