Show body content with popup in Wordpress - php

$large_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
$output .= '<a class="overlay" rel="shadowbox" href="'.$large_image[0].'">';
It's OK, working fine.
But I want, show post's body in popup. I tried like this but not working.
$content = wp_get_attachment_image_src( body($post->ID));
Whats the problem? How can I fix it?
Thanks.

I don't know, maybe because body isn't a function in WP or php, or because you're trying to use a function that return image source (come on the function name tell you by itself what it does) and hope it'll return (by magic) post content?
Here is how you get the content from a post, within the loop:
$content = get_the_content();
And if you're outside the loop, you can do:
$content = $post->post_content;

you can consider alligator popup
check how it works, you will get some idea.

Related

Wordpress Echoing Urls Strangely

In a section of code I'm attempting to show a menu title and the associated featured image.
The featured image is going to be the background of a <div> element, like this:
echo '<div style="width:100%;background-image:url("'.$thumbnail[0].'")"><h1>'. $menu->title .'</h1></div></a>';
I've tried two different method of getting the URL:
$image = get_the_post_thumbnail_url($menu->object_id, 'full');
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $menu->object_id ), 'full' );
Whenever any of these urls are rendered into the browser the url is completely garbled from this:
"http://localhost:8085/wp-content/uploads/D_DSC0130.png"
Into this:
url(" http:="" localhost:8085="" wp-content="" uploads="" d_dsc0130.png")"="">
I've not seen any posts or discussion talking about anything similar. The site is hosted in IIS and running on PHP 7.1.7.
I have already attempted to escape the returned value with this:
echo '<div style="width:100%;background-image:url("'.esc_url($thumbnail[0]).'")"><h1>'. $menu->title .'</h1></div></a>';
if I echo this, the URL is fine and the image is displayed on the page:
echo '<img src="'.esc_url($image).'" />';
How can I correct the rendered values in the div version?
If the url is being returned ok then you probably need to escape it to output properly
echo '<div style="width:100%;background-image:url("'.esc_url($thumbnail[0]).'")"><h1>'. $menu->title .'</h1></div></a>';

Removing Images from a WordPress Post

I wanted to remove the images from a WordPress post to give me more control of how I could layout the design for the front page of a website. After doing some messing around and failing, I finally found a great post which had an amazing little piece of code that solved my problem.
<?php
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $post->post_content);
echo $postOutput;
?>
But some time i have link above images like:
<img src="PATH_IMAGES">
So, How can remove it?
Change above code by following code which remove link if exist into your content.
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $post->post_content);
$postContent = preg_replace("/<a[^>]+\>/i", "", $postOutput);
echo $postContent;

Wordpress filters and excerpts

I am trying to pull content from one page into another. I could be going about this the wrong way, but I am just trying to get the general method of how it is done. This is what I have so far trying to get the data I need. I am not getting the permalink or the image. The other issue is when applying the substr method to the $content variable it actually cuts the html tags off to making it to where anything after it gets nested into whatever html happens to be in the content. I appreciate any tips you may have. Here is the function I created to try and pull the content in it's initial stage.
function show_post($path) {
$post = get_page_by_path($path);
$title = apply_filters('the_title', $post->post_title);
$image = apply_filters('featured_image', $post->the_post_thumbnail);
$content = apply_filters('the_content', $post->post_content);
$link = apply_filters('the_permalink', $post->get_permalink);
echo '<h3>'.$title.'</h3>';
echo substr($content, 0, 300) . '...';
}
$path is the relative url of the website to where I am pulling content so lets say my url i want to grab data is www.test.com/about/bio I would make the path be about/bio.
I appreciate your help in this.

if / else in wordpress to pull a specific image

I had some trouble with facebook pulling the wrong images from Wordpress links added to the FB news feed. I was able to correct it with the "if" snippet of code below. I added the "else" statement to provide for a fall-back image in case the user hasn't added a featured image to the linked WP post, but it doesn't seem to be working. Obviously I'm no php coder... any help?
<?php
if ( has_post_thumbnail())
{
$fb_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
}
else {
$fb_image = wp_get_attachment_image_src ("http://MY-IMAGE-URL"), 'full');
}
?>
I also tried the code below in the else statement, but to avail
wp_get_attachment_image_src( 732, $size, $icon )
Thanks in advance
The function wp_get_attachment_image_src is used to get the URL (src) for an image attachment.
However, you cannot directly pass on an URL like you do as it works based on an attachment ID.
In your else statement, replace the line with the following in order to work with a default image :
$fb_image = "http://MY-IMAGE-URL";
Hope this helps

Dynamically get some data and insert it into a div (wordpress related)

I have a challenge with a wordpress theme I'm developing, but I think what I'm after is really just a general php/JS solution, nothing Wordpress specific.
So I have the below code with pulls in the thumbnail and description for an array of images I have uploaded. What I'd like to do is when a user clicks on the link the description and caption associated with that image is displayed in a div elsewhere on the page.
My issue is that so far the only way I can think of to do that is to set a javascript variable within my php foreach statement, but the problem with that is that this overwrites the variable value each time (as I can't change the variable name) so by the end of it all I don't have a different JS variable for each image, I just have one with the details from the last image in the array.
I know AJAX might be one way forward, but I know pretty much nothing about it. If anyone can help point me in the right direction I'd really appreciate it.
Thanks
<?php
$gallery_images = get_custom_field('galleryImages:to_array');
foreach ($gallery_images as $galleryID) {
$description = $attachment->post_content; //get image description
$caption = $attachment->post_excerpt; //get image caption
?>
link
<?php
}
?>
<div id="targetDiv"></div>
I think you're going about this the wrong way, personally. Using AJAX to interact with a WordPress site seems like overkill for the simple ability of showing some peripheral information about an image.
What I would do is have WordPress spit out the image, along with the caption info when the page is initially downloaded, but hide the caption info and then use client-side JavaScript to show/hide it when it's needed.
<?php
$button_html = "";
$caption_html = "";
$gallery_images = get_custom_field('galleryImages:to_array');
$gallery_images_count = 0;
foreach ($gallery_images as $galleryID) {
$description = $attachment->post_content; //get image description
$caption = $attachment->post_excerpt; //get image caption
$button_html .= '<div id="caption-button-' . $gallery_images_count . '" class="show-caption-button">Show Caption</div>';
$caption_html .= '<div id="caption-' . $gallery_images_count . '" style="display:none;">' . $caption . '</div>';
$gallery_images_count++;
}
echo '<div id="buttonDiv">' . $button_html . '</div>';
echo '<div id="targetDiv">' . $caption_html . '</div>';
?>
Then the JavaScript/jQuery:
$('.show-caption-button').click(function(){
var caption_id = $(this).prop('id').substring(15);
$('#caption-'+caption_id).eq(0).toggle();
});
It's hard to test without setting up a WordPress myself, but essentially what we're doing is adding caption divs with numbered id's to a string variable in PHP as we're looping through our images. Then, at the end of the loop, we echo that out to the page.
In JavaScript/jQuery, we're grabbing the corresponding id of the caption button and using it to toggle the relevant caption further down in the page.

Categories