我正在尝试学习如何编写php代码,以便在WordPress中自定义图像库(以及其他自定义)。
我的代码非常适合样式化的库页面,but having a difficult time trying to figure out how to get the title and alt attributes of the images within the WP gallery (我猜这是因为图片不被视为帖子的附件,因为它们是在gallery功能中)。
我想使用WP gallery,因为我想为galleries使用WP内置的功能。
非常感谢您的帮助。。。这是最愚蠢的做事方式还是什么?
Note: 在不使用WP gallery的情况下,尝试编辑页面上的图像时,get\\u attachment或get\\u children也会带来灾难,因为从页面中删除的旧附件或子级仍然会显示)。
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="gallery-wrapper three-cols clearfix">
<?php // image gallery content
if( has_shortcode( $post->post_content, \'gallery\' ) ) {
// Add a filter for full gallery size:
add_filter( \'shortcode_atts_gallery\',\'wpse_full_size_gallery\' );
$gallery = get_post_gallery_images( $post->ID );
// Remove the filter:
remove_filter( \'shortcode_atts_gallery\',\'wpse_full_size_gallery\' );
foreach ( $gallery as $image_url ) {
$alt = get_post_meta($attachment->ID, \'_wp_attachment_image_alt\', true);
$image_title = $attachment->post_title;
echo \'<div class="one-third columns gallery-item">\';
echo \'<div class="item-picture" data-type="image">\';
echo \'<img src="\' . $image_url . \'">\' ;
echo \'<div class="image-overlay">\';
echo \'<a href="\' . $image_url . \'" data-rel="prettyPhoto[gallery]"><span class="zoom"></span></a>\';
echo \'</div>\';
echo \'<span class="item-label">\' . $image_title . \'</span>\';
echo \'</div>\';
echo \'</div>\';
}
}
?>
</div>
<!-- article -->
<article class="full-width columns" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<br class="clear">
<?php edit_post_link(); ?>
</article>
<!-- /article -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( \'Sorry, nothing to display.\', \'html5blank\' ); ?></h2>
</article>
<!-- /article -->
<?php endif; ?>
最合适的回答,由SO网友:Jan Beck 整理而成
一点背景:WP将附件存储在与POST相同的数据库表中。因此,表行对应于媒体编辑模式的字段:
get_post_gallery_images
将返回图像的URL,但不返回数据库中的实际数据。您可以进行反向查询
look for posts that contain the image URL 但这将比必要的更加困难。
而是使用get_post_gallery 作用这个实际上是get_post_gallery_images
也是,但也返回数组中附件的ID。使用这些ID从数据库中获取信息get_posts
:
<?php
$gallery = get_post_gallery( get_the_ID(), false );
$args = array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => -1,
\'post_status\' => \'any\',
\'post__in\' => explode( \',\', $gallery[\'ids\'] )
);
$attachments = get_posts( $args );
foreach ( $attachments as $attachment ) {
$image_alt = get_post_meta( $attachment->ID, \'_wp_attachment_image_alt\', true);
if ( empty( $image_alt )) {
$image_alt = $attachment->post_title;
}
if ( empty( $image_alt )) {
$image_alt = $attachment->post_excerpt;
}
$image_title = $attachment->post_title;
$image_url = wp_get_attachment_image_src( $attachment->ID, \'full\' );
echo \'<div class="one-third columns gallery-item">\';
echo \'<div class="item-picture" data-type="image">\';
echo \'<img src="\' . $image_url[0] . \'" alt="\'. $image_alt .\'">\' ;
echo \'<div class="image-overlay">\';
echo \'<a href="\' . $image_url[0] . \'" data-rel="prettyPhoto[gallery]">
<span class="zoom"></span></a>\';
echo \'</div>\';
echo \'<span class="item-label">\' . $image_title . \'</span>\';
echo \'</div>\';
echo \'</div>\';
} ?>
脚本在中查找alt标记信息
_wp_attachment_image_alt
,
post_title
和
post_excerpt
直到它找到一个值。