Add image alt to WP Post Thumbnails - php

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;
}
}

Related

How can I call a function action anywhere on a form?

I have a function that shows the uploaded avatar and remove button in case you want to delete the avatar. Everything works correctly. My problem is that the displayed avatar and remove button are merged together, if I want to move the remove button somewhere else I can't do it.
So I'm looking for a way to call the remove button wherever I want. I was thinking of js solutions to change the html structure as a last hope and solution, but I don't want to do that.
Anyone have any ideas on how I could move the remove button elsewhere? The function is located in functions.php, the destination I should move the button to is the form-edit-account.php template which I customized to my needs.
The line i want to move is this:
echo '<a href=' . wc_get_account_endpoint_url('impostazioni') . '?rm_profile_image_id=' . $attachment_id . '> ' . __('Remove') . ' </a>';
This is a complete function:
// Display / Remove Avatar
function action_woocommerce_edit_account_form($size) {
// Get current user id
$user_id = get_current_user_id();
// Get attachment id
$attachment_id = get_user_meta($user_id, 'image', true);
// True
if ($attachment_id) {
$original_image_url = wp_get_attachment_url($attachment_id);
// Display Image instead of URL
echo wp_get_attachment_image($attachment_id, $size = array('150', '150')); // Invece dell'array size, stava 'full' come parametro.
if (isset($_GET['rm_profile_image_id'])) {
if ($attachment_id == $_GET['rm_profile_image_id']) {
wp_delete_attachment($attachment_id);
//delete_user_meta($user_id, 'image');
if (delete_user_meta($user_id, 'image')) {
wp_delete_attachment($attachment_id);
}
?><script>window.location='<?php echo wc_get_account_endpoint_url('impostazioni') ?>';</script><?php
exit();
}
} else {
echo '<a href=' . wc_get_account_endpoint_url('impostazioni') . '?rm_profile_image_id=' . $attachment_id . '> ' . __('Remove') . ' </a>';
}
}
} add_action( 'woocommerce_edit_account_form_start', 'action_woocommerce_edit_account_form' );
Ok, it's weird but it works, I put the function directly inside the form. It's not a very "clean" solution, but it's working for me. If anyone thinks I am doing something wrong I appreciate any suggestions and corrections.
<form name="Form" class="mts-edit-account" action="" method="post" <?php do_action( 'woocommerce_edit_account_form_tag' );?> >
<!-- Avatar Remove button -->
<div class="global_container avatar">
<?php
if (isset($_GET['rm_profile_image_id'])) {
if ($attachment_id == $_GET['rm_profile_image_id']) {
wp_delete_attachment($attachment_id);
//delete_user_meta($user_id, 'image');
if (delete_user_meta($user_id, 'image')) {
wp_delete_attachment($attachment_id);
}
?><script>window.location='<?php echo wc_get_account_endpoint_url('impostazioni') ?>';</script><?php
exit();
}
} else {
echo '<a href=' . wc_get_account_endpoint_url('impostazioni') . '?rm_profile_image_id=' . $attachment_id . '> ' . __('Remove') . ' </a>';
} ?>
</div>
//Other fields........
</form>

Display different content depending on post author

I am trying to display a different image on my page depending on who the Wordpress author of the post is.
So far I have tried a few scripts but none of them work. Any help is greatly appreciated. Here is what I am trying to do.
<?php $author = get_the_author(); ?>
<?php
if ( $author('author1') ) {
echo '
<img src="">;
'
} elseif ( $author('author2') ) {
echo '
<img src="">;
'
} else {
// if neither, echo something else
}
?>
The get_the_author() function return the author's display name as a string. So you have to simply compare the result of get_the_author(). There is no array or object as return value.
So I would go with the following solution using a switch instead of if:
<?php $author = get_the_author(); ?>
<?php
switch($author) {
case 'author1':
echo '<img src="">';
break;
case 'auhtor2':
echo '<img src="">';
break;
default:
// if neither, echo something else
}
?>
In case you want to use the if statement you can use the following:
<?php $author = get_the_author(); ?>
<?php
if ($author === 'author1') {
echo '<img src="">';
} elseif ($author === 'author2') {
echo '<img src="">';
} else {
// if neither, echo something else
}
?>

Image URL appears in a wrong place in WordPress shortcode output

I'm trying to make a shortcode for displaying logged in user avatar from buddypress
// user avtar
function user_avatar() {
$user_avatar_url = bp_loggedin_user_avatar('type=full&html=false');
$user_image = '<img src="' . $user_avatar_url . '"/>';
return $user_image;
}
add_shortcode('avatar', 'user_avatar');
but when I try to insert shortcode [avatar], the HTML output looks like this:
http://localhost/.../images/bp_default_avatar.jpg <img src=""></img>
Somehow image url/src appears before 'img' tag?
Try using $userimage = "<img src='$user_avatar_url' />";
instead
Apparently it was the buddypress issue, I changed a few things and now it's working
// user avtar
function user_avatar() {
$userid = bp_loggedin_user_id();
$user_avatar_url = $avatarurl = bp_core_fetch_avatar( array( 'item_id' => $userid, 'html' => false ) );
$user_image = '<img src="' . $user_avatar_url . '"/>';
return $user_image;
}
add_shortcode('avatar', 'user_avatar');
try like these
<?php
function user_avatar()
{
$user_avatar_url = bp_loggedin_user_avatar( 'type=full&html=false' );
$user_image = '<img src="' . $user_avatar_url . '"/>';
return $user_image;
}
add_shortcode('avatar', 'user_avatar');
?>

Wordpress Custom Field using Advanced Custom Fields

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\" />";

In template page_list get image attribute Concrete5

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

Categories