PHP if/else get_field() with WordPress - php

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.

Related

Using Advanced Custom Field's Repeat to create a gallery

I'm using Owl Carousel to create a carousel gallery of logos. I'm using Advanced Custom Field's repeater feature to grab all of the urls of the logos. Here's the markup for the gallery:
<div id="clients">
<?php
$clients = get_field('client_carousel');
$client_logo = get_field('client_logo');
if( $clients ) {
?>
<section>
<div id="owl-clients" class="owl-theme owl-carousel">
<?php foreach( $clients as $client_logo ): ?>
<div class="item">
<img class="featurette-image img-responsive center-block" src="<?php echo $client_logo['url']; ?>" alt="<?php echo $client_logo['alt']; ?>"></div>
<?php endforeach; ?>
</div>
</section>
<?php } ?>
</div><!--end clients-->
The repeater field name is client_carousel and the field name of the field within it is client_logo.
I have a working demo here. It recognizes the seven logos in the group, but the only thing I can't get is the url for the img src.
Couple things to note. You're overriding the $client_logo variable, so not sure if that was just an abandoned artifact in the code since it doesn't look like you use it anyhow:
$client_logo = get_field('client_logo');
<?php foreach( $clients as $client_logo ): ?>
Other thing is ACF repeater field allows you to choose the output of the field type "image" - assuming that's what you set for the "url" field. Now, suppose you set "url" as an image, you can then select the output as Image URL instead of the default Image Array if you want the raw URL returned.
Supposing that's how you set it, then it should give you the image url without having ot modify your code:
<?php echo $client_logo['url']; ?>
However, we don't know the architecture of your repeating field and without that knowledge we wouldn't be able to advise further. Update your post and I can update this answer if this info doesn't point you in the right direction.

wordpress advanced custom fields background image

I am trying to set an image uploaded through custom fields plugin and have it display as the background of a div (which is used in a slider).
However the image is not displaying...I have text in the custom fields and that is showing okay so I think its something to do with the line of code I am using to pull in the image.
I am trying to set the background of .slide1 with the image.
The custom field name is slide1_background.
HTML:
<div class="slide1" style="background-image:url('<?php the_field('slide_bg1'); ?>');">
<div class="slide1-cont"><p class="slide-text">
<h1><?php the_field('slide_title1'); ?></h1>
<img src="<?php bloginfo('template_directory')?>/images/line.png" /></p>
<p><?php the_field('slide_content1'); ?></p></div>
</div>
CSS:
.slide1{
background-repeat:no-repeat;
background-size:cover;
background-position:center;
height: 800px;
}
Look at the difference in your code in your question, where you try to set the background-image, compared to the code in your comment in another answer where you're setting it as an image source.
the_field('slide_bg1') returns an array, so you're trying to set the background image source as a PHP array which gets converted to a string as "Array" so in your HTML it'll look like: background-image:url('Array')
You need to get the field first, then echo the url element of the returned array as the source of the background image:
$image = get_field( 'slide_bg1' );
if ( !empty( $image ) ) { ?>
<div class="slide1" style="background-image:url('<?php echo $image['url']; ?>');">
<?php }
Use echo
<div class="slide1" style="background-image:url('<?php echo the_field('slide_bg1'); ?>');">

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']

ACF Gallery thumbnails (image included)

I am trying to achieve the below image:
Basically, I have set up ACF Gallery, and used the code below:
<?php $images = get_field('gallery'); if( $images ): ?> <!-- This is the gallery filed slug -->
<?php foreach( $images as $image ): ?> <!-- This is your image loop -->
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /> <!-- Image Code -->
<?php endforeach; ?> <!-- This is where the image loop ends -->
<?php endif; ?> <!-- This is where the gallery loop ends -->
Which now lists all the images in the gallery. What I am looking for, is the first image to be full size, and the other 3 images to be thumbnails, like the image, which when clicked, change the full size image.
Anyone have any ideas?
EDIT:
Im open to another way of doing this
Normally I wouldn't post all of this, as it would require creating a lot of stuff, but luckily I just made something similar for work yesterday.
<?php $galleryImages = get_field('gallery'); ?>
<div id="largeGalleryImage">
<img src="<?php echo $galleryImages[0]['sizes']['gallery-full']; ?>" id="galleryImageLarge" />
</div>
<div id="galleryThumbs">
<?php $i = 0;
foreach($galleryImages as $galleryThumb){
$i++;
if($i==1){
$imgClass = 'active';
}else{
$imgClass = '';
}
echo '<img src="'.$galleryThumb['sizes']['gallery-thumb'].'" class="imageThumb imageNum'.$i.' '.$imgClass.'" picURL="'.$galleryThumb['sizes']['gallery-full'].'" />';
} ?>
</div>
<script type="text/javascript">
//Setup Gallery Clicks
$("#galleryThumbs .imageThumb").click(function () {
if ($(this).hasClass('active')) {
//do nothing
} else {
var newImage = $(this).attr('picURL');
$('#galleryImageLarge').attr('src', newImage);
$("#galleryThumbs .imageThumb").removeClass('active');
$(this).addClass('active');
}
});
</script>
In this example, "gallery-full" is a set custom image size for the large photo, and "gallery-thumb" is a set custom image size for my thumbnails.
The thumbnails have an attribute applied to them, called "picURL" and it houses the URL of the large image. The large image is auto-filled with the URL of the first image. Then, using jQuery, when a thumb is clicked, it changes the src value of the large image with the value of the thumbnail's "picURL".
It also gives the current thumbnail an "active" class, to allow for styling your current thumb.

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