質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

1回答

1618閲覧

wordpressのカスタム投稿タイプ(+カスタムタクソノミーも)をプラグインに頼らず表示したい

nukoro

総合スコア4

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2020/01/17 07:04

編集2020/01/31 10:28

wordpressのカスタム投稿タイプ(+カスタムタクソノミーも)をプラグインに頼らず利用したいと思い書き込みいたしました。※テーマは _s(underscores)をカスタマイズして利用しています

試したこと ※追記あり

すでに解決済みのこちらのページを参考にし、カスタム投稿一覧・記事ページで本文の表示はできました。しかしそのままでは日付が表示できなかったため、修正して日付は表示できるようになりました。
しかしどうしてもカスタムタクソノミーの情報(カテゴリーやタグ)の情報を出すことができず四苦八苦しております。

※その後いろいろ試行錯誤し、ターム(カテゴリーやタグ)の情報を出すことができるようになりました
しかし今度はタームをクリックした際に表示される一覧ページで、本文とターム情報が表示できず悩んでおります。
どうかお知恵をお貸しください・・・

php

1//function.phpに書いたコード ※抜粋 2 3add_action('init', 'blog_post_type'); 4function blog_post_type() { 5 $params = array( 6 'labels' => array( 7 'name' => 'ブログ', 8 'singular_name' => 'ブログ', 9 'add_new' => '新規追加', 10 'add_new_item' => 'ブログを新規追加', 11 'edit_item' => 'ブログを編集する', 12 'new_item' => '新規ブログ', 13 'all_items' => 'ブログ一覧', 14 'view_item' => 'ブログの説明を見る', 15 ), 16 'menu_position' => 5, 17 'public' => true, 18 'has_archive' => true, 19 'exclude_from_search' => true, 20 'rewrite' => array( 21 'slug' => 'blog', 22 'with_front' => false 23 ), 24 'supports' => array( 25 'title', //(タイトル) 26 'editor', //(内容の編集) 27 'author', //(作成者) 28 'thumbnail', //(アイキャッチ画像) 29 ), 30 'taxonomies' => array('blog_category','blog_tag') 31 ); 32 register_post_type('blog', $params); 33} 34// カスタム投稿タイプ(blog)用のカテゴリ&タグを作成する 35add_action('init', 'create_blog_taxonomies'); 36function create_blog_taxonomies() { 37 // カテゴリを作成 38 $labels = array( 39 'name' => 'カテゴリ', 40 'singular_name' => 'カテゴリ', 41 'search_items' => 'カテゴリを検索', 42 'all_items' => '全てのカテゴリ', 43 'parent_item' => '親カテゴリ', 44 'parent_item_colon' => '親カテゴリ:', 45 'edit_item' => 'カテゴリを編集', 46 'update_item' => 'カテゴリを更新', 47 'add_new_item' => '新規カテゴリを追加', 48 'new_item_name' => '新規カテゴリ', 49 'menu_name' => 'カテゴリ' 50 ); 51 $args = array( 52 'hierarchical' => true, 53 'labels' => $labels, 54 'rewrite' => array( 'slug' => 'blog_category' ) 55 ); 56 register_taxonomy( 'blog_category', 'blog', $args ); 57 58 // タグを作成 59 $labels = array( 60 'name' => 'タグ', 61 'singular_name' => 'タグ', 62 'search_items' => 'タグを検索', 63 'all_items' => '全てのタグ', 64 'parent_item' => null, 65 'parent_item_colon' => null, 66 'edit_item' => 'タグを編集', 67 'update_item' => 'タグを更新', 68 'add_new_item' => '新規タグを追加', 69 'new_item_name' => '新規タグ', 70 'separate_items_with_commas' => 'タグが複数ある場合はコンマで区切ってください', 71 'add_or_remove_items' => 'タグを追加or削除する', 72 'choose_from_most_used' => 'よく使われているタグから選択', 73 'not_found' => 'アイテムは見つかりませんでした', 74 'menu_name' => 'タグ' 75 ); 76 $args = array( 77 'hierarchical' => false, 78 'labels' => $labels, 79 'update_count_callback' => '_update_post_term_count', 80 'rewrite' => array( 'slug' => 'blog_tag' ) 81 ); 82 83 register_taxonomy( 'blog_tag', 'blog', $args ); 84} 85

