How to catch alt content from wp post? - php

I would like to use alt attribute tags with image on the main site.
I use code that calls for pictures on my index page from latest wordpress posts. I use this code for index.php:
<img src="<?php echo catch_that_image() ?>" width="250" alt="">
The code in the functions.php file for catch that image is:
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
What I want to achieve is to get the alt tag from that post, not only the image. Actually some kind of "catch that alt". The new code in index.php would be
<img src="<?php echo catch_that_image() ?>" width="250" alt="text from wordpress post">
How can can I add anything to functions.php to get that alt text together?
The purpose is that google often search for images and doesn't find alt tags on the main site but only on individual wordpress posts.

you can add the following code in your functions.php to enable that.
function add_img_title($attr, $attachment = null) {
$img_title = trim(strip_tags($attachment->post_title));
$attr['alt'] = $img_title;
$attr['title'] = $img_title;
return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'add_img_title', 10, 2);

Related

Dynamically change image titles and alt tags through PHP - On html render, not in database

I want to re-title my image's alt tags etc based on the page they are being displayed on. For SEO Purposes.
I've tried to write a PHP function to do so and call the function on page load with "alt=<?php echo $post_title ?>" and "title=<?php echo $post_title ?>" but nothing is working.
How would you do it? PHP or jQuery... or?
Here's one of the functions I tried to manipulate and turn into something that works... I did get it to change the alt tags but I havent had any success beyond that.
function isa_add_img_title( $attr, $attachment = null ) {
$img_title = trim( strip_tags( $attachment->post_title ) );
$attr['title'] = $img_title;
$attr['alt'] = $img_title;
return $attr;
}
add_filter('wp_get_attachment_image_attributes','isa_add_img_title', 10, 2);
It appears this post may have had the answer but the link in the answer is now broken:
Dynamically filling image alt tags with the page title, PHP
Thanks so much! Appreciate any help.
This will set the alt and title of every image on the page to the post title.
function duck_diver_add_img_title( $attr, $attachment = null ) {
// Fetch the global post object. This is what the page it's on is.
global $post;
// Get the post title.
$img_title = wp_kses_post( $post->post_title );
$attr['title'] = $img_title;
$attr['alt'] = $img_title;
return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'duck_diver_add_img_title', 10, 2 );
<!DOCTYPE html>
<?php
$test ="Test";
?>
<html>
<head>
<title><?php echo "$test" ?></title>
</head>
<body>
<div class="center">
<img src="ribbn2.jpg" alt=<?php echo "$test" ?> width="100%" height="100%">

add a no image placeholder on elementor pro post element if there is no featured image

is there filter of some sort that can add a image if there is no featured image present when using the Elementor pro "post" element.
Because the title goes up if there is no image placed and it breaks the sites display
want to add a placeholder image like below when no featured image is available
You can add this filter to you theme functions.php :
function mlnc_filter_post_thumbnail_html( $image_placeholder ) {
// If there is no post thumbnail,
// Return a default image
if ( '' == $image_placeholder ) {
return '<img src="' . get_template_directory_uri() . '/images/default-thumbnail.png"/>';
}
// Else, return the post thumbnail
return $image_placeholder;
}
add_filter( 'post_thumbnail_html', 'mlnc_filter_post_thumbnail_html' );
Another way is to go where the image is outputting and add an If statement like below:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg"/>
<?php } ?>

modify html output when add image in posts and pages

i'm trying to edit the output of add image in posts the normal code from wordpress is
<img src="http://huemix.ly/wp-content/pics/pic.jpg" />
i want to replace that output with
<div class="huemix">
<img class="posts-img" src="http://huemix.ly/wp-content/pics/pic.jpg" />
<a href="http://huemix.ly/wp-content/pics/pic.jpg" class="fancybox" ></a>
<div class="fancy"></div>
</div>
all my try goes down :(
You need to search proper chunk of code in wordpress functions file. The file should be named post.php and should be located in wp-includes. The function name should be wp_insert_attachment().
Or using filters:
<?php
add_filter( 'image_send_to_editor', 'my_image_func', 10, 7);
function my_image_func($html, $id, $alt, $title, $align, $url, $size ) {
$url = wp_get_attachment_url($id); // Grab the current image URL
$html = "<img src="$url" class="uhuhu"/>";
return $html;
}
?>

Geting the first image url from a post?

I am trying to create a dynamic website, I have a database with some news, and I wanted to get the first image from the post content
The news are added to this database with wordpress, as you know wordpress images are inside the post_content.
Please can you help me to get the first image of the post and dispay the link.
Thank you
Put this in your functions.php file:
function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
and then simply call the function on the page you need to get the first image of the post like this:
<?php echo get_first_image() ?>

Query to show images with recent posts in Wordpress sidebar/widget

To show recent items from a Wordpress category in a widget I'm using this code...
<ul>
<?php $recent = new WP_Query("cat=1231&showposts=5"); while($recent->have_posts()) :
$recent->the_post();?>
<li><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul>
...but how can I make this query also display the first image in each post, and is there any way to set a 'default' image in case there is no image?
Is there also a way to use thumbnails here, rather than loading the full size image and using HTML to resize?
This is probably what you are looking for, found here
function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i’, $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = “/images/default.jpg”;
}
return $first_img;
}

Categories