kz
フォーラムへの返信
-
フォーラム: 使い方全般
返信が含まれるトピック: ギャラリーの写真の上に文字かバナーを入れたいです1)テーマの functions.php に以下のようなコードを書けばOK。
※デフォルトのタグが dl, dt, dd なのでアイコン用タグ(dt)の中にタイトルを入れていますが、お好みで変更してください。apply_filters( 'post_gallery', 'my_post_gallery', 10, 2 ); function my_post_gallery( $output, $attr ) { // We're trusting author input, so let's at least make sure it looks like a valid orderby statement if ( isset( $attr['orderby'] ) ) { $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); if ( !$attr['orderby'] ) unset( $attr['orderby'] ); } extract(shortcode_atts(array( 'order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post ? $post->ID : 0, 'itemtag' => 'dl', 'titletag' => 'div', // ★追加 'icontag' => 'dt', 'captiontag' => 'dd', 'columns' => 3, 'size' => 'thumbnail', 'include' => '', 'exclude' => '', 'link' => '' ), $attr, 'gallery')); $id = intval($id); if ( 'RAND' == $order ) $orderby = 'none'; if ( !empty($include) ) { $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); $attachments = array(); foreach ( $_attachments as $key => $val ) { $attachments[$val->ID] = $_attachments[$key]; } } elseif ( !empty($exclude) ) { $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); } else { $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); } if ( empty($attachments) ) return ''; if ( is_feed() ) { $output = "\n"; foreach ( $attachments as $att_id => $attachment ) $output .= wp_get_attachment_link($att_id, $size, true) . "\n"; return $output; } $itemtag = tag_escape($itemtag); $captiontag = tag_escape($captiontag); $icontag = tag_escape($icontag); $valid_tags = wp_kses_allowed_html( 'post' ); if ( ! isset( $valid_tags[ $itemtag ] ) ) $itemtag = 'dl'; if ( ! isset( $valid_tags[ $captiontag ] ) ) $captiontag = 'dd'; if ( ! isset( $valid_tags[ $icontag ] ) ) $icontag = 'dt'; $columns = intval($columns); $itemwidth = $columns > 0 ? floor(100/$columns) : 100; $float = is_rtl() ? 'right' : 'left'; $selector = "gallery-{$instance}"; $gallery_style = $gallery_div = ''; if ( apply_filters( 'use_default_gallery_style', true ) ) $gallery_style = " <style type='text/css'> #{$selector} { margin: auto; } #{$selector} .gallery-item { float: {$float}; margin-top: 10px; text-align: center; width: {$itemwidth}%; } #{$selector} img { border: 2px solid #cfcfcf; } #{$selector} .gallery-caption { margin-left: 0; } /* see gallery_shortcode() in wp-includes/media.php */ </style>"; $size_class = sanitize_html_class( $size ); $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>"; $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div ); $i = 0; foreach ( $attachments as $id => $attachment ) { if ( ! empty( $link ) && 'file' === $link ) $image_output = wp_get_attachment_link( $id, $size, false, false ); elseif ( ! empty( $link ) && 'none' === $link ) $image_output = wp_get_attachment_image( $id, $size, false ); else $image_output = wp_get_attachment_link( $id, $size, true, false ); $image_meta = wp_get_attachment_metadata( $id ); $orientation = ''; if ( isset( $image_meta['height'], $image_meta['width'] ) ) $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape'; $title = wptexturize($attachment->post_title); // ★タイトル(またはバナー)を代入 $output .= "<{$itemtag} class='gallery-item'>"; // ★3行下でアイコン用タグの中にタイトルを入れています! $output .= " <{$icontag} class='gallery-icon {$orientation}'> <{$titletag} class='gallery-title'>$title</{$titletag}> $image_output </{$icontag}>"; if ( $captiontag && trim($attachment->post_excerpt) ) { $output .= " <{$captiontag} class='wp-caption-text gallery-caption'> " . wptexturize($attachment->post_excerpt) . " </{$captiontag}>"; } $output .= "</{$itemtag}>"; if ( $columns > 0 && ++$i % $columns == 0 ) $output .= '<br style="clear: both" />'; } $output .= " <br style='clear: both;' /> </div>\n"; return $output; }
フォーラム: 使い方全般
返信が含まれるトピック: 投稿記事内の指定文章にタグ付けをする記事タイトル(1)
<p data-tag="A">あいうえお</p> <p data-tag="B">かきくけこ</p>
記事タイトル(2)
<p data-tag="A">あいうえお</p>
とかなんとか自分ルールのタグ記法で記事を書いて、別ページで以下のように書けばOK。
$tag = 'data-tag="A"'; // タグ(A)を指定 echo '<div>タグ(A)</div>'; $tagged_posts = get_posts( array( 's' => $tag, 'posts_per_page' => -1, // 全件取得 ) ); foreach ( $tagged_posts as $post ) { setup_postdata( $post ); $content = apply_filters( 'the_content', get_the_content() ); if ( preg_match_all( "/<p\s+$tag\s*>(.+?)<\/p>/", $content, $matches ) ) { $title = get_the_title(); foreach ( $matches[1] as $match ) { echo "<div> $match − $title</div>"; } } } wp_reset_postdata();
マークアップはお好みで◎
フォーラム: 使い方全般
返信が含まれるトピック: singleページでのカテゴリ表示マークアップはお好みで◎
$cats = get_the_terms( $post, 'category' ); $ids = wp_list_pluck( $cats, 'term_id' ); foreach( $cats as $cat ) { if( ! $cat->parent ) { $url = get_term_link( $cat ); echo "<a href=\"$url\"><b>{$cat->name}</b></a><br>"; // 大カテゴリ $children = get_terms( 'category', array( 'child_of' => $cat->term_id, 'hide_empty' => false, // またはお好みで true ) ); foreach ( $children as $child ) { $url = get_term_link( $child ); if ( in_array( $child->term_id, $ids ) ) { echo "<a href=\"$url\">_<b>{$child->name}</b></a><br>"; // 記事が属する小カテゴリ } else { echo "<a href=\"$url\">_{$child->name}</a><br>"; // 記事が属さない小カテゴリ } } } }
フォーラム: 使い方全般
返信が含まれるトピック: カスタム分類とquert_posts()の引数についてカスタム投稿のカテゴリー分類の一覧を出力するために
ループ処理のquert_posts()を使う時、query_posts() を使わなくても taxonomy.php で一覧表示できます。
なので、
<?php query_posts('posts_per_page=5&order=DESC&' . $query_string); ?>
は不要です。
「カスタム分類一覧では1ページの表示件数を5件にしたい」場合は、
下記のようなコードをテーマの functions.php に貼付ければOK◎
#未検証add_action( 'pre_get_posts', 'my_pre_get_posts' ); function my_pre_get_posts( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } if ( is_tax() ) { $query->set( 'posts_per_page', 5 ); } }
フォーラム: 使い方全般
返信が含まれるトピック: カスタムフィールドによるアーカイブリストについて>一覧表示はされますが(値の数字のみ)
このリンクの URL(1個だけで良いです)と
WordPress のバージョンと
WordPress 管理画面[設定|パーマリンク設定|共通設定]の値を
お知らせください。フォーラム: 使い方全般
返信が含まれるトピック: get_category_linkでのURLに「category」が入る是非はともかく
WP No Category Base
でお望みのことはできます。フォーラム: 使い方全般
返信が含まれるトピック: サイト内検索で、カスタムタクソノミーの記事のみ出力検索対象の記事を glossary が glossary_category, glossary_tag の値のもののみに制限する、ということなら以下でOK。
add_action( 'pre_get_posts', 'my_pre_get_posts' ); function my_pre_get_posts( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_search ) { $query->set( 'tax_query', array( 'taxonomy' => 'glossary', 'terms' => array( 'glossary_category', 'glossary_tag' ), 'field' => 'slug', ) ); } }
タクソノミー glossary
ターム glossary_category
ターム glossary_tagなんとか_category, なんとか_tag はタームではなくてタクソノミーに付けるスラッグにする方が何かと良い気がします。
コンテンツ設計的には、
カスタム投稿タイプ glossary
階層ありタクソノミー glossary_category
階層なしタクソノミー glossary_tag
がよくある感じです。フォーラム: 使い方全般
返信が含まれるトピック: single.phpにエラーがでてしまい対処方法がわかりません。catch_that_image() を以下の jim912 さんの返信のように変更されると良い気がします。
[解決済み] 記事中一番最初の画像を自動表示するが画像なしの時はHTML非表示にしたいフォーラム: 使い方全般
返信が含まれるトピック: タームリスト一覧の投稿数取得についてタームIDで記事数取得:
get_term( $chosen_id, 'タクソノミースラッグ' )->count
タームスラッグで記事数取得:
get_term_by( 'slug', 'タームスラッグ', 'タクソノミースラッグ' )->count
参照:codex.wordpress.org
get_term
get_term_byフォーラム: 使い方全般
返信が含まれるトピック: サイト内検索で、カスタムタクソノミーの記事のみ出力#未検証
テーマフォルダの functions.php に以下のようなコードを貼付ければOK。
add_action( 'pre_get_posts', 'my_pre_get_posts' ); function my_pre_get_posts( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_search ) { if ( '任意のキーワード' == $query->get( 's' ) ) { $query->set( 'tax_query', array( 'taxonomy' => '任意のタクソノミースラッグ', 'terms' => array( '任意のターム1', '任意のターム2' ), 'field' => 'slug', ) ); } } }
フォーラム: マルチサイト
返信が含まれるトピック: 登録ユーザーがそれぞれの個別ページにのみアクセス購読者はユーザー名と同じスラッグの記事へリダイレクトさせる、なら以下でOK。
<?php // 管理画面へのアクセス時に専用ページ(記事)へリダイレクトする add_action( 'auth_redirect', 'my_auth_redirect' ); function my_auth_redirect( $user_id ) { $user = get_user_by( 'id', $user_id ); if ( isset( $user->caps['subscriber'] ) && $user->caps['subscriber'] ) wp_redirect( home_url( $user->user_login ) ); } // 公開側へのアクセス時に専用ページ(記事)へリダイレクトする add_filter( 'request', 'my_request' ); function my_request( $query_vars ) { if ( ! is_admin() ) { auth_redirect(); // ログイン必須にする if ( current_user_can( 'subscriber') ) $query_vars = array( 'name' => $GLOBALS['current_user']->user_login ); } return $query_vars; } ?>
メンバー系プラグインを使うならコンフリクトにご注意。
フォーラム: 使い方全般
返信が含まれるトピック: カテゴリをカスタムフィールドで並び替えしたいフォーラム: 使い方全般
返信が含まれるトピック: 同カテゴリ記事一覧の出力順を「今表示している記事」からにしたいテーマフォルダの functions.php に以下をコピペ:
<?php function my_posts_where( $where, $query ) { global $wpdb, $post; if ( 'DESC' == $query->query_vars['order'] ) $where .= $wpdb->prepare( " AND $wpdb->posts.post_date < %s", $post->post_date ); else $where .= $wpdb->prepare( " AND $wpdb->posts.post_date > %s", $post->post_date ); return $where; } function my_get_posts( $args ) { global $post; $args = wp_parse_args( $args, array( 'suppress_filters' => false, 'tax_query' => array( array( 'taxonomy' => 'category', 'terms' => wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) ) ), ), ) ); add_filter( 'posts_where', 'my_posts_where', 10, 2 ); $posts = get_posts( $args ); remove_filter( 'posts_where', 'my_posts_where', 10, 2 ); return $posts; } ?>
記事一覧を表示するところのコード例:
<?php $ps = my_get_posts( 'order=ASC&posts_per_page=-1' ); // これでお求めの記事を取得 foreach ( $ps as $post ) { setup_postdata( $post ); ?><p><?php the_title(); the_time( ' Y.m.d. H.i.s' ) ?></p><?php } wp_reset_postdata(); ?>
※コードを貼付ける時は複数の
<?php
と?>
が入れ子にならないように注意。フォーラム: 使い方全般
返信が含まれるトピック: カスタムフィールドでチェックボックスを使用したい〜〜略〜〜 $area = $_POST["area"] ; 〜〜略〜〜 if($post_id != '0') { add_post_meta($post_id, 'groupname', $groupname); foreach ( (array) $area as $a ) add_post_meta($post_id, 'area', $a, false); }
かしら。
フォーラム: テーマ
返信が含まれるトピック: ギャラリーのショートコード「exclude」が効かない#WP 3.4.1 で検証
投稿画面の本文に
[gallery columns='3' exclude='4,5']
*ように* 入力して、指定したID の画像がギャラリーから除かれるのを確認しました。content-gallery.php の
<div class="post-thumbnail"><?php echo do_shortcode("[gallery columns='3']"); ?></div>
の箇所では exclude 指定していないので除外はされません。
<div id="info_content" class="main-content"> <?php the_content(); ?> </div>
の箇所で、投稿本文内に exclude 指定の gallery ショートコードがあれば
指定したID の画像がギャラリーから除かれて表示されます。解決していなければ「解決済」にしなくて良いと思います◎