html

1//一覧ページなどに書いたコード 2<?php the_terms($post->ID,'blog'); ?>

php

1////archive.php ※抜粋 ※追記あり 2<main> 3<div id="box_main" class="col-sm-9 site-main" role="main"> 4 5<div id="entry_box"> 6 <?php 7 <?php if ( have_posts() || is_tax() ) : ?><!-- ※ここを修正しました --> 8 9 <header class="page-header"> 10 <?php 11 the_archive_title( '<h2 class="page-title">', '</h2>' ); 12 the_archive_description( '<div class="taxonomy-description">', '</div>' ); 13 ?> 14 </header><!-- .page-header --> 15 16 <?php 17 /* Start the Loop */ 18 while ( have_posts() ) : the_post(); 19 20 /* 21 * Include the Post-Format-specific template for the content. 22 */ 23 get_template_part( 'template-parts/content', get_post_format() ); 24 25 endwhile; 26 27 the_posts_navigation(); 28 29 else : 30 31 get_template_part( 'template-parts/content', 'none' ); 32 33 endif; ?> 34</div> 35 36</div> 37</main> 38

php

1//content.php ※追記あり 2<?php ?> 3 4<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 5 <div class="entry-header"> 6 <?php 7 if ( is_single() ) : 8 the_title( '<h2 class="entry-title">', '</h2>' ); 9 else : 10 the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' ); 11 endif; 12 13//カスタム投稿を追加 14wp_list_categories('title_li=&taxonomy=blog_category,blog_tag'); 15 16 if ( 'post' === get_post_type() || 'blog' === get_post_type() ) : ?><!-- ※ここを修正しました --> 17 <div class="entry-meta"> 18 <?php underscores_posted_on(); ?> 19 </div><!-- .entry-meta --> 20 <?php endif; ?> 21 </div><!-- .entry-header --> 22 23 24 <div class="entry-content clearfix"> 25 <?php 26 the_content( sprintf( 27 /* translators: %s: Name of current post. */ 28 wp_kses( __( '続きを読む', 'underscores' ), array( 'span' => array( 'class' => array() ) ) ), 29 the_title( '<span class="screen-reader-text">"', '"</span>', false ) 30 ) ); 31 32 wp_link_pages( array( 33 'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'underscores' ), 34 'after' => '</div>', 35 ) ); 36 ?> 37 </div><!-- .entry-content --> 38 39 <div class="entry-footer"> 40 <?php underscores_entry_footer(); ?> 41 </div><!-- .entry-footer --> 42</article><!-- #post-## -->

ファイルの構造

archive.php は content.php を読込み、content.php は template-tags.php を読込む構造になっているようです。
template-tags.php で共通表示される部分(本文やその他情報)を管理しているようです。

