Display repeater field of ACF - php

i´m trying to display a repeater field made of images in my wordpress template but it just doesn´t work. I tried reading the doc of the plugin:
http://www.advancedcustomfields.com/add-ons/repeater-field/
But still can´t make it work.
Here it´s the markup:
<div id="slideshow">
<ul id="slides">
<li><img src="<?php the_repeater_field('home_slider'); ?>" alt="" /></li>
</ul>
</div>
The name of the repeater field is "home_slider" and the name of the field is "home_image". Please can someone help me out?
This is what displays after load the page:
<img src alt>

The repeater-field stores everything in a array, so you need to retreive it then loop it out, try this:
$slides = get_field('home_slider'); // Grabs the array
// Check if there is any data in the array before looping
if($slides) {
echo '<ul id="slideshow">';
foreach($slides as $s) {
echo '<li>';
echo '<img src="'.$s['home_image'].'" alt="" />';
echo '</li>';
}
echo '</ul>';
}
Now depending on what you set the image to return (image url, attachement ID or Image Object ) you will need to use different methods to get the path to the image.

Related

Display all users/authors in wordpress template

I'm trying to display all my authors/users in a custom page template using advanced custom fields. I'm using the following code to display their profile photos that grab from an acf image field called author_header. I put the users in an unordered lists by the following.
<div class="author">
<ul>
<li>
<?php
$publisher_photo = get_the_author_meta('author_header');
$image_src = wp_get_attachment_image_src($publisher_photo);
echo '<img src="'. $image_src[0] .'" />';
?>
</li>
</ul>
</div>
The problem I'm facing is that all the users get my profile photo. I want to be able to just grab the ones that they uploaded to the author_header field.
I have also tried this revised code but the img src doesn't show up.
<?php
$field_name = "author_header";
$post_id = "user_{$user_id}";
$publisher_photo = get_field($field_name, $post_id);
$image_src = wp_get_attachment_image_src($publisher_photo);
echo '<img src="'. $image_src[0] .'" />';
?>
I do have all of their names correctly displaying in the unordered list by the following.
<h2 class="authorName">[ <?php echo $user->display_name; ?> ]</h2>
You should add the user ID in get_the_author_meta like this:
$user_id = $user->ID;
$publisher_photo = get_the_author_meta('author_header', $user_id);
Please make sure that you will add this code in the loop, so user_id will be different for different user every time.
Regards,
Stanimir

Advanced Custom Fields repeater image caption

I am trying to display an image caption for a wordpress Advanced Custom Fields (ACF) repeater field, and haven't had any luck with the follow three options:
<?php if($middle_image['image']): ?>
<?php $midimage = wp_get_attachment_image_src($middle_image['image']); ?>
<?php $caption = $midimage->post_excerpt; ?>
<?php $captiontwo = $middle_image['image']['caption'] ?>
<?php $captionthree = $middle_image['image']->post_excerpt; ?>
<?php $alt = get_post_meta($middle_image['image'], '_wp_attachment_image_alt', true); ?>
<?php $main_image = wp_get_attachment_image_src($middle_image['image'], 'two-column-cropped' ); ?>
<div class="two-column-cropped"><img src="<?php echo $main_image[0]; ?>" alt="<?php echo $alt ?>" />
<?php if($caption) { ?>
<div class="image_caption"><?php echo $caption; ?></div>
<?php } ?>
</div>
<?php endif; ?>
Any suggestions would be appreciated. Thanks.
wp_get_attachment_src() does not fetch any data about an image besides the URL, height and width. You might want wp_get_attachment_metadata() however I think you would be better off changing your ACF field to return an Image Object (actually an array) instead of the Image ID (as I assume you have it now).
ACF can return any of three things for an image field: The image src URL, the Attachment ID (which can be passed to functions such as wp_get_attachment_image_src()), or the attachment information as an array.
You can check that you are receiving the correct response from ACF by using var_dump($middle_image) or var_dump($midimage)
I assume that $middle_image is your repeater field.
As long as ACF is configured correctly to return the image object (instead of the image URL or ID), you can simply remove this line:
<?php $midimage = wp_get_attachment_image_src($middle_image['image']); ?>
and then access the image caption using $middle_image['image']['caption']

PHP if/else get_field() with WordPress

I'm trying to give a client some options in the WP backend to customize a carousel that is displayed on their website's homepage. I am using Advanced Custom Fields to handle getting the input from the client. I want to give them two options:
Option #1) Allows the client to insert a string of text to be displayed ('carousel_title_text')
Option #2) Allows the client to upload a logo to be displayed ('carousel_logo')
I want the code to check for title text, and if there is none, display the logo instead. I haven't yet decided what will happen if both fields are empty. Anyway, here is what I've come up with:
<?
if( get_field('carousel_title_text') ) { ?>
<h2 class="promo"><?php the_field('carousel_title_text');?></h2>
<? } elseif( get_field('carousel_logo') ) {
the_field('carousel_logo');
}
?>
The 'carousel_title_text' displays as long as there is an input, but when it's empty and 'carousel_logo' is not, it does not properly output the logo. Can anyone tell me what I'm doing wrong or if there's a better approach?
Thanks in advance,
If I get it right, your problem is not with the If/Else statement but with the logo field. I assume you properly configured this field (carousel_logo) as Image in ACF.
Image fields offer three different Return Value possibilities:
Note: In some versions of ACF you'll found Image Object instead of Image Array, but it's conceptually the same.
None of them will show your image properly if you just use the_field('carousel_logo'); in your template. So, how do you get your logo?
If you want the easiest solution, choose Image URL as Return Value for your logo field and print it like that:
<?
$title = get_field('carousel_title_text');
$logo = get_field('carousel_logo');
if (!empty($title)) : ?>
<h2 class="promo"><?= $title ?></h2>
<?php elseif (!empty($logo)) : ?>
<img class="logo" src="<?= $logo ?>">
<?php else: ?>
<!-- No title, no logo -->
<?php endif ?>
I've changed a little your code, but basically the key line here is:
<img class="logo" src="<?= $logo ?>">
This, or the equivalent:
<img class="logo" src="<?php echo $logo ?>">
should show your image when the title is missing.
Bonus: If you're handling different thumbnail sizes I'd recommend you to use Image Array as Return Value. In that case, $logo would be an Array containing all information (such as sizes, all thumbnail URLs, captions, etc.)
Use:
<img class="logo" src="<?= $logo['sizes']['medium'] ?>">
where 'medium' is your thumbnail size label to output it.
Also, please check official docs on ACF's Image field type for more examples and further explanations.
You have put else condition on last, that will default text will show if client will not Enter text ot logo.
you will used bellow code.
<?php
if( get_field('carousel_title_text') ) { ?>
<h2 class="promo"><?php the_field('carousel_title_text');?></h2>
<?php } elseif( get_field('carousel_logo') ) {
the_field('carousel_logo');
}else {
echo " <h2 class="promo"> Default slider title </h2>"
}?>
it's default title client indicate to he has not entered slider text
Hope it's work for you.
Try:
<?php
$output = (get_field('carousel_title_text')) ? get_field('carousel_title_text'):get_field('carousel_logo');
echo $output;
?>
This sounds like it's to do with the settings for the logo, which I'm assuming is an image field? In the Wordpress admin there will be a setting under that field called 'Return value' which can be set to either 'Array' or 'URL'.
If this is set to 'Array', you'd need to store that array to a variable and then access its properties in your markup like so:
<?php
if ( get_field('carousel_title_text') ) { ?>
<h2 class="promo"><?php the_field('carousel_title_text'); ?></h2>
<?php } else{
$carousel_logo = get_field('carousel_logo');
if ( $carousel_logo ) { ?>
<img src="<?php echo $carousel_logo['url']; ?>" />
<?php } else { ?>
<h2 class="promo">Default slider title </h2>
<?php }
}
?>
Alternatively, you could set your field to only return the image URL and then access it like so:
<?php
if ( get_field('carousel_title_text' ) { ?>
<h2 class="promo"><?php the_field('carousel_title_text'); ?></h2>
<?php } elseif ( $carousel_logo ) { ?>
<img src="<?php the_field('carousel_logo'); ?>" />
<?php } else { ?>
<h2 class="promo">Default slider title </h2>
<?php }
?>
ACF's documentation on the Image field will provide more info on this.

Display an image alt or title below the image and in h1 html tag

I've got an image carousel in a site. I want to display the image alt tag or title below the image when I do mover over. At the same time I want to display that same name in another h1 tag.
The code is in PHP and Magento implementation. I know it is a simple trick. But I tried all the codes using jQuery after, append and replacewith functions. These functions are working. But I couldn't display the alt or title name.
Php Code:
<ul id="mycarousel" class="jcarousel-skin-tango alt">
<?php
// products
if(!isset($CatID))
$CatID = 10; // Set to classic collection if empty;
$_category = Mage::getModel('catalog/category')->load($CatID);
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('status', 1)//enabled
->addCategoryFilter($_category);
foreach($products as $index=>$prod):
$product = Mage::getModel('catalog/product')->load($prod->getId());
echo '<li style="border: none;">
<a class="thumbActImg1" href="'.$product->getProductUrl().'" data-rel="'.(string)Mage::helper('catalog/image')->init($product, 'small_image')->keepFrame(FALSE)->resize($_resize, $_resize).'" alt="'. $product->getName() .'">
<div class="thumbActImg2">
<img src="'.$product->getThumbnailUrl(80, 80).'" alt="'. $product->getName() .'" />
</div>
</a>
</li>';
endforeach;
?>
</ul>
jQuery code:
jQuery(this,'img.thumbActImg2').after('<div class="pname"><?php echo $product->getName()?></div>');
jQuery('#pro_name h1').replaceWith('<h1><?php echo $product->getName()?></h1>');
Right now, I used php echo in order to display name. But it displaying the current page product name instead of mover over image name.
Could anyone help me out please.
Hi,
the PHP-Code is evaluated once, when the site is generated. So the <h1><?php echo $product->getName()?></h1> is always the same text.
You have to rewrite/get the current information from the image, when it's cycled (if I understand you). So here is what may work for you:
//insert the code, where the carousel is triggered
//get the alt attribute from image and replace h1 content
jQuery('#pro_name h1').html(jQuery('img.thumbActImg2').attr('alt'));
I hope this is what you're looking for, the problem isn't descripted very briefly.
Greetings Mat

What is the php code I need to add to this file in order to get an image to appear in this custom post type?

I am using a wordpress theme that has a custom post type called "link" which simply produces a post where the title links to an outside link you specify. It also includes some text in the body of the post. I would like to also be able to display an image. Right now it is not showing the image.
Here is the code:
elseif ( has_post_format( 'link' )) {
if(get_post_meta($post->ID, '_format_link_url', true)!=''){
$link = get_post_meta($post->ID, '_format_link_url', true);
}else{
$link = '';
}
?>
<h2 class="entry-title">
<a href="<?php echo $link; ?>" target="_blank" title="Follow this link">
<?php the_title(); ?>
</a>
</h2>
<p>
Is there anything I can add to make it display an image as well as text?
Thanks!
I can't see anywhere on this example where you are using an tag to insert an image. The img tag has two required attributes, src and alt. Src is the path to your image, whereas alt is the alternate text for the image. So:
<img src="images/myImage.png" alt="This Is My Image"/>
Hope that helps!

Categories