だから私は私たちが内科と呼ぶ2の特定のカテゴリーのすべての画像を表示することができるように内蔵のギャラリーショートコードを追加または変更しようとしていますが、私がしたい場合も同じ機能を持つことができます異なるカテゴリーのすべての画像をつかむには、4というIDを持つダウンタウンと呼ばれることができます。これでどこから始められますか?これが私が編集しようとしている組み込みのショートコードのコードです。
add_shortcode('gallery', 'gallery_shortcode');
/**
* Builds the Gallery shortcode output.
*
* This implements the functionality of the Gallery Shortcode for displaying
* WordPress images on a post.
*
* @since 2.5.0
*
* @staticvar int $instance
*
* @param array $attr {
* Attributes of the gallery shortcode.
*
* @type string $order Order of the images in the gallery. Default 'ASC'. Accepts 'ASC', 'DESC'.
* @type string $orderby The field to use when ordering the images. Default 'menu_order ID'.
* Accepts any valid SQL ORDERBY statement.
* @type int $id Post ID.
* @type string $itemtag HTML tag to use for each image in the gallery.
* Default 'dl', or 'figure' when the theme registers HTML5 gallery support.
* @type string $icontag HTML tag to use for each image's icon.
* Default 'dt', or 'div' when the theme registers HTML5 gallery support.
* @type string $captiontag HTML tag to use for each image's caption.
* Default 'dd', or 'figcaption' when the theme registers HTML5 gallery support.
* @type int $columns Number of columns of images to display. Default 3.
* @type string|array $size Size of the images to display. Accepts any valid image size, or an array of width
* and height values in pixels (in that order). Default 'thumbnail'.
* @type string $ids A comma-separated list of IDs of attachments to display. Default empty.
* @type string $include A comma-separated list of IDs of attachments to include. Default empty.
* @type string $exclude A comma-separated list of IDs of attachments to exclude. Default empty.
* @type string $link What to link each image to. Default empty (links to the attachment page).
* Accepts 'file', 'none'.
* }
* @return string HTML content to display gallery.
*/
function gallery_shortcode( $attr ) {
$post = get_post();
static $instance = 0;
$instance++;
if ( ! empty( $attr['ids'] ) ) {
// 'ids' is explicitly ordered, unless you specify otherwise.
if ( empty( $attr['orderby'] ) ) {
$attr['orderby'] = 'post__in';
}
$attr['include'] = $attr['ids'];
}
/**
* Filters the default gallery shortcode output.
*
* If the filtered output isn't empty, it will be used instead of generating
* the default gallery template.
*
* @since 2.5.0
* @since 4.2.0 The `$instance` parameter was added.
*
* @see gallery_shortcode()
*
* @param string $output The gallery output. Default empty.
* @param array $attr Attributes of the gallery shortcode.
* @param int $instance Unique numeric ID of this gallery shortcode instance.
*/
$output = apply_filters( 'post_gallery', '', $attr, $instance );
if ( $output != '' ) {
return $output;
}
$html5 = current_theme_supports( 'html5', 'gallery' );
$atts = shortcode_atts( array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post ? $post->ID : 0,
'itemtag' => $html5 ? 'figure' : 'dl',
'icontag' => $html5 ? 'div' : 'dt',
'captiontag' => $html5 ? 'figcaption' : 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => '',
'link' => ''
), $attr, 'gallery' );
$id = intval( $atts['id'] );
if ( ! empty( $atts['include'] ) ) {
$_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( ! empty( $atts['exclude'] ) ) {
$attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
} else {
$attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
}
if ( empty( $attachments ) ) {
return '';
}
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment ) {
$output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n";
}
return $output;
}
$itemtag = tag_escape( $atts['itemtag'] );
$captiontag = tag_escape( $atts['captiontag'] );
$icontag = tag_escape( $atts['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( $atts['columns'] );
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = '';
そのため、これの目的は[gallery category = "2"]や[gallery id = "2"]、あるいは特定のカテゴリ内のすべての画像を表示するようにすることです。
編集したいのですが、[gallery category = "emergency-medicine"]を付けて、すべての画像または最新のトップ20を投稿に含めることができます。そうでない場合は、[gallery post_type = "galleryになります。 "category =" medicine "]そして私がこれらのカスタム投稿タイプ/カテゴリーを作るために使うものはプラグインToolSet Typesにあります
function gallery_shortcode( $attr ) {
$post = get_post();
static $instance = 0;
$instance++;
if ( ! empty( $attr['ids'] ) ) {
// 'ids' is explicitly ordered, unless you specify otherwise.
if ( empty( $attr['orderby'] ) ) {
$attr['orderby'] = 'post__in';
}
$attr['include'] = $attr['ids'];
}
/**
* Filters the default gallery shortcode output.
*
* If the filtered output isn't empty, it will be used instead of generating
* the default gallery template.
*
* @since 2.5.0
* @since 4.2.0 The `$instance` parameter was added.
*
* @see gallery_shortcode()
*
* @param string $output The gallery output. Default empty.
* @param array $attr Attributes of the gallery shortcode.
* @param int $instance Unique numeric ID of this gallery shortcode instance.
*/
$output = apply_filters( 'post_gallery', '', $attr, $instance );
if ( $output != '' ) {
return $output;
}
$html5 = current_theme_supports( 'html5', 'gallery' );
$atts = shortcode_atts( array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => null,
'itemtag' => $html5 ? 'figure' : 'dl',
'icontag' => $html5 ? 'div' : 'dt',
'captiontag' => $html5 ? 'figcaption' : 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => '',
'link' => '',
'category' => null,
'post_type' => 'uc-general-gallery',
'img' => null
), $attr, 'gallery' );
$ids = array();
ob_start();
if ($atts['id'] != null ) {
$id_str = preg_replace('/\s+/', '', $atts['id']); // strip whitespace
$ids = explode(',', $id_str);
?>
<?php foreach($ids as $id): ?>
<?php if(is_numeric($id)): ?>
<?php endif; ?>
<?php endforeach; ?>
<?php
} else if($atts['category'] != null) {
$cat_str = preg_replace('/\s+/', '', $atts['category']); // strip whitespace
$args = array(
'post_type' => $atts['post_type'],
'order' => 'DESC',
'category_name' => $cat_str,
'posts_per_page' => 999
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) :
?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php endwhile; ?>
<?php
endif;
}
if ( ! empty( $atts['include'] ) ) {
$_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( ! empty( $atts['exclude'] ) ) {
$attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
} else {
$attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
}
if ( empty( $attachments ) ) {
return '';
}
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment ) {
$output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n";
}
return $output;
}
$itemtag = tag_escape( $atts['itemtag'] );
$captiontag = tag_escape( $atts['captiontag'] );
$icontag = tag_escape( $atts['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( $atts['columns'] );
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = '';
だからこれは私がそれを動かすために私がしたことです(ちょっと醜いけれども)今私は入力することができます[gallery post_type = "uc-一般ギャラリー" category = "人 - 住民"]または単に[gallery category = "etc"]画像を取得して表示しますが、ビジュアルエディタではアイテムが見つからないと表示されますが、実際に投稿を表示するとページに表示されるので、今度はモバイルに対応する必要があります。ユーザーが画面を縮小すると、スライダーまたはスライドショーに変わります。
編集する
気にしないで!私のコードはバグが多く、カテゴリ別ではなく追加されたすべての画像が表示されます。これを修正する簡単な方法はありますか?
税照会で別のWP_Query
を実行して、IDを収集してギャラリーのショートコードのinclude
属性に入れるには、さまざまな方法があります。
セカンダリのWP_Query
を実行せずに別のアプローチを試してみましょう。
以下のカスタムギャラリーのショートコード属性を紹介します。
[gallery taxonomy="media_category" term="people"]
これはpost_gallery
、shortcode_atts_gallery
フィルター、そしてpre_get_posts
アクションを通してそれをサポートするデモプラグインです:
<?php
/**
* Plugin Name: Native Gallery Shortcode With Taxonomy And Term Filter Support
* Description: Add support for [gallery taxonomy='some-taxonomy' term="some-term"]
* Plugin URI: http://wordpress.stackexchange.com/a/257801/26350
*/
namespace WPSE\Q257743;
class Gallery
{
private $term;
private $taxonomy;
public function init()
{
add_filter( 'post_gallery', [$this, 'post_gallery'], 10, 2 );
}
public function post_gallery( $html, $attr )
{
// Check user input for term and taxonomy
if( empty( $attr['term'] ) || empty( $attr['taxonomy'] ) )
return $html;
// Validate taxonomy
if( ! taxonomy_exists( $attr['taxonomy'] ) )
return esc_html__( "Taxonomy doesn't exists!", 'wpse-257743' );
// Validate term
if( ! term_exists( $attr['term'] , $attr['taxonomy'] ) )
return esc_html__( "Term doesn't exists!", 'wpse-257743' );
$this->term = $attr['term'];
$this->taxonomy = $attr['taxonomy'];
// Activate customization
add_filter( 'shortcode_atts_gallery', [$this, 'gallery_atts'] );
add_action( 'pre_get_posts', [$this, 'pre_get_posts'] );
return $html;
}
public function gallery_atts( $pairs )
{
// Set include to a temporary non empty value (that becomes 0)
$pairs['include'] = ' ';
// Only run once per gallery
remove_filter( current_filter(), __FUNCTION__ );
return $pairs;
}
public function pre_get_posts( \WP_Query $q )
{
// Make sure it's attachments query
if( 'attachment' !== $q->get( 'post_type' ) )
return;
$post__in = $q->get( 'post__in' );
// Target queries with post__in (include)
if ( empty( $post__in ) )
return;
// Check for our temporary 0 value
if( 1 === count( $post__in ) && 0 != $post__in[0] )
return;
$q->set( 'post__in', null );
// Set the taxonomy query:
$q->set( 'tax_query',
[
[
'taxonomy' => $this->taxonomy,
'field' => 'slug',
'terms' => $this->term,
]
]
);
// Adjust the number of images to e.g. max 100
$q->set( 'posts_per_page', 100 );
// Only run once per gallery
remove_action( current_action(), __FUNCTION__ );
}
}
add_action( 'init', [new Gallery, 'init' ] );
ここでは100画像という制限を追加しました。
あなたがそれをあなたのニーズに合わせてさらにテストして調整できることを望みます。複数の用語をサポートできるように変更してください。