※色々試した結果「if ( 'blog' === get_post_type() ~」を追加して、タームの情報が表示できるようになりました

php

1//template-tags.php ※追記あり 2<?php 3 4if ( ! function_exists( 'underscores_posted_on' ) ) : 5/** 6 * Prints HTML with meta information for the current post-date/time and author. 7 */ 8function underscores_posted_on() { 9 $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>'; 10 if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) { 11 $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>'; 12 } 13 14 $time_string = sprintf( $time_string, 15 esc_attr( get_the_date( 'c' ) ), 16 esc_html( get_the_date() ), 17 esc_attr( get_the_modified_date( 'c' ) ), 18 esc_html( get_the_modified_date() ) 19 ); 20 21 $posted_on = sprintf( 22 esc_html_x( '投稿日:%s', 'post date', 'underscores' ), 23 '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>' 24 ); 25 26 $byline = sprintf( 27 esc_html_x( 'by %s', 'post author', 'underscores' ), 28 '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>' 29 ); 30 31 echo '<span class="posted-on">' . $posted_on . '</span>'; // WPCS: XSS OK. 32} 33endif; 34 35if ( ! function_exists( 'underscores_entry_footer' ) ) : 36/** 37 * Prints HTML with meta information for the categories, tags and comments. 38 */ 39 40function underscores_entry_footer() { 41 // Hide category and tag text for pages. 42 if ( 'post'||'blog' === get_post_type() ) { 43 /* translators: used between list items, there is a space after the comma */ 44 $categories_list = get_the_category_list( esc_html__( ', ', 'underscores' ) ); 45 if ( $categories_list && underscores_categorized_blog() ) { 46 printf( '<p class="cat-links">' . esc_html__( 'カテゴリー:%1$s', 'underscores' ) . '</p>', $categories_list ); // WPCS: XSS OK. 47 } 48 49 /* translators: used between list items, there is a space after the comma */ 50 $tags_list = get_the_tag_list( '', esc_html__( ', ', 'underscores' ) ); 51 if ( $tags_list ) { 52 printf( '<p class="tags-links">' . esc_html__( 'タグ:%1$s', 'underscores' ) . '</p>', $tags_list ); // WPCS: XSS OK. 53 } 54 } 55//カスタム投稿タイプのアーカイブ ※追加しました 56if ( 'blog' === get_post_type() ) { 57 the_taxonomies( $args ); 58} 59 if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) { 60 echo '<span class="comments-link">'; 61 /* translators: %s: post title */ 62 comments_popup_link( sprintf( wp_kses( __( 'Leave a Comment<span class="screen-reader-text"> on %s</span>', 'underscores' ), array( 'span' => array( 'class' => array() ) ) ), get_the_title() ) ); 63 echo '</span>'; 64 } 65 66} 67endif; 68 69/** 70 * Returns true if a blog has more than 1 category. 71 */ 72function underscores_categorized_blog() { 73 if ( false === ( $all_the_cool_cats = get_transient( 'underscores_categories' ) ) ) { 74 // Create an array of all the categories that are attached to posts. 75 $all_the_cool_cats = get_categories( array( 76 'fields' => 'ids', 77 'hide_empty' => 1, 78 // We only need to know if there is more than one category. 79 'number' => 2, 80 ) ); 81 82 // Count the number of categories that are attached to the posts. 83 $all_the_cool_cats = count( $all_the_cool_cats ); 84 85 set_transient( 'underscores_categories', $all_the_cool_cats ); 86 } 87 88 if ( $all_the_cool_cats > 1 ) { 89 // This blog has more than 1 category so underscores_categorized_blog should return true. 90 return true; 91 } else { 92 // This blog has only 1 category so underscores_categorized_blog should return false. 93 return false; 94 } 95} 96 97/** 98 * Flush out the transients used in underscores_categorized_blog. 99 */ 100function underscores_category_transient_flusher() { 101 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 102 return; 103 } 104 // Like, beat it. Dig? 105 delete_transient( 'underscores_categories' ); 106} 107add_action( 'edit_category', 'underscores_category_transient_flusher' ); 108add_action( 'save_post', 'underscores_category_transient_flusher' ); 109

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

どの部分で問題が起きているかが読み取れませんでしたが、とりあえず下記部分は間違っていると思います。

PHP

1// if ( 'post'||'blog' === get_post_type() ) // 'post'がtrueと判定されて必ず通過する 2 if ( 'post' === get_post_type() || 'blog' === get_post_type() )

【PHP: 論理型 (boolean) - Manual】
https://www.php.net/manual/ja/language.types.boolean.php#language.types.boolean.casting

投稿2020/01/17 16:18

kei344

総合スコア69416

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nukoro

2020/01/18 01:18

その部分は分岐条件の書き方を調べて自分で追加した部分なのですが、 書き方が間違っていたのですね・・・正しい書き方を教えて頂き助かります! 該当部分を直してみて表示内容に変化はありませんでしたが、ご指摘ありがとうございました
kei344

2020/01/18 03:10

どこでタームを表示しているかが提示のコードからは読み取れませんでした。そこの問題では?
nukoro

2020/01/31 10:27

お返事遅くなりましてすみません>< 時間がかかってしまいましたが、いろいろ調べて試行錯誤し、タームの情報は表示できるようになりました! ただ今度はタームをクリックした際に表示される一覧ページで、本文とターム情報が表示できず悩んでおります・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問