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
Related
I am trying to run a function which is using a featured image as my header image on my webpage I want to include the options to also allow my end user to select between using the featured image or to select a slider instead if they wish to use that page depending here is the code I have for the featured image development.
add_action('neve_before_primary', 'getPageFeaturedImage', 5);
function getPageFeaturedImage() {
// These two variables will only be used if set
$pageTitle = get_field('page-title');
$pageSecondTitle = get_field('page_second_title');
if (has_post_thumbnail($post -> ID) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post -> ID ), 'single-post-thumbnail');
<div class="featured-image-container">
<img src="<?php echo $image[0]; ?>" class="featured-image">
</div>
</div>
This code when run is making the feadured image the main image on the page with a title container on top of it with some css which doesnt matter for this question below you will find the code I have for the slider.
function smartsliderheader() {
echo '<div class="smart-slider-header">';
$slider = get_field("smart_slider_header");
echo do_shortcode($slider);
}
This on its own does what I need it to do the featured image code works and so does the slider but getting them both to run toghether and have only one of them run if the other has no options used is where I could used some help.
Any help with this will be much apprechiated. I look forward to your questions if I have missed something out.
I would simply do this using an if statement checking if the $pageTitle and the $pageSecondTitle are empty
if(empty($pageTitle) && empty($pageSecondTitle)) {
// do stuff for when settings aren't filled in
}
else {
// do stuff for when settings are filled in.
}
I don't know if this answers your question. Let me know if this goes into the direction you want to go so I can build on it further.
I was actually able to solve this by myself by doing the following code we are using 2 variables one is for a featured image and the other is go a slider please look below for the code and an explanation.
if(!$checkFeaturedImage and !$slider) {
return null ;
}
This is checking to see if either of these variable's have a value assigned to them e.g. if a featured image is selected a value will be assigned to it and vice versa for a slider what this is doing if neither of these have a value it will stop the rest of the function running and save render time on the site.
if ($slider) {
echo '<div class="header-featured-image">' ;
echo do_shortcode($slider);
echo '</div>' ;
}
What this is doing is it is making a call to an advanced custom field set up to allow a slider to be selected and if a value is returned that value is then being directly echoed out onto the page.
else {
if (has_post_thumbnail($post -> ID) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post -> ID ), 'single-post-thumbnail'); }
?>
<div class="header-featured-image">
<div class="featured-image-container">
<img src="<?php echo $image[0]; ?>" class="featured-image">
</div>
</div>
}
<?php
}
What this is doing is this is displaying our featured image onto our page if a value is returned by our page editor.
The way this code has been set up is if a featured image and a slider have both been selected it will default back to a slider as that is the first call and due to the null statement at the beginning if nothing is selected nothing will run saving some rendering time.
I want to display all my attachments(images) under tag if image file name has contain that tag (word) in file name.
I mean if I have tag http://example.com/tag/john
I want to display all images that have john in their name.
like /wp-content/uploads/2018/08/my_name-john.jpg, /wp-content/uploads/2017/06/john_meet-us.jpg,
Im new to php and also to wordpress, i dont know if that possible to fetch tag from url that open in browser and then find images for that tag.
I know its helping form, i also have to provide codes what i try and then people help us. but as i said im new yes i google it and i try this code its work but have many limits and also dont know how to modifies for my use.
<?php
function get_attachment_url_by_title( $title ) {
$attachment = get_page_by_title($title, OBJECT, 'attachment');
//print_r($attachment);
if ( $attachment ){
$attachment_url = $attachment->guid;
}else{
return 'image-not-found';
}
return $attachment_url;
}
echo get_attachment_url_by_title('title of image');
?>
Above code show image if we manually provide title of the image, its its must same as file name then above code work otherwise not. so hope you got my question. Thanks in advance. ( Sorry for my bad English)
You can get the attachments url by tag by below Sql Query :
Paste the below code in your current active theme functions.php file.
function get_url_from_tag_name($tag = 'old'){
global $wpdb;
$query = 'SELECT guid FROM `'.$wpdb->prefix.'posts` WHERE guid like "%'.$tag.'%" AND post_type="attachment"';
$results = $wpdb->get_results( $query, OBJECT );
return $results;
}
I hope it help you. Thanks :)
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>';
To get it out of the way here is my code
<!-- Start Slider -->
<?php
// Get Data From Meta
$images = rwmb_meta( 'cam_slider_images', 'type=image_advanced' );
// Blank Variable To Append Too
$slider_image_output = "";
// Append Meta Value To Variable
foreach ( $images as $image )
{
$slider_image_output.= '[slide][image src="'.$image['url'].'" alt="'.$image['alt'].'"][/slide]';
}
// Echo The Shortcode With WordPress :)
echo (do_shortcode('[slider animation="fade, slide" slide_time="4000" slide_speed="500" slideshow="true" random="true" control_nav="false" prev_next_nav="true" no_container="true"]'. $slider_image_output . '[/slider]'));
?>
<!-- End Slider -->
I am attempting to display the images in the slider shortcode, now it works great however on the page the images only come out to be 150x150 pixels which is thumbnail size. So if you look at the image src url in firebug etc its -150x150.. so I was wondering how could I make WordPress export fullsize.
I am using the X theme and its shortcodes.
Thanks in advance.
EDIT
Also using the following plugin for my meta box
http://metabox.io/docs/get-meta-value/
I don't 100% get what exactly you are trying to achieve , but using documentation page you sent,
you can try change your line :
$slider_image_output.= '[slide][image src="'.$image['url'].'" alt="'.$image['alt'].'"][/slide]';
to this one:
$slider_image_output.= '[slide][image src="'.$image['full_url'].'" alt="'.$image['alt'].'"][/slide]';
I am a bit of a newby to mySQL. I have added a new column that stores image names for images stored in a file (eg.toweltest.jpg). I want to draw these to display images on a page. This is someone else's code I am adapting so its a bit patchy. Basically I want to draw the folder name and then add whatever image name is associated with that product. So far I have this:
<?php
$SNIPPET = true;
require './includes/php/header.php';
$product = Product::Get($_GET['product_id']);
$supplier = Supplier::Get($product->supplier_id);
$url = "/products/";
$image = 'toweltest.jpg';
?>
<h1><?php echo htmlspecialchars($product->code); ?><br><br><?php echo htmlspecialchars($product->name); ?></h1>
<h2><?php echo htmlspecialchars($product->description); ?></h2>
<img src='<?php echo $url.$image; ?>'>
This shows the image but obviously I want the image that is associated with each product. The part that says:
$image = 'toweltest.jpg';
needs to be dynamic but I don't know how to phrase it. Any help appreciated.
Thanks
Rich
Can't tell if we don't know the column name for the image, but it should be something like:
$image = $product->image;
This is actually a guess, since I can't see the rest of the code.