getfield() not working in WordPress - wordpress

This is the testing code I wrote to display an ad on a site. It does not display the link and image. I have added the link and image using default settings.
<?php
if(1){
echo("<div style='position:absolute; right:142.5px; top:0px; top:303px;'>
<a href='");
echo get_field( 'add_ad_link' );
echo("'><img src=').get_field( 'ad_image' ).('></a></div>");
}
?>

If you are using the correct field names and this is within your loop then you just need to correct your quotes.
<?php if (1) : ?>
<div style='position:absolute; right:142.5px; top:0px; top:303px;'>
<a href="<?php echo get_field( 'add_ad_link' ); ?>">
<img src="<?php echo get_field( 'ad_image' ); ?>">
</a>
</div>
<?php endif; ?>
If you are going to be outputting a large amount of HTML it is sometimes easier to read if you just output the HTML instead of trying to echo a string of html. This makes it easier to keep your quotes in the right order.

Related

Wordpress - Need to pull ALT text from images

Can Somebody Help how to pull alt text from wordpress images in the following code??
<?php $logos = get_field('upload_logos');
$size = 'full';
if( $logos ): ?>
<div>
<div>
<?php foreach( $logos as $logo ): ?>
<div>
<div class="ccrc-logo-box-inner"><img src="<?php echo wp_get_attachment_url( $logo['ID'], $size ); ?>" alt=""></div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
You are using get_field() function, so I guess you have 'Advanced Custom Fields' plugin. You want to get the data of a field with type of image. So looking at the documentation:
https://www.advancedcustomfields.com/resources/image/
You can access the alt text from the field object, the same way you already did it with $logo['ID'] before.
<img src="<?php echo wp_get_attachment_url( $logo['ID'], $size ); ?>" alt="<?php echo $logo['alt']; ?>">
You are using the wp_get_attachment_image() function to output your url
Documentation of wordpress tells us you can pass the alt as a parameter to fetch and output it:
https://developer.wordpress.org/reference/functions/wp_get_attachment_image/#parameters

How can I get ACF data on WooCommerce template-homepage.php

I cannot get my repeater to work on template-homepage.php.
I have created a group called 'homepage_settings' and in this group is a repeater called 'categories'. I cannot get this content to show on the template-homepage.php file.
<ul class="medium-block-grid-3 small-block-grid-1" data-equalizer>
<?php
$rows = get_field('categories');
if($rows) {
foreach($rows as $row) {
?>
<li>
<a href="<?php echo $row['link']; ?>">
<span class="bg" style="background-image: url('<?php echo $row['image']; ?>')"></span>
<span class="title"><?php echo $row['category_name']; ?></span>
</a>
</li>
<?php
}
}
?>
</ul>
No content is being displayed and no errors?
It sounds like you need to grab "homepage_settings" and not "categories" first. Does this line of code print anything?
<?php
$rows = get_field('categories');
echo '<pre>'.print_r($rows,1).'</pre>';
?>
If that doesn't produce anything, then you are grabbing the wrong field. First grab homepage_settings and see if that has anything inside.
<?php
$rows = get_field('homepage_settings');
echo '<pre>'.print_r($rows,1).'</pre>';
?>
That should have your "categories" data inside (make sure you have put content into the fields in the CMS). Then, you can use a foreach to loop through it.
<?php
foreach($rows['categories'] as $row) {
echo '<pre>'.print_r($row,1).'</pre>';
}
?>
That should be it. Take it one variable at a time, and go from there.

displaying background image within loop and if statement wordpress

Im having trouble displaying my background image.
I have created a loop to display my posts and then trying to set background image, however it's breaking somewhere? Below is my code within the loop.
<div class="artist-feed">
<?php
$artistloop = new WP_Query( array( 'post_type' => 'artist') );
if ( $artistloop->have_posts() ) :
while ( $artistloop->have_posts() ) : $artistloop->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<div class="single-artist" style="background-image: url(<?php echo the_post_thumbnail(); ?>);">
<div class="artist-info">
<h2><?php echo get_the_title(); ?></h2>
</div>
</div>
</a>
<?php endwhile;
endif;
wp_reset_postdata();
?>
</div>
Your issue is with the call to the_post_thumbnail(). This method actually displays the thumbnail, not the URL for it. See here:
https://developer.wordpress.org/reference/functions/the_post_thumbnail/
To get the URL to your thumbnail, try get_the_post_thumbnail_url(). This should be what you need. See here:
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

remove space from an anchor link in wordpress

I need to give a unique id to an anchor tag, and have chosen to use the wordpress post title"
Details
<div id="<?php the_title(); ?>" class="content">
<?php the_content(); ?>
</div>
The problem is if there is a space in the title then the link doesn't work.
Q: Is there a better WP thing to call as a refernece (tried post ID but this didn't return anything), or a simple way to remove the space (if any)
Thanks all.
You can use php's str_replace function to replace the space to any other character
<?php $title = get_the_title();
$title = str_replace(' ', '_', $title); ?>
Details
<div id="<?php echo $title; ?>" class="content">
<?php the_content(); ?>
</div>

Advanced Custom Fields - Fancybox Slideshow on Clicking a Thumbnail

I'm trying to create a fancybox slideshow that pops up when you click on one thumbnail. I'm using Advanced Custom Fields with Gallery Field.
This is what I have;
<?php
$images = get_field('gallery');
$image_1 = $images[0];
?>
<a href="<?php echo $images['url']; ?>" rel="fancybox">
<img src="<?php echo $image_1['url']; ?>" /></a>
Unfortunately, nothing happens when you click on the imageā€¦
Any leads?
Thanks!
As Pranita said you should be using a for loop for generating the gallery.
If this display more thumbnails than you want, you can simply build your HTML/CSS so that every thumbnail, except the first, is hidden.
Use this, from the official documentation, and customize it to your needs.
<?php $images = get_field('gallery');
if( $images ) : ?>
<div id="carousel" class="flexslider">
<ul class="slides">
<?php foreach( $images as $image ): ?>
<li>
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
The problem is you have used $images['url'] where images is a multidimensional array. It has to be $image_1['url'];
Simply use for loop if you want every image. Else if you want only one image then use
<a href="<?php echo $images[0]['url']; ?>" rel="fancybox">
<img src="<?php echo $images[0]['url']; ?>" /></a>

Resources