0

我需要使用 redux 获取我在图库中上传的所有图像的标题。

这是带有 redux 框架的主题选项中的代码:https ://docs.reduxframework.com/core/fields/gallery/

我尝试使用此代码在 WordPress 网站上显示标题(仅用于测试):

<?php 
    $attachmentIds = explode(',', $redux_demo['opt-gallery']);
    foreach($attachmentIds as $attachmentId): 
        $metaAttachment = wp_get_attachment_metadata( $attachmentId );
        echo '<pre>';
        print_r( $metaAttachment );
        echo '</pre>';
?>

但是,这段代码返回给我 [caption] => (empty)

    大批(
    [宽度] => 330
    [身高] => 180
    [文件] => 2015/10/330x1805.jpg
    [大小] => 数组
        (
            [缩略图] => 数组
                (
                    [文件] => 330x1805-150x150.jpg
                    [宽度] => 150
                    [身高] => 150
                    [mime 类型] => 图像/jpeg
                )

            [中] => 数组
                (
                    [文件] => 330x1805-300x164.jpg
                    [宽度] => 300
                    [身高] => 164
                    [mime 类型] => 图像/jpeg
                )

        )

    [image_meta] => 数组
        (
            [光圈] => 0
            [信用] =>
            [相机] =>
            [标题] =>
            [created_timestamp] => 0
            [版权] =>
            [焦距] => 0
            [iso] => 0
            [快门速度] => 0
            [标题] =>
            [方向] => 0
        )

)

标题字段有值,但似乎redux没有保存信息或我的代码有误?

4

1 回答 1

0

这是我正在寻找的解决方案:

在functions.php文件中:

function wp_get_attachment( $attachment_id ) {
    $attachment = get_post( $attachment_id );
    return array(
        'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
        'caption' => $attachment->post_excerpt,
        'description' => $attachment->post_content,
        'href' => get_permalink( $attachment->ID ),
        'src' => $attachment->guid,
        'title' => $attachment->post_title
    );
}

在您的模板文件中:

<?php 
    global $redux_demo;
    $myGalleryIDs = explode(',', $redux_demo['opt-gallery']);
    foreach($myGalleryIDs as $myPhotoID):
        $photoArray = wp_get_attachment($myPhotoID);
    ?>
        <a href="<?php echo wp_get_attachment_url( $myPhotoID ); ?>" class="lightbox" title="<?php echo $photoArray[caption]; ?>">
            <img src="<?php echo wp_get_attachment_url( $myPhotoID ); ?>" class="img-rounded" alt="<?php echo $photoArray[title]; ?>">
    </a>
<?php endforeach; ?>

我希望它有帮助!:-)

于 2015-10-10T22:18:02.623 回答