Where to add code to custom gallery shortcode - php

I'm using the information here http://ieg.wnet.org/2016/02/replacing-default-wordpress-gallery-shortcode/ as the basis for a custom gallery shortcode, and trying to create this carousel slider https://www.jssor.com/demos/image-gallery.slider. Problem is I don't know how exactly to implement it.
The images and everything up until then shows in the source code, but then nothing after it.
<div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:800px;height:356px;overflow:hidden;">
<?php
foreach ( $images as $image ) {
$thumbnail = wp_get_attachment_image_src($image->ID, 'homepage-thumb');
$thumbnail = $thumbnail[0];
$fullsize = wp_get_attachment_image_src($image->ID, 'service-page');
$fullsize = $fullsize[0];
$gallery .= "<div><img data-u='image' src='".$thumbnail."'><img data-u='thumb' src='".$fullsize."'></div>";
}
return $gallery;
?>
</div>
It's probably something simple, this is just far past my base of knowledge when it comes to this.

You are returning the value instead of printing it to the web page. Use echo instead of return. Also make sure you echo it in each iteration of the loop so you don't just print one image, but all of them.
<div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:800px;height:356px;overflow:hidden;">
<?php
foreach ( $images as $image ) {
$thumbnail = wp_get_attachment_image_src($image->ID, 'homepage-thumb');
$thumbnail = $thumbnail[0];
$fullsize = wp_get_attachment_image_src($image->ID, 'service-page');
$fullsize = $fullsize[0];
$gallery .= "<div><img data-u='image' src='".$thumbnail."'><img data-u='thumb' src='".$fullsize."'></div>";
echo $gallery;
}
?>
</div>

Related

I am trying to use ACF repeater to call a random image on a wordpress site

This is my code, as aforesaid - i'm trying to get a single picture to echo inside a premade div out of repeater element. Despite working directly with the example on the website - I would love some help.
<?php
$repeater = get_field('left_column_repeater' ); // get all the rows
$rand_row = $repeater[ array_rand( $repeater ) ]; // get a random row
$rand_row_image = $rand_row['left_column_image' ]; // get the sub field value
// Note
// $first_row_image = 123 (image ID)
$image = wp_get_attachment_image_src( $first_row_image, 'full' );
// url = $image[0];
// width = $image[1];
// height = $image[2]; ?>
<div class="circular" style="background-image: url('<?php echo $image[0]; ?>');"> </div>
Where did I go wrong?

How to replace a variable URL string (inside <img src="">) depending on pattern? (SimplePie)

This is my first post here. I'm not sure if the title of my post is the most descriptive way of showing my problem, sorry in advance!
Anyway, I've got a Facebook RSS feed on my site using simplepie - I'm displaying posts with a brief summary of its content and an accompanying image, both coming from the feed itself.
I've got a single div that displays this information and is repeated according to the number of stories I've set to display, in my case 3, so the HTML page outputs three div's each with a different story/accompanying image.
<div class="block">
<a href="<?php echo $image_url ?>" target="_blank">
<img src="<?php echo $image ?>" alt="" />
</a>
</div>
The problem is Facebook RSS feeds only show tiny thumbnails generated from the original image (either external or from Facebook's server), but I want them in their original resolution.
I've succesfully replaced the thumbnails coming from Facebook's server by changing the name of the image (from "something_s.jpg" to "something_n.jpg"):
$image = str_replace("_s.jpg", "_n.jpg", $image);
I tried with ltrim to remove the Facebook URL appended before the external image's URL (e.g. https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDZORAaPpbr5COr&w=154&h=154&url=http://example.com/image.jpg). So, I thought this would do it:
$image = ltrim(stristr(str_replace("_s.jpg", "_n.jpg", $image), '&url='), '&url=');
Oddly enough, external images showed up in their original resolution (e.g. example.com/image.jpg inside <img src="">) but those coming from Facebook (e.g. 10153970_10153145169383306_1966565627_n.jpg) did not anymore. The img src attribute is just empty!
What am I doing wrong here? Any help is appreciated.
(BTW, this is simplepie code in my page):
<?php
require_once('../php/autoloader.php');
$feed = new SimplePie();
$feed->set_feed_url(array('https://www.facebook.com/feeds/page.php?format=rss20&id=40796308305'));
$feed->set_item_limit(3);
$feed->init();
function returnImage ($text) {
$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
$pattern = "/<img[^>]+\>/i";
preg_match($pattern, $text, $matches);
$text = $matches[0];
return $text;
}
function scrapeImage($text) {
$pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
preg_match($pattern, $text, $link);
$link = $link[1];
$link = urldecode($link);
return $link;
}
$feed->strip_htmltags(array('embed','center','strong'));
$feed->handle_content_type();
foreach ($feed->get_items() as $item):
$feedDescription = $item->get_content();
$image = returnImage($feedDescription);
$image = scrapeImage($image);
$image = ltrim(stristr(str_replace("_s.jpg", "_n.jpg", $image), '&url='), '&url=');
$image_url= $item->get_permalink();
$description = $item->get_description();
$description = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $description);
endforeach;
?>
Mr.Rod
use this custom function to change Image src Attribue..
<?php
function ChangeImageSrc($image , $Newsrc){
$regex = '#src="(([^"/]*/?[^".]*\.[^"]*)"([^>]*)>)#';
$Content = preg_replace($regex, $image, 'src="'.$Newsrc.'"');
return $Content;
}
// Useage
$markup = "<img src='fb.com/blahblah.jpg'>";
// Change Src
$toNewSrc = ChangeImageSrc($markup , 'http://example.com/newsrc.png');
print $toNewSrc;
?>
after adding this function to your script
change this line
$image = ltrim(stristr(str_replace("_s.jpg", "_n.jpg", $image), '&url='), '&url=');
to
$image = ChangeImageSrc('src="_n.jpg"', '_s.jpg');
you have to more customize this function to match your needs .

How do I get the featured image description from my wordpress page?

So I set up my wordpress theme to allow users to upload featured images, and Im building my index page to display selected pages' featured images but would also like to display the description of the image.
The thing is, Im not using the loop, Im pulling the page IDs using wordpress's settings API as options.
So displaying the featured images is done like this:
<?php $bucket_options = get_option('frontpage_display_options'); ?>
<?php $page_one = $bucket_options['frontpage_bucket_one']; ?>
<?php $page_one = get_post($page_one); ?>
<?php if (has_post_thumbnail($page_one->ID)) : ?>
<?php echo get_the_post_thumbnail($page_one->ID, 'bucket'); ?>
<?php endif; ?>
I keep reading that this will work:
echo get_post(get_the_post_thumbnail_id($page_one->ID))->post_content;
or this:
echo get_post(get_the_post_thumbnail($page_one->ID))->post_content;
But neither of them displays anything
That capability is awaiting a new release: http://core.trac.wordpress.org/ticket/12235
But a solution that is floating around is to create a function in functions.php:
function the_post_thumbnail_caption() {
global $post;
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
if ($thumbnail_image && isset($thumbnail_image[0])) {
echo '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
}
}
And then call the_post_thumbnail_caption();
This works for me. It echo's the title, caption and description of the featured image.
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
echo '<p>' . get_post(get_post_thumbnail_id())->post_title . '</p>';
echo '<p>' . get_post(get_post_thumbnail_id())->post_excerpt . '</p>';
echo '<p>' . get_post(get_post_thumbnail_id())->post_content . '</p>';
endif;
?>

Get last section from echo and store in variable PHP

I've a function that will echo the URL of the image from the content of Wordpress.
I got the function work now no problem
// Get Image Attachments
function sa_get_image($postid=0, $size='thumbnail') { //it can be thumbnail or full
if ($postid<1)
$postid = get_the_ID();
$thumb = get_post_meta($postid, "thumb", TRUE); // Declare the custom field for the image
if ($thumb != null or $thumb != '') {
echo $thumb;
}
elseif ($images = get_children(array( //If you upload an image function gets first image
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => '5',
'post_mime_type' => 'image', )))
foreach($images as $image) {
$thumbnail=wp_get_attachment_image_src($image->ID, $size);
?>
<?php echo $thumbnail[0]; ?>
<?php }
else { //If you don't upload or declare as thumb custom field func. gets custom (default) image
echo get_bloginfo ( 'template_directory' ); //same as wp-content/themes/your-theme/
echo '/images/image-pending.gif'; // Put this image into your themes images folder and set the path here
}
}
The only problem now is that the <?php echo $thumbnail[0]; ?> if there are more than one image it will echo all of them something like this
<img src=" http://applesiam.com/wp-content/uploads/2555-05-02_19h14_34-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h14_11-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_43-150x123.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_20-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_17-150x150.png ">
As you can see it just separated by some spaces.
Now I just want to have the last image if there is more than one image in the $thumbnail
I'm not really expert with PHP as my semester for PHP course will start next week.
Thanks in advance for any suggestion going to be.
Try:
$imgSrc = end((explode(' ', trim($imgSrc)));
Where $imgSrc is the value you put into <img src="!!!==>>here<<==!!!">.
quickly typed, w/o any warranty. Should leave single URLs intact, and if multiple ones separated by space(s) will take the last.
Try this instead.
echo trim($thumbnail[sizeof($thumbnail)-1]);

How can I implement JQuery Pagination into a custom script?

I am using a bespoke Jquery/PHP gallery script which pulls images from a Flickr feed.
I have tried to implement the JQuery pagination plugin, to no avail.
Here is the code...
<?php
require_once('php/simplepie.inc');
$feed = new Simplepie('http://api.flickr.com/services/feeds /photos_public.gne?id=44262300#N06&lang=en-us&format=rss_200');
$feed->handle_content_type();
function image_from_description($data) {
preg_match_all('/<img src="([^"]*)"([^>]*)>/i', $data, $matches);
return $matches[1][0];
}
function select_image($img, $size) {
$img = explode('/', $img);
$filename = array_pop($img);
$s = array(
'_s.', // square
'_t.', // thumb
'_m.', // small
'.', // medium
'_b.' // large
);
$img[] = preg_replace('/(_(s|t|m|b))?\./i', $s[$size], $filename);
return implode('/', $img);
}
?>
<script type="text/javascript">
$(function(){
$("#images div").quickpaginate({ perpage: 4, showcounter: false, pager : $("#image_counter") });
});
</script>
<div class="album-wrapper" id="images">
<?php foreach ($feed->get_items() as $item): ?>
<div class="photo">
<?php
if ($enclosure = $item->get_enclosure()) {
$img = image_from_description($item->get_description());
$full_url = select_image($img, 4);
$thumb_url = select_image($img, 0);
echo '<img id="photo_' . $i . '" src="' . $thumb_url . '" />'."\n";
}
?>
</div>
<?php endforeach; ?>
</div>
<div id="image_counter"></div>
Can anyone see what I have missed or I am doing wrong?
Thanks,
Dan
Have you tried trying to isolate the area where the error might be located? Can you figure it out if it is in the PHP-side of your code or on the JavaScript side?
In any case, I've had success using the jQuery pager companion plugin for the Tablesorter plugin. It doesn't work very well for very long lists of data, however.

Categories