Getting image alt text in Wordpress slider - wordpress

Our web site is using the theme's slider.php on our front page. I'd like for the images displayed in that slider to use the alt text that is in the post meta (edited in the WP media library).
I've attempted to create a variable ($alt), and have that variable echoed when the img is called, but is coming up empty. Here's the relevant portion of slider.php:
$alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
<img src="<?php echo $thumbnail[0]; ?>" alt="<?php echo $alt[0]; ?>" />
</a>
<?php else: ?>
`
Any idea what I'm doing wrong?

You have set the third argument as true which means the function get_post_meta returns a string;
http://codex.wordpress.org/Function_Reference/get_post_meta
Therefore the following should work instead;
<img src="<?php echo $thumbnail[0]; ?>" alt="<?php echo $alt; ?>" />

Okay - so I've figured this out. The code above is trying to get the post meta from the thumbnail of the image - and, to my knowledge, the thumbnail image doesn't have meta data.
I've just created a custom field, and now the slider loop call thats field.

Related

Pulling Single Post Featured Image under a Category List

I have this code in my child theme but it's pulling in the category featured image instead of the individual single posts image, when viewing a list of single post under category. The code is within a loop.
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail( "full" ); ?>
<?php } ?>
If anybody gets caught up on retreiving an image outside of a single post this solution worked for me.
<img src="<?php $image_source = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); echo $image_source[0]; ?>">
The $post->ID may have to change depending on what you're working on.

Wordpress Header Image not showing alt text

my theme is showing the alt text for the main header image as alt="". Even though I have added this in the media library etc.
I have found the PHP code in the header.PHP file as below. What do I need to change to manually put the alt text in?
<img class="logo" src="<?php echo esc_url(($site_logo)); ?>" alt="<?php bloginfo('sitename'); ?>" />
Thanks
You can't use sitename in <?php bloginfo('sitename'); ?> for example you can use <?php bloginfo( 'name' ); ?>
Below values what you can use.
https://developer.wordpress.org/reference/functions/bloginfo/#possible-values-for-show

Addition of Some Image Next to "Add to Cart" Button

I need an image to be appeared next to Add to Cart button in WooCommerce product page, using Advanced Custom Fields plugin. In order to displaying values, as I know, the_field function must be added to some page template. In Which template file and where should I place the the_field function?
It depends on what type of "add to cart" you are using (simple product, variable, etc).
Regardless, each are in this directory (for single-product): templates/single-product/add-to-cart.
You should (for best results) use the image array for your ACF content.
You would write this (changing the get_field('image') to get_field('your_field_name'):
<?php
$image = get_field('image'); // assigns the image field to the variable of $image
if( !empty($image) ): ?> <!--if the $image variable isn't empty, display the following:-->
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /> <!--displays the URL for the image variable and also the alt tag which is entered in the WordPress media library-->
<?php endif; ?> <!--ends the if statement -->
If you wanted a basic display, you could use image URL and do this:
<?php if( get_field('image') ): ?> <!--only shows the image field if it exists (an image was uploaded through ACF)-->
<img src="<?php the_field('image'); ?>" /> <!--displays the URL of the image if it is not an array/ID set in ACF field parameters, but a URL -->
<?php endif; ?> <!-- end the IF statement -->
I prefer using the alt tag when at all possible.

wordpress -> showing custom data from child pages + pagination

You can see here what i am doing:
http://www.arvag.net/otkrijte-svet/leto/
So its pulling 2 custom fields from child pages, one of them is just url for the image on the left, and the other field is that text showing on the right.
Now, what i want to do is to add pagination there. The code i have now will just simply show all child pages, but i want to show only 5 child pages, so if user want to see 6th child page he would have to click on link for "Page 2" and so on.
The code im using to display these child pages is this:
<?php
$pages = get_pages('child_of='.$post->ID.'&sort_column=post_title&sort_order=desc');
$count = 0;
foreach($pages as $page)
{
$short_info = get_post_meta($page->ID,'info',true);
$image = get_post_meta($page->ID,'slika',true);
$count++;
?>
<div class='preview_slika left'><img src="<?php echo $image ?>" alt="<?php echo $page->post_title ?>" /></div>
<div class='preview_info right'>
<h2><?php echo $page->post_title ?></h2>
<p><?php echo $short_info ?></p>
Više o >>
</div>
<div class='clear'></div>
<?php
}
?>
So any idea what to do to get what i need?
Take a look at wp-PageNavi plugin. And this blog post if you want to integrate it in your theme an not have another plugin to manage.
Some guy posted some sample code on wordpress forums which works much better for me than this plugin.
http://wordpress.org/support/topic/383773

Wordpress: How to get a thumbnail post plugin to work

I need a second opinion on something. I am trying to use the WP Post plugin as described in the following link:
http://www.seoadsensethemes.com/wordpress-wp-post-thumbnail-plugin/
You will see a section toward the bottom stating that you can call the feature using the following code:
$Wppt->get_post_thumbnail( $post->ID, 'post-thumbnail-square' );
It shows the following example inserted into the post loop:
<!-- wp post thumbnail -->
<?php if ( function_exists( "$Wppt->get_post_thumbnail" ) ) {
$thumb = $Wppt->get_post_thumbnail( $post->ID, 'thumbnail-custom-key-name' );
if ( !empty( $thumb ) ) { ?>
<img class="thumbnail" src="<?php echo $thumb['url']; ?>" title="<?php echo $thumb['title']; ?>" alt="<?php echo $thumb['alt']; ?>" width="<?php echo $thumb['width']; ?>" height="<?php echo $thumb['height']; ?>" />
<?php }
} ?>
<!-- wp post thumbnail -->
I cannot get this to work. Can someone tell me how this should specifically be used? I am simply pasting the code above my loop on the default theme, replacing the 'thumbnail-custom-key-name' with the one assigned to my thumbnail. Is this the right way to do it?
In your sample code it looks like you're using a default value 'thumbnail-custom-key-name' is that the name of an actual preset you've made?

Resources