如何在插入的图像下显示说明?

时间:2012-04-05 作者:SNaRe

我正在使用wordpress的本机媒体管理器。我有个问题。我可以在图片下显示标题。但我也想展示一下描述。有可能吗?

3 个回复
最合适的回答,由SO网友:fuxia 整理而成

这其实很简单:您只需劫持图像标题的短代码处理程序,就可以获取post_content (图像描述)并将其内容设置为属性的值caption. 然后调用原始的短代码处理程序。

以下示例需要shortcode属性desc 设置为1 要使魔术发挥作用:

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: T5 Caption With Description
 * Description: Adds the description to images with a caption if you set <code>desc=1</code> in the shortcode.
 * Version:     2015.03.26
 * Author:      Thomas Scholz <[email protected]>
 * Author URI:  http://toscho.de
 * License:     MIT
 * License URI: http://www.opensource.org/licenses/mit-license.php
 */

namespace T5\\Images\\Captions;

$callback = __NAMESPACE__ . \'\\add_description\';

// Hijack the native shortcode handlers.
add_shortcode( \'wp_caption\', $callback );
add_shortcode( \'caption\',    $callback );

/**
 * Add image description if needed
 *
 * @param  array $attr
 * @param  null $content
 * @return string
 */
function add_description( $attr, $content = null )
{
    if ( needs_description( $attr ) )
        $attr[\'caption\'] = get_description( $attr[\'id\'] );

    return img_caption_shortcode( $attr, $content );
}

/**
 * Check required attribute values
 *
 * @param array $attr
 * @return bool
 */
function needs_description( Array $attr ) {

    if ( empty ( $attr[\'desc\'] ) )
        return FALSE;

    if ( 1 > (int) $attr[\'width\'] )
        return FALSE;

    return ! empty ( $attr[\'id\'] );
}

/**
 * Prepare post content (the description)
 *
 * @param  string $attachment_id Usually it looks like \'attachment_123\'
 * @return string
 */
function get_description( $attachment_id ) {

    $post_id = str_replace( \'attachment_\', \'\', $attachment_id );
    $img     = get_post( (int) $post_id );

    if ( is_a( $img, \'WP_Post\' ) )
        return wpautop( $img->post_content );

    return \'\';
}
现在添加描述…

enter image description here

…使用常规标题标记…

enter image description here

…并将描述正确格式化:

enter image description here

如果添加参数desc 值不同于1 至标题(desc=0 例如)不使用说明。

Download on GitHub.

SO网友:Kevin Woblick

首先感谢toscho 为他的解决方案。如果任何人都需要主题代码,而不是将其用作插件,那么这里将采用的代码重写为单个函数:

add_shortcode(\'wp_caption\', \'img_caption_add_description\');
add_shortcode(\'caption\', \'img_caption_add_description\');
function img_caption_add_description($attr, $content = null)
{
    $post_id = str_replace(\'attachment_\', \'\', $attr[\'id\']);
    $img = get_post((int)$post_id);

    if (is_a($img, \'WP_Post\')) {
        $attr[\'caption\'] = $img->post_content;
    }

    return img_caption_shortcode($attr, $content);
}
使用Wordpress 4.2.3对此进行了测试,它运行正常,没有任何问题。

SO网友:SNaRe

我找到了解决办法。我想在所有图片下显示描述。这个解决方案非常有效。幸亏https://wordpress.stackexchange.com/users/3687/tom-auger

<?php
/*
Plugin Name: WPSE-45326 Gallery Replacement example
Plugin URI: https://wordpress.stackexchange.com/questions/45326
Description: A plugin to demonstrate how to replace the default \'gallery\' shortcode and add additional HTML tags for more customization.
Version: 1.0
Author: Tom Auger
Author URI: http://www.tomauger.com
License: GPL2
*/

class wpse_45326_Gallery_Replacement {
    function __construct(){
        // Hook on the plugins-loaded action since it\'s the first real action hook that\'s available to us.
        // However, if you\'re using a theme and want to replace that theme\'s `gallery` custom shortcode,
        // you may need to use another action. Search through your parent theme\'s files for \'gallery\' and see
        // what hook it\'s using to define it\'s gallery shortcode, so you can make sure this code runs AFTER their code.
        add_action( "init", array( __CLASS__, "init" ) );
    }

    function init(){
        remove_shortcode( \'gallery\' ); // Remove the default gallery shortcode implementation
        add_shortcode( \'gallery\', array( __CLASS__, "gallery_shortcode" ) ); // And replace it with our own!
    }

    /**
    * The Gallery shortcode.
    *
    * This has been taken verbatim from wp-includes/media.php. There\'s a lot of good stuff in there.
    * All you want to do is add some more HTML to it, and since (for some reason) they didn\'t provide more
    * filters to be able to add, we have to replace the Gallery shortcode wholesale.
    *
    * @param array $attr Attributes of the shortcode.
    * @return string HTML content to display gallery.
    */
    function gallery_shortcode($attr) {
        global $post;

        static $instance = 0;
        $instance++;

        $output = apply_filters(\'post_gallery\', \'\', $attr);
        if ( $output != \'\' )
            return $output;

        if ( isset( $attr[\'orderby\'] ) ) {
            $attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
            if ( !$attr[\'orderby\'] )
                unset( $attr[\'orderby\'] );
        }

        // NOTE: These are all the \'options\' you can pass in through the shortcode definition, eg: [gallery itemtag=\'p\']
        extract(shortcode_atts(array(
            \'order\'      => \'ASC\',
            \'orderby\'    => \'menu_order ID\',
            \'id\'         => $post->ID,
            \'itemtag\'    => \'dl\',
            \'icontag\'    => \'dt\',
            \'captiontag\' => \'dd\',
            \'columns\'    => 3,
            \'size\'       => \'thumbnail\',
            \'include\'    => \'\',
            \'exclude\'    => \'\',
            // Here\'s the new options stuff we added to the shortcode defaults
            \'titletag\'  => \'p\',
            \'descriptiontag\' => \'p\'
        ), $attr));

        $id = intval($id);
        if ( \'RAND\' == $order )
            $orderby = \'none\';

        if ( !empty($include) ) {
            $include = preg_replace( \'/[^0-9,]+/\', \'\', $include );
            $_attachments = get_posts( array(\'include\' => $include, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );

            $attachments = array();
            foreach ( $_attachments as $key => $val ) {
                $attachments[$val->ID] = $_attachments[$key];
            }
        } elseif ( !empty($exclude) ) {
            $exclude = preg_replace( \'/[^0-9,]+/\', \'\', $exclude );
            $attachments = get_children( array(\'post_parent\' => $id, \'exclude\' => $exclude, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
        } else {
            $attachments = get_children( array(\'post_parent\' => $id, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
        }

        if ( empty($attachments) )
            return \'\';

        if ( is_feed() ) {
            $output = "\\n";
            foreach ( $attachments as $att_id => $attachment )
                $output .= wp_get_attachment_link($att_id, $size, true) . "\\n";
            return $output;
        }

        $itemtag = tag_escape($itemtag);
        $captiontag = tag_escape($captiontag);
        $columns = intval($columns);
        $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
        $float = is_rtl() ? \'right\' : \'left\';

        $selector = "gallery-{$instance}";

        $gallery_style = $gallery_div = \'\';
        if ( apply_filters( \'use_default_gallery_style\', true ) )
            $gallery_style = "
            <style type=\'text/css\'>
                #{$selector} {
                    margin: auto;
                }
                #{$selector} .gallery-item {
                    float: {$float};
                    margin-top: 10px;
                    text-align: center;
                    width: {$itemwidth}%;
                }
                #{$selector} img {
                    border: 2px solid #cfcfcf;
                }
                #{$selector} .gallery-caption {
                    margin-left: 0;
                }
            </style>
            <!-- see gallery_shortcode() in wp-includes/media.php -->";
        $size_class = sanitize_html_class( $size );
        $gallery_div = "<div id=\'$selector\' class=\'gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}\'>";
        $output = apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );

        $i = 0;
        foreach ( $attachments as $id => $attachment ) {
            $link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

            $output .= "<{$itemtag} class=\'gallery-item\'>";
            $output .= "
                <{$icontag} class=\'gallery-icon\'>
                    $link
                </{$icontag}>";

            // MODIFICATION: include the title and description HTML if we\'ve supplied the relevant shortcode parameters (titletag, descriptiontag)
            if ( $captiontag ) {
                $output .= "
                    <{$captiontag} class=\'wp-caption-text gallery-caption\'>";
                // The CAPTION, if there is one
                if ( trim( $attachment->post_excerpt ) ) {
                    $output .= "
                        " . wptexturize($attachment->post_excerpt);
                }

                // The TITLE, if we\'ve not made the \'titletag\' param blank
                if ( $titletag ){
                    $output .= "
                        <{$titletag} class=\\"gallery-item-title\\">" . $attachment->post_title . "</{$titletag}>";
                }

                // The DESCRIPTION, if we\'ve not specified a blank \'descriptiontag\'
                if ( $descriptiontag ){
                    $output .= "
                        <{$descriptiontag} class=\\"gallery-item-description\\">" . wptexturize( $attachment->post_content ) . "</{$descriptiontag}>";
                }

                $option .= "
                    </{$captiontag}>";
            }
            $output .= "</{$itemtag}>";
            if ( $columns > 0 && ++$i % $columns == 0 )
                $output .= \'<br style="clear: both" />\';
        }

        $output .= "
                <br style=\'clear: both;\' />
            </div>\\n";

        return $output;
    }
}

new wpse_45326_Gallery_Replacement();

结束

相关推荐

Overriding Gallery Margin

继续构建免费主题。使用图库CSS。Wordpress会自动将左边距添加到库中。示例:http://themeforward.com/demo2/features/gallery-2/ 那么,我该如何摆脱这种令人讨厌的利润呢?/* Gallery */ .gallery { margin:30px auto auto; text-align:center } .gallery-item { float:left; margin-top:10px;&#x