【问题标题】:Redux Framework: Get image gallery captionRedux 框架:获取图片库标题
【发布时间】:2016-01-08 11:46:28
【问题描述】:

我需要使用 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 ) )

caption字段有值,但是redux好像没有保存信息还是我的代码有误?

【问题讨论】:

    标签: php wordpress frameworks gallery redux-framework


    【解决方案1】:

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

    在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; ?>
    

    希望对你有帮助! :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      相关资源
      最近更新 更多