我正在处理一个自定义模板文件,该文件可以提取媒体库中具有特定标记的所有图像。我在社区的帮助下完成了这项工作,它还显示了与图像相关的帖子的标题。我需要拉标签后,图像作为类名附加到容器上。这是我的代码:
<?php
$args = array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => -1,
\'post_status\' => \'any\',
\'post_parent\' => null,
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_tag\',
\'field\' => \'slug\',
\'terms\' => \'logo\'
)
)
);
$images = get_posts( $args );
if ( $images ) {
foreach ( $images as $image ) {
// Get the parent post ID
$parent_id = $image->post_parent;
// Get the parent post Title
$parent_title = get_the_title( $parent_id );
// Get the parent post permalink
$parent_permalink = get_permalink( $parent_id );
// Get image caption
$logoimg = wp_get_attachment_image( $image->ID, \'Work Gallery\' );
$posttags = get_the_tags();
if ($posttags) {
foreach ($posttags as $tag) {
str_replace(\'-\',\'_\',$tag->slug . \' \');
}
}
echo \'<aside class="work_item" data-id="id-\' . get_the_ID() . \'" data-type="\' . $posttags . \'">\';
echo \'<ul>\';
echo \'<li>\';
echo \'<div class="img_wrap">\';
echo $logoimg;
echo \'</div>\';
echo \'<ul class="work_meta">\';
echo \'<li class="work_title">\';
echo \' \' . $parent_title . \' \';
echo \'</li>\';
echo \'<li class="work_item_content">\';
echo \' \' . get_post( get_post_thumbnail_id($post->ID) )->post_excerpt . \' \';
echo \'</li>\';
echo \'</ul>\';
echo \'<ul class="work_features">\';
echo \'<li class="lightbox">\';
$attachment_id = $post->ID; // dynamic attachment ID
$image_attributes = wp_get_attachment_image_src( $attachment_id, \'full\' ); // returns an array
echo \'<a class="work_lb tooltip" data-id="\' . get_the_ID() . \'" title="view in lightbox" href="\' . $image_attributes[3] . \'" rel="prettyPhoto[mixed]">\';
echo \'Lightbox\';
echo \'</a>\';
echo \'</li>\';
echo \'</ul>\';
echo \'</li>\';
echo \'</ul>\';
echo \'</aside>\';
}
wp_reset_postdata();
}
?>
该行:echo \'<aside class="work_item" data-id="id-\' . get_the_ID() . \'" data-type="\' . $posttags . \'">\';
是我试图获取posts标签的地方(例如:hvac、承包商)。在另一个自定义模板文件上,我使用以下代码:data-type="<?php $posttags = get_the_tags(); if ($posttags) { foreach ($posttags as $tag) { echo str_replace(\'-\',\'_\',$tag->slug . \' \'); } } ?> <?php $posttags = get_the_terms($post->ID, \'type\'); if ($posttags) { foreach($posttags as $tag) { echo str_replace(\'-\',\'_\',$tag->slug . \'\'); } } ?>"
这在该页面上运行得很好,它提取了posts标签并去掉了空格和字符,我想在上面发布的新模板上应用相同的代码。当然,非常感谢您的帮助!