将WordPress图库连接到自定义类别(分类)

时间:2014-04-13 作者:vega

我想使用Wordpress图库在一个图库中显示一个类别的所有图片(在固定页面上),我不必通过手动插入图片来一直更新这些图片。

因此,我使用此功能在媒体中添加了分类功能:

function is_add_categories_to_attachments() {
register_taxonomy_for_object_type( \'category\', \'attachment\' );
}
add_action( \'init\' , \'is_add_categories_to_attachments\' );
太好了,成功了。在“媒体”菜单中,我现在可以添加任意多个类别。在我的例子中,我制作了三个,分别命名为alpha、beta和gamma。

然后我想在一个固定页面上发布一个图库,其中包含一个类别:

[gallery type="squares" category="alpha" order="DESC" orderby="ID" link="file"]
但这不起作用,我不得不在孩子的函数中使用post\\u gallery过滤器。php,因此我可以在那里更改代码,但我完全不知道如何将类别识别输入gallery\\u快捷方式:

function is_gallery($output, $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\'];
}

// Allow plugins/themes to override the default gallery template.
$output = apply_filters(\'post_gallery\', \'\', $attr);
if ( $output != \'\' )
    return $output;

// 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\',
    \'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\';

    $output .= "<{$itemtag} class=\'gallery-item\'>";
    $output .= "
        <{$icontag} class=\'gallery-icon {$orientation}\'>
            $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;
}
add_filter("post_gallery", "is_gallery",10,2);
您能告诉我如何将类别功能插入galley\\u快捷码吗?

提前谢谢你。

那么这是正确的吗?

function is_gallery($output, $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\'];
}

// Allow plugins/themes to override the default gallery template.
$output = apply_filters(\'post_gallery\', \'\', $attr);
if ( $output != \'\' )
return $output;

// 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\',
\'icontag\'    => \'dt\',
\'captiontag\' => \'dd\',
\'columns\'    => 3,
\'size\'       => \'thumbnail\',
\'include\'    => \'\',
\'exclude\'    => \'\',
\'link\'       => \'\'
), $attr, \'gallery\'));

$id = intval($id);
if ( \'RAND\' == $order )
$orderby = \'none\';

$beta_attachments = new WP_Query( array(
\'post_type\' => \'attachment\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => 0,
\'category_name\' => \'beta\', // note: use category SLUG
) );

$beta_id_array = array();
foreach ( $beta_attachments as $beta ) {
$beta_id_array[] = $beta->ID;
}
$beta_ids = implode( \',\', $beta_id_array );

$gamma_attachments = new WP_Query( array(
\'post_type\' => \'attachment\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => 0,
\'category_name\' => \'gamma\', // note: use category SLUG
) );

$gamma_id_array = array();
foreach ( $gamma_attachments as $gamma ) {
$gamma_id_array[] = $gamma->ID;
}
$gamma_ids = implode( \',\', $gamma_id_array );

$alpha_attachments = new WP_Query( array(
\'post_type\' => \'attachment\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => 0,
\'category_name\' => \'alpha\', // note: use category SLUG
) );

$alpha_id_array = array();
foreach ( $alpha_attachments as $alpha ) {
$alpha_id_array[] = $alpha->ID;
}
$alpha_ids = implode( \',\', $alpha_id_array );

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\';

$output .= "<{$itemtag} class=\'gallery-item\'>";
$output .= "
    <{$icontag} class=\'gallery-icon {$orientation}\'>
        $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;
}
add_filter("post_gallery", "is_gallery",10,2);
更新hi芯片,

我现在理解了你的想法/解决方案,并尝试了它(好吧,多亏了你才把它复制过来)。我认为这个逻辑是可行的,但在实践中却行不通。我使用了这样的代码,它没有“看到”类别。因此,它只显示一个空内容(当然是导航下方)。

人php:

 <?php
 /**
 * Template Name: People Gallery
 */

 get_header(); ?>

<?php

$people_attachments = new WP_Query( array(
\'post_type\' => \'attachment\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => 0,
\'category_name\' => \'people\', // note: use category SLUG
) );

$people_id_array = array();

if ( $people_attachments->have_posts() ) : while ( $people_attachments->have_posts() ) : $people_attachments->the_post();

$people_id_array[] = get_the_ID();

endwhile; endif;
// Important!
wp_reset_postdata();

$people_ids = implode( \',\', $people_id_array );

echo do_shortcode( \'[gallery link="file" columns="5" order="DESC" orderby="ID" include="\' . $people_ids . \'"]\' );

?>

<?php var_dump( $people_id_array ); ?>

<?php // get_sidebar(); ?>

<?php get_footer(); ?>
正在工作

1 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

如果这是您的目标,请编辑:

我想使用Wordpress图库在一个图库中显示一个类别的所有图片(在固定页面上),我不必通过手动插入图片来一直更新这些图片。

那么最简单的方法就是通过custom page template.

创建模板,复制page.php 模板文件,并将其命名template-alpha-gallery.php

在顶部添加以下内容:

 <?php
 /**
  * Template Name: Alpha Gallery
  */
保持<?php get_header(); ?><?php get_footer(); ?>, 以及您需要保留的任何HTML标记。删除现有循环代码。

注意:模板HTML/循环标记非常依赖于主题。您需要根据当前主题修改这些说明。

在循环代码的位置添加以下短代码输出:

 $alpha_attachments = new WP_Query( array(
     \'post_type\' => \'attachment\',
     \'post_status\' => \'inherit\',
     \'posts_per_page\' => 0,
     \'category_name\' => \'alpha\', // note: use category SLUG
 ) );

 $alpha_id_array = array();

 foreach ( $alpha_attachments->posts as $alpha ) {
     $alpha_id_array[] = $alpha->ID;
 }

 $alpha_ids = implode( \',\', $alpha_id_array );

 echo do_shortcode( \'[gallery ids="\' . $gallery_ids . \'"]\' );
创建一个新的静态页面,并为其分配;Alpha画廊;页面模板

享受你的画廊

原始答案[gallery] shortcode不接受“a”;“类别”;然而,参数,it does include an \'include\' parameter, 接受以逗号分隔的附件ID列表的:

[gallery include="1,2,3"]
因此,您可以使用include 列出具有指定类别的所有附件的ID。

首先,查询正确的附件:

$alpha_attachments = new WP_Query( array(
    \'post_type\' => \'attachment\',
    \'post_status\' => \'inherit\',
    \'posts_per_page\' => 0,
    \'category_name\' => \'alpha\', // note: use category SLUG
) );
然后,循环并获取ID:

编辑你需要循环浏览结果。而不是这样:

$alpha_id_array = array();

foreach ( $alpha_attachments as $alpha ) {
    $alpha_id_array[] = $alpha->ID;
}
。。。执行以下操作:

$alpha_id_array = array();

if ( $alpha_attachments->have_posts() ) : while ( $alpha_attachments->have_posts() ) : $alpha_attachments->the_post();

    $alpha_id_array[] = get_the_ID();

endwhile; endif;
// Important!
wp_reset_postdata();
然后,格式化字符串:

$alpha_ids = implode( \',\', $alpha_id_array );
然后,将该字符串传递给短代码。如果您使用的是自定义页面模板,可以这样做:

echo do_shortcode( \'[gallery include="\' . $alpha_ids . \'"]\' );
应该就是这样。

编辑当我单击文件时,它会加载最大的可用大小。你能告诉我,链接的创建或表达式是在什么地方打开的吗。我创建了自定义尺寸vor示例“;“厨房尺寸”;或“或”;博客大小“;,但我不知道如何在这个画廊里使用它们。

这个[gallery] shortcode has a size parameter. 只需将其添加到输出中;例如,对于;“中等”;图像:

echo do_shortcode( \'[gallery size="medium" include="\' . $alpha_ids . \'"]\' );

结束

相关推荐

使用Get_Categories显示类别的图像,或显示任何子帖子中的图像

我正在使用get\\u categories列出父类别的子类别。我想使用get\\u categories输出将图像添加到子类别。我可以从我正在使用get\\u categories的类别的子类别(即父类别的子类别)的任何帖子中获取特色图像。我不想显示任何其他孙儿信息,只想从每组类别的孩子中获得一张特色图片我当前使用的代码是$args = array(\'child_of\' => 1 ); $categories = get_categories($args); forea