简体   繁体   中英

How to unhide Image Captions in Photoswipe for Wordpress

I'm using PhotoSwipe Gallery in my Wordpress theme and when I open the Slideshow, the Image Captions don't appear.

I'm trying to write a plugin for Wordpress which should fix this problem. As far as I know (thank God there is Google Chrome's Inspect Element function!!!) the div that I would need to change is the following:

<div class="ps-caption-content">&nbsp;</div>

If I insert something else than between the div tags in Google Chrome's Inspector it becomes the Image's caption.

But I have no idea how to write a jQuery or javaScript function in order change the caption.

I googled and found the following suggestion for customizing PhotoSwipe Image captions, but it didn't work for me:

https://github.com/dimsemenov/PhotoSwipe/blob/master/src/examples/11-custom-captions.html

Does anyone have an idea how I could unhide/show the real captions of the images in the slideshow?

I added wrote a new Plugin for Wordpress and installed and activated. It contains a js folder with .js file called custom_photoswipe_image_titles.js .

Inside my plugin I have the following php code:

<?php
/*
Plugin Name: wpCasa Image Titles
Plugin URI: 
Description:  Adds titles above the listing gallery images
Version: 1.0.0
Author: Elshan Siradjov
Author URI: http://www.w3schools.com/
License: 
*/


/**
 * Load javascript and css Files
 *
 */



function image_titles_add_to_doc_head()
{
    wp_enqueue_style('photoswipe-style', 'http://www.siradjov.anex.at/playground/wp-content/themes/wpcasa/lib/assets/js/photoswipe/photoswipe.css');

    wp_register_script( 'klassmin', 'http://siradjov.anex.at/playground/wp-content/themes/wpcasa/lib/assets/js/photoswipe/klass.min.js' );
        wp_enqueue_script( 'klassmin' );

    wp_register_script( 'code_photoswipe', 'http://siradjov.anex.at/playground/wp-content/themes/wpcasa/lib/assets/js/photoswipe/code.photoswipe-3.0.5.min.js' );
        wp_enqueue_script( 'code_photoswipe' );



    wp_register_script( 'custom_js_photoswipe_image_titles', 'http://siradjov.anex.at/playground/wp-content/plugins/wpcasa-image-titles/js/custom_photoswipe_image_titles.js' );
        wp_enqueue_script( 'custom_js_photoswipe_image_titles' );
}

add_action( 'wp_enqueue_scripts', 'image_titles_add_to_doc_head' );

Inside the custom_photoswipe_image_titles.js file I tried the following jquery code in order to change the image caption:

$('div.ps-caption-content').html('Test Caption');

But it didn't work. The caption is still empty.

I also to tried to embed the jQuery code in a document ready function like this:

jQuery(document).ready(function($)   
{   
    $('div.ps-caption-content').html("blabla");    
});

It didn't work either.

@vico: I found a piece of code inside code.photoswipe-3.0.5.min.js :

(function(e,c,a){

 a.registerNamespace("Code.PhotoSwipe.Toolbar");
 var b=e.Code.PhotoSwipe;

 b.Toolbar.CssClasses={
      toolbar:"ps-toolbar",
      toolbarContent:"ps-toolbar-content",
      toolbarTop:"ps-toolbar-top",
      caption:"ps-caption",
      captionBottom:"ps-caption-bottom",
      captionContent:"ps-caption-content",   // This is where ps-caption-content is set!
      close:"ps-toolbar-close",
      play:"ps-toolbar-play",
      previous:"ps-toolbar-previous",
      previousDisabled:"ps-toolbar-previous-disabled",
      next:"ps-toolbar-next",
      nextDisabled:"ps-toolbar-next-disabled"};
  .....
  ......

I also found this code inside code.photoswipe-3.0.5.min.js :

initialize:function(d,c){
var g;
this.settings=c;
this.cache=d;
this.isVisible=!1;
this.fadeOutHandler=this.onFadeOut.bind(this);
this.touchStartHandler=this.onTouchStart.bind(this);
this.touchMoveHandler=this.onTouchMove.bind(this);
this.clickHandler=this.onClick.bind(this);
g=b.Toolbar.CssClasses.toolbar;
this.settings.captionAndToolbarFlipPosition&&(g=g+" "+b.Toolbar.CssClasses.toolbarTop);
this.toolbarEl=a.DOM.createElement("div",{"class":g},this.settings.getToolbar());

a.DOM.setStyle(this.toolbarEl,{left:0,position:"absolute",overflow:"hidden",zIndex:this.settings.zIndex});
this.settings.target===e?a.DOM.appendToBody(this.toolbarEl):a.DOM.appendChild(this.toolbarEl,this.settings.target);

a.DOM.hide(this.toolbarEl);
this.closeEl=a.DOM.find("."+b.Toolbar.CssClasses.close,this.toolbarEl)[0];
this.settings.preventHide&&!a.isNothing(this.closeEl)&&a.DOM.hide(this.closeEl);
this.playEl=a.DOM.find("."+b.Toolbar.CssClasses.play,this.toolbarEl)[0];
this.settings.preventSlideshow&&!a.isNothing(this.playEl)&&a.DOM.hide(this.playEl);
this.nextEl=a.DOM.find("."+b.Toolbar.CssClasses.next,this.toolbarEl)[0];
this.previousEl=a.DOM.find("."+b.Toolbar.CssClasses.previous,this.toolbarEl)[0];
g=b.Toolbar.CssClasses.caption;this.settings.captionAndToolbarFlipPosition&&(g=g+" "+b.Toolbar.CssClasses.captionBottom);
this.captionEl=a.DOM.createElement("div",{"class":g},"");
a.DOM.setStyle(this.captionEl,{left:0,position:"absolute",overflow:"hidden",zIndex:this.settings.zIndex});
this.settings.target===e?a.DOM.appendToBody(this.captionEl):a.DOM.appendChild(this.captionEl,this.settings.target);
a.DOM.hide(this.captionEl);

this.captionContentEl=a.DOM.createElement("div",{"class":b.Toolbar.CssClasses.captionContent},"");    // This where captionContent is being set!!!

a.DOM.appendChild(this.captionContentEl,this.captionEl);
this.addEventHandlers()},

In the function above the following line seems to be setting the value for the ps-caption-content:

this.captionContentEl=a.DOM.createElement("div",{"class":b.Toolbar.CssClasses.captionContent},"");

That isn't the place to be changing it.

here is a fiddle of the actual code that outputs your DIV

 this.captionContentEl = a.DOM.createElement("div", {
                "class": b.Toolbar.CssClasses.captionContent
            }, "");

and here is a fiddle of your source code expanded

http://siradjov.anex.at/playground/wp-content/themes/wpcasa/lib/assets/js/photoswipe/code.photoswipe-3.0.5.min.js

http://jsfiddle.net/SjZWy/

NOW you just have to find out where in the code produces your ACTUAL caption and then stick the caption var string into that div and your thing will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM