I am having some trouble modifying a plugin WordPress popular posts I installed.
It has the option to fetch thumbnails from a custom field, which I have entered as "image_facebook". But the thumbnails are not being displayed.
While checking the code I found that img src has post id instead of returning image URL.
"<img src="5438" width="135" height="135" alt="Alt text" border="0" />"
I have narrowed down the problem to another plugin which I have installed http://wordpress.org/plugins/advanced-custom-fields/ and while it is active I have to use "the_field ()" to fetch the custom field content instead of regular WordPress "get_post_meta" It is documented here http://www.advancedcustomfields.com/resources/functions/the_field/
I need to edit the below code in the WordPress popular posts file to use the_field() function. The code in the WordPress-popular-posts.php says:-
// POST THUMBNAIL
if ($instance['thumbnail']['active'] && $this->thumb) {
$tbWidth = $instance['thumbnail']['width'];
$tbHeight = $instance['thumbnail']['height'];
$thumb = "<a href=\"". $permalink ."\" title=\"{$title}\" target=\"".$this->user_ops['tools']['link']['target']."\">";
if ( $this->user_ops['tools']['thumbnail']['source'] == "custom_field" ) { // get image from custom field
$path = get_post_meta($p->id, $this->user_ops['tools']['thumbnail']['field'], true);
if ( $path != "" ) {
if ( $this->user_ops['tools']['thumbnail']['resize'] ) {
$thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );
} else {
$thumb .= "<img src=\"{$path}\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" alt=\"{$title}\" border=\"0\" />";
}
} else {
$thumb .= "<img src=\"". $this->default_thumbnail ."\" alt=\"{$title}\" border=\"0\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" />";
}
} else { // get image from post / Featured Image
$thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );
}
//$thumb .= "</a>";
}
In my theme files I am able to retrieve the Image URL via the following code:-
<img src="<?php echo get_field('image_facebook'); ?>" alt="<?php the_title(); ?>" class="postImg" />
Please help me put this function in the plugin code above.
UPDATE
Ok so with the below code, the image URL is fetched but it is fetching the same image URL for all 10 popular posts.
// POST THUMBNAIL
if ($instance['thumbnail']['active'] && $this->thumb) {
$my_image = get_field('image_facebook');
$my_title = get_the_title();
$tbWidth = $instance['thumbnail']['width'];
$tbHeight = $instance['thumbnail']['height'];
$thumb = "<a href=\"". $permalink ."\" title=\"{$title}\" target=\"".$this->user_ops['tools']['link']['target']."\">";
if ( $this->user_ops['tools']['thumbnail']['source'] == "custom_field" ) { // get image from custom field
$path = get_post_meta($p->id, $this->user_ops['tools']['thumbnail']['field'], true);
if ( $path != "" ) {
if ( $this->user_ops['tools']['thumbnail']['resize'] ) {
$thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );
} else {
//$thumb .= "<img src=\"{$path}\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" alt=\"{$title}\" border=\"0\" />";
$thumb .= "<img src=\"" . $my_image . "\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" alt=\"" . $my_title . "\" border=\"0\" />";
}
} else {
$thumb .= "<img src=\"". $this->default_thumbnail ."\" alt=\"{$title}\" border=\"0\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" />";
}
} else { // get image from post / Featured Image
$thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );
}
//$thumb .= "</a>";
}
<img src="5438" width="135" height="135" alt="Alt text" border="0" />
If this is your only problem, you can modify the value that the ACF image field returns. Right now it is probably set to Image ID. Try setting it to Image URL instead:
In case that doesn't help, I would try this. Keep in mind I don't understand how your plugin interacts with ACF. First I would set your variables:
$my_image = get_field('image_facebook');
$my_title = get_the_title();
Then I would replace every instance of $thumb .= with your functioning ACF code, just to test, like this:
$thumb .= "<img src=\"" . $my_image . "\" alt=\"" . $my_title . "\" class=\"postImg\" />";
Related
I have the code below that changes Woo Commerce product archive/loop images to flip between 2 images on hover, instead of just the default single/static 'featured image' (second image is taken from product gallery images).
It is indeed flipping images as it should and working well.
Problem is that the images are no longer clickable - therefore they are not linking to the actual product as they should / did before.
Any help will be greatly appreciated.
N
function tp_create_flipper_images() {
global $post, $woocommerce ,$product;
$get_gallery_image_ids = $product->get_gallery_image_ids();
//wp_dbug($get_gallery_image_ids);
$get_image_id = $product->get_image_id();
$image_url_top = get_the_post_thumbnail_url($post->ID, 'woocommerce_thumbnail');
$placeholder_img = wc_placeholder_img_src('woocommerce_thumbnail');
//wp_dbug($placeholder_img);
if($get_image_id){
$image_top_alt = get_post_meta($get_image_id, '_wp_attachment_image_alt', TRUE);
if(!$image_top_alt){
$image_top_alt = $product->get_name();
}
if($get_gallery_image_ids){
$image_bottom_alt = get_post_meta($get_gallery_image_ids[0], '_wp_attachment_image_alt', TRUE);
if(!$image_bottom_alt){
$image_bottom_alt = $image_top_alt;
}
$output = '<div class="tp-image-wrapper">';
//$post->post_title;
//$image_url_top = get_the_post_thumbnail_url($post->ID, 'woocommerce_thumbnail');
$image_url_bottom = wp_get_attachment_image_src($get_gallery_image_ids[0], 'woocommerce_thumbnail' );
$output .= '<img class="tp-image" src="'.$image_url_top.'" alt="'.$image_top_alt.'" />';
//$output .= '<img class="bottom" width="300" height="300" src="'.$image_url_bottom[0].'" />';
$output .= '<img class="tp-image-hover" src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" />';
$output .= '</div>';
}
else{
$output = '<div class="tp-image-wrapper"><img class="image" src="'.$image_url_top.'" alt="'.$image_top_alt.'" /></div>';
}
}
else{
$output = '<div class="tp-image-wrapper"><img class="image" src="'.$placeholder_img.'" /></div>';
}
echo $output;
}
}
Thanks to user #Bhauti, both his solutions below worked for me. Leaving it here incase anyone is searching for the same answer.
$output .= '<img class="tp-image-hover" src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" />';
OR
$output .= '<img class="tp-image-hover" src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" />';
I am trying to add the alt attribute to post thumbnails on my blog.
I get the alt text to echo, but not as an attribute, but as text!
<?php if ( has_post_thumbnail() ) {$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ ); $image_alt = wpseoFocusKW();
echo '<img width="100%" src="' . $image_src[0] . '" alt=' . $image_alt .' >';} ?></div></div>
You can see the issue here: http://benefacto.org/three-days-paid-volunteering-leave-an-update-from-rob-wilsons-office/
You will note I'm using the Yoast Keyword as the alt, which works fine.
Any thoughts much appreciated.
Ben
Try the following (only PHP part):
<?php
if (has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src(get_post_thumbnail_id(),'thumbnail');
$image_alt = wpseo_get_value('focuskw', $post->ID);
echo '<img width="100%" src="'.$image_src[0].'" alt="'.$image_alt.'">';
}
?>
The content of the function wpseoFocusKW() looks like this:
function wpseoFocusKW()
{
$focuskw = wpseo_get_value('focuskw', $post->ID);
echo $focuskw;
}
This function only echo the keyword, but don't return!
Reference: http://snipplr.com/view/67931/
You can create a custom function or change the original like this:
function wpCustomSeoFocusKW($return = false)
{
$focuskw = wpseo_get_value('focuskw', $post->ID);
if ($return) {
return $focuskw;
} else {
echo $focuskw;
}
}
I have the following situation.
If there isn't an image in the DB, the page it's on shows a big image placeholder. What is the best way to hide the image placeholder if an image doesn't exist?
<img src="<?php echo '../img/artists/' . $row_rsAccents['artistPhoto']; ?>" width="100%"/>
http://westerndesignconference.com/intheloop/
You can do this with a simple if/else statement like so:
//I prefer to set things with variables
$placeholder_img = "../img/artists/placeholder.jpg";
$db_img = $row_rsAccents['artistPhoto'];
if($db_img){
$img_src = $db_img;
} else {
$img_src = $placeholder_img;
}
echo "<img src='$img_src' alt='' width='100%' />";
If there is a value returned - show an image. If the condition fails, no <img> will be displayed, preventing the blank gap
if (isset($row_rsAccents['artistPhoto'])) {
echo '<img src="../img/artists/' . $row_rsAccents['artistPhoto'] . '" width="100%"/>'
}
if (file_exists('artist.jpg') {
echo "<img src='artist.jpg'>";
}
else {
echo "<img src='default.jpg'>";
}
My following code is based on
1.Get current URL
2.Go through array and check if in url value = to value in array
do this:
$on_this_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
foreach ($city_array as $sandwich) {
if (strpos($on_this_link, $sandwich) == true) {
$sandwich = trim($sandwich, '/');
$city = $sandwich;
if ($city == 'newyork') {
foreach ($category_array as $double_sandwich) {
if (strpos($on_this_link, $double_sandwich) == true) {
$double_sandwich = trim($double_sandwich, '/');
$category_is = $double_sandwich;
Loader::model('page_list');
$nh = Loader::helper('navigation');
$pl = new PageList();
$pl->filterByAttribute('city', '%' . $city . '%', 'like');
$pl->filterByAttribute('category','%'.$category_is.'%','like');
$pl->sortByDisplayOrder();
$pagelist = $pl->get();
foreach ($pagelist as $p) {
echo '<li> ' .htmlspecialchars($p->getCollectionName()) . ' </li>';
?>
}
}
}
}
So It will show me only pages that have the same attribute with URL
Each of this page has image attribute that I want to show.
How can I pass this Image Attribute??
Check out the comments in the page list block's view template:
https://github.com/concrete5/concrete5/blob/master/web/concrete/blocks/page_list/view.php#L33
You can get image attributes by putting some code like this inside your foreach ($pagelist as $p) loop:
$img = $p->getAttribute('example_image_attribute_handle');
if ($img) {
//you could output the original image at its full size like so:
echo '<img src="' . $img->getRelativePath() . '" width="' . $img->getAttribute('width') . '" height="' . $img->getAttribute('height') . '" alt="" />';
//or you could reduce the size of the original and output that like so:
$thumb = Loader::helper('image')->getThumbnail($img, 200, 100, false); //<--200 is width, 100 is height, and false is for cropping (change to true if you want to crop the image instead of resize proportionally)
echo '<img src="' . $thumb->src . '" width="' . $thumb->width . '" height="' . $thumb->height . '" alt="" />';
}
Thx but I did it already just another way!
I didnt used
Loader::model('page_list');
Instead I used:
$blockType = BlockType::getByHandle('page_list');
And hardcoded the block!
$th = Loader::helper('text');
$ih = Loader::helper('image');
$page_current = Page::getCurrentPage();
$page_2 = $page_current->getCollectionHandle();
$img = $page->getAttribute('product'); $thumb =
$ih->getThumbnail($img, 240,150, false);
And after I changed a little bit my Code above:
$on_this_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
foreach ($city_array as $sandwich) {
if (strpos($on_this_link, $sandwich) == true) {
$sandwich = trim($sandwich, '/');
$city = $sandwich;
if ($city == 'newyork') {
foreach ($category_array as $double_sandwich) {
if (strpos($on_this_link, $double_sandwich) == true) {
$double_sandwich = trim($double_sandwich, '/');
$category_is = $double_sandwich;
$city_att = $page->getAttribute('city', '%' . $city . '%', 'like');
$sub_cat_att = $page->getAttribute('category','%'.$category_is.'%','like'); ?>
<?php if($city == $city_att && $category_is == $sub_cat_att){ ?><li><img src="<?php echo $thumb->src ?>" width="<?php echo $thumb->width ?>" height="<?php echo $thumb->height ?>" alt="" />
<h3> <?php echo $title ?></h3>
<div class="product_description">
<?php echo $description ?>
</div>
Read More...
</li> <?php } ?> <?php
}
}
}
So everything it is working But still thx for respond! Apreciate
I am currently trying to add in alt img tags for an existing website running the interspire shopping cart platform, I have gotten pretty close I bleieve, but i cannot seem to get it right. Any help would be grealty appreciated.
// Is there a thumbnail image we can show?
$thumb = $GLOBALS['ISC_CLASS_PRODUCT']->GetThumb();
$alttext = $GLOBALS['ISC_CLASS_PRODUCT']->GetProductName();
if ($thumb == '' && GetConfig('DefaultProductImage') != '') {
if (GetConfig('DefaultProductImage') == 'template') {
$thumb = GetConfig('ShopPath').'/templates/'.GetConfig('template').'/images/ProductDefault.gif';
} else {
$thumb = GetConfig('ShopPath').'/'.GetConfig('DefaultProductImage');
}
$thumbImage = '<img src="'.$thumb.'" alt="->GetProductName" />';
} else if ($thumb != '') {
$thumbImage = '<img src="'.GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'.$thumb.'" alt=""'.$alttext.'" />';
}
I have tried posting the code but it says new users cannot post image tags for some reason
It looks to me like you have two double quotes after you open the alt attribute, then the text, then another closing quote.
This line won't work
$thumbImage = '<img src="'.$thumb.'" alt="->GetProductName" />';
You probably want something like this
$thumbImage = '<img src="'.$thumb.'" alt="'.$GLOBALS['ISC_CLASS_PRODUCT']->GetProductName().'" />';
//or as you have already set $alttext:
$thumbImage = '<img src="'.$thumb.'" alt="' . $alttext . '" />';
ylebre meant: (scroll the code box to the right)
} else if($thumb != '') {
$thumbImage = '<img src="'.GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'.$thumb.'" alt=""'.$alttext.'" />';
}
^
|
there is one extra " at the end!
} else if($thumb != '') {
$thumbImage = '<img src="'.GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'.$thumb.'" alt="' . htmlspecialchars($alttext) . '" />';
}