获取帖子嵌入的图像标题

时间:2016-03-23 作者:Madeirense

我正在尝试获取嵌入后图像的标题,然后在灯箱上使用。

这是我的代码:

function my_theme_prefix_content_gallery( $content ) {
    global $post;

    $args = array(
        \'post_type\' => \'attachment\',
        \'post_mime_type\'=>\'image\',
        \'numberposts\' => 1,
        \'post_status\' => null,
        \'post_parent\' => $post->ID,
    );

    $attachments = get_posts( $args );
    if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           $caption = $attachment->post_excerpt;
        }
    }

    $pattern     = "/<a(.*?)href=(\'|\\")(.*?).(bmp|gif|jpeg|jpg|png)(\'|\\")(.*?)>/i";
    $replacement = \'<a$1href=$2$3.$4$5 rel="lightbox" title="\' . $caption . \'"$6>\';
    $content     = preg_replace( $pattern, $replacement, $content );

    return $content;
}
add_filter( \'the_content\', \'my_theme_prefix_content_gallery\' );
我现在拥有的(上面的代码)是媒体库中其他图像的标题。如何获取我看到的图像的标题?

EDIT

嵌入图像弹出窗口的放大弹出位:

 $(\'a[rel="lightbox"]\').magnificPopup ({
        type: \'image\',
        mainClass: \'mfp-fade\',
        fixedContentPos: false,
        removalDelay: 350,
        gallery: {
            enabled: true
        }
});
因此,使用这段代码,我可以在单击时在放大弹出窗口中打开嵌入后的图像。我所缺少的只是当他们在放大弹出窗口中打开时显示标题的方式。

1 个回复
最合适的回答,由SO网友:Luis Sanz 整理而成

如果我理解正确的话,您正在尝试将自定义rel属性和基于附件标题的title属性添加到帖子库中嵌入的每个图像中。您可以尝试使用\'wp_get_attachment_link\' 滤器

<?php

    function wpse221533_add_caption_title_to_content_gallery_image_attachments( $markup, $id, $size, $permalink, $icon, $text ) {

        //Target only images
        if ( ! wp_attachment_is_image( $id ) ) :
            return $markup;
        endif;

        //Limit the scope of the new attributes
        $post_types = array( \'post\', \'page\' );

        if ( ! is_singular( $post_types ) ) :
            return $markup;
        endif;

        //Get attachment data
        $current_attachment_object = get_post( $id );

        //Get attachment caption
        $current_attachment_caption = $current_attachment_object->post_excerpt;

        //Nothing wrong with regex, but str_replace is cheaper
        $markup = str_replace( \' href=\', \' rel="lightbox" title="\' . $current_attachment_caption . \'" href=\', $markup );

        return $markup;

    }

    add_filter( \'wp_get_attachment_link\', \'wpse221533_add_caption_title_to_content_gallery_image_attachments\', 10, 6 ) ;

?>

EDIT 16/04/06

事实上,我没有正确理解,因为我指的是帖子的图片库和帖子的单幅图片附件。以上代码仅适用于库。

此其他代码将对单个图像附件执行此操作:

<?php

    function wpse221533_add_caption_title_to_content_single_image_attachments( $content ) {

        //Limit the scope of the new attributes
        $post_types = array( \'post\', \'page\' );

        if ( ! is_singular( $post_types ) ) :
            return $content;
        endif;

        //Parse the content DOM for links
        $doc = new DOMDocument();
        $doc->loadHTML( $content );

        $anchors = $doc->getElementsByTagName( \'a\' ); 

        //Parse each link
        foreach ( $anchors as $anchor ) :

            //Get the rel attribute
            $anchor_rel = $anchor->getAttribute( \'rel\' );

            //Get the image ID based on the rel attribute
            $current_attachment_id = substr( strrchr( $anchor_rel, "-" ), 1 );

            //Check if the extracted ID is actually a number
            if ( ! is_numeric( $current_attachment_id ) ) :
                return $content;
            endif;

            //Target only images
            if ( ! wp_attachment_is_image( $current_attachment_id ) ) :
                return $content;
            endif;

            //Get the attachment object
            $current_attachment_object = get_post( $current_attachment_id );

            //Get the attachment caption
            $current_attachment_caption = $current_attachment_object->post_excerpt;

            //Set the rel attribute
            $anchor->setAttribute( \'rel\', \'lightbox\' );

            //Finally set the title attribute based on the attachment caption
            $anchor->setAttribute( \'title\', $current_attachment_caption );

        endforeach;

        //Save the DOM changes
        $content = $doc->saveHTML();

        return $content;

    }

    add_filter( \'the_content\', \'wpse221533_add_caption_title_to_content_single_image_attachments\', 10, 1 );

?>

相关推荐

如何修改WP_INCLUDE/BLOCKS/LATEST_posts.php

我是WordPress开发的初学者,希望得到一些帮助。我在一个简单的WordPress网站上工作。基本上,用户希望在主页上显示最新的帖子。我使用了最新帖子块,并将其设置为显示整个帖子内容,现在用户不希望帖子标题链接到单个帖子页面(因为帖子的内容显示在主页上)。如何安全地修改模板文件,使其像h2标记一样使用,而不是在主题中使用的href标记。我知道您可以创建子主题并修改wp_content 文件,但我不确定如何处理中的文件wp_include. 我读到一些关于修改functions.php 但我不确定,如果