贴在帖子上的图片库

时间:2012-04-10 作者:porton

我有几个“类别”和所谓的“项目”(内容类型为“项目”的帖子)。每个项目对应一个类别。

我称之为“画廊”,是几个图像,每个图像都附有几个字符串(描述、作者)。

每个类别和每个项目都应附加一个图库。如果未将库附加到项目,则应在项目页面上显示该类别的库。

(请注意,我在这里所称的“类别”可能与WordPress手册中所称的“类别”相同,也可能不同。)

如何在WordPress中实现?

1 个回复
SO网友:moraleida

如果计划使用WP的内置图像自定义字段,请在single-project.php (假设仅在单个项目页面上调用库):

// this will get all images attached to that specific post
$imagequery = new WP_Query(
    array(
    \'post_type\' => \'attachment\',
    \'post_mime_type\' => \'image/jpeg\',
    \'post_parent\' => $post->ID
    ));

if ($imagequery->have_posts()) {
    // loop to present those images here
}
else { // if no images attached to that post
    // fetch all posts from the taxonomy
    $catquery = get_posts(
        array(
        \'post_type\' => \'projects\',
        \'numberposts\' => -1,
        \'yourtaxonomyname\' => \'yourtermname\' // specify the \'category\' here
        ));

    $postids = array();

    if ($catquery) {
        foreach ( $catquery as $onepost ) {
        array_push($postids, $onepost->ID); // make array of post ids
}

    $newimagequery = new WP_Query( // then query these ids to get their images
    array(
    \'post_type\' => \'attachment\',
    \'post_mime_type\' => \'image/jpeg\',
    \'post__in\' => $postids
    ));

if ($newimagequery->have_posts()) {
    // loop to present images from other posts here
}


}

结束

相关推荐

Overriding Gallery Margin

继续构建免费主题。使用图库CSS。Wordpress会自动将左边距添加到库中。示例:http://themeforward.com/demo2/features/gallery-2/ 那么,我该如何摆脱这种令人讨厌的利润呢?/* Gallery */ .gallery { margin:30px auto auto; text-align:center } .gallery-item { float:left; margin-top:10px;&#x