ACF - image as a subfield - wordpress

so after a lot of researching and trials and errors, I come to you. This seems to be very basic and still no luck. The issue is that my image does not appear and in the console it gives me an <img src (unknown) />.
So, in my ACF, I have a field called first_row that contains sub fields, one of the is called left_side_bg. It's attributes are all standard, and is set to return an Image Array. As a side note, I have another subfield called left_side_text.
On my php file, I'm trying to retrieve it like this:
<?php
$info = get_field('first_row');
$left_side_bg = get_sub_field('left_side_bg');
?>
<div class="single-banner">
<img src="<?php echo $left_side_bg['url']?>" alt="">
<div class="inner-text">
<h4><?php echo $info['left_side_text']; ?></h4>
</div>
</div>
The left_side_text shows fine, but no luck with the image... is there anywhere I can double check or debug this better, or am I fetching the data incorrectly?
As a side note, I have tried different ways to retrieve the image by changing to Image URL, Image ID (in the ACF custom editor) and so on, but no luck, so I returned to the original format I tried to get the image the first time with all the ACF fields as default (getting the Image as an Array).

get_sub_field() is meant for Repeaters and Flexible Content (rows). I assume left_side_bg and left_side_text are in the same group. Therefore, try it like so:
<?php
$info = get_field('first_row');
?>
<div class="single-banner">
<img src="<?php echo $info['left_side_bg']['url']?>" alt="">
<div class="inner-text">
It also makes a difference to what your Return Value is configured. I hope it is set to "Image Array". Otherwise, you might have to omit the ['url'] if it's "Image URL" instead.
If that isn't working, it might be a good idea to post a screenshot of what your ACF field configuration (field group) looks like.

Related

URL Field not appearing correctly in anchor

I have run into a very interesting problem. I am working on a site for a client of mine and have run into this issue using Advanced Custom Fields no wordpress. I have added a new custom field to the site for their rewards program spot, and I am having trouble getting the url field to work corretly. I have the code added to the template php file that it needs to be in and it looks like this at the moment.
<div class="row">
<?php $ficvalue = get_field('free-icecream-url');
if ($ficvalue) {
$ficurl = esc_url($ficvalue);
}?>
<h3 class="heading" style="text-align: center;"><?php the_field('free-icecream-header'); ?></h3>
<a href"<?php echo $ficurl; ?>" class="btn btn-primary" style="max-width: 15em;"><?php the_field('free-icecream-btn'); ?></a>
</div>
When I put the url into the custom url field on the backend in the page editor, it doesn't appear in the anchor correctly. if I add this line to the page, it shows the url correctly on the page itself:
<?php echo get_field('free-icecream-url'); ?>
This will show the url as it is inserted into the custom field, however adding it into the anchor's href like this provides a different result.
<?php the_field('free-icecream-btn'); ?>
<!-- results as the following in chromes inspect -->
<!-- https: www.rococoicecream.com rococo-rewards
Instead of https://www.rococoicecream.com/rococorewards -->
I have tried to use esc_url() and esc_html() to no avail. and when using the_field() it yields the same results. What am I doing wrong?
The answer to my question here was answered shortly after posting it. I updated the original question but I will also place it here. I found the problem I was having was that I missed a = after the href in the anchor for my button. After finding that out, it works like a charm.

Access contents of a node of custom content type

I have a jquery slide show in the front page of this site I'm working on. I want to populate the slideshow with the contents of a node which is of a content type that i defined. The content type has multiple (image field and description). How do I access these contents from the front page.
Thanks in advance.
-- Aayush Shrestha
I have figured out a way to do the above mentioned task. Not sure if it is the most efficient way to do it but this is what I did.
Basically I loaded the content node using
$node = node_load($node_id);
Then I print_r()'ed the $node to see its array structure.
After that it was just a matter of accessing the value of the required field and echoing it at the required place. It looked something like this.
$slideArray = $node->field_slide['und'];
$count = count($slideArray);
<img src="<?php echo base_path() . 'sites/default/files/' . $slideArray[$i]['filename'] ?>" width="930">

Add Link Field in Template in Drupal 6 with CCK

Good Evening,
I'm using Drupal 6, CCK Module, and the Link Field Type. All are installed and activated.
I created a custom content type called Slider. A user can upload an image, a title, and a teaser. I created a custom field for that slider content type as well as one called Link with the field name: "field_link_test".
I created an entry, filled in all of the data including a URL for that link field type and clicked "Save". In views-view-table--slider.tpl.php, I added:
Learn More
but on the front end, everything shows except for that link. I also tried emptying the Drupal cache.
Any ideas?
Update template code below, which all works fine, except for the new link value outputs nothing.
<div id="slider">
<div class="slider-holder">
<?php foreach($rows as $row): ?>
<div class="slide">
<?php print $row['field_image_fid'] ?>
<div class="info-slide">
<h2><?php print $row['title'] ?></h2>
<p><?php print strip_tags($row['teaser']) ?></p>
Learn More
</div><!--INFO-SLIDE-->
</div><!--SLIDE-->
<?php endforeach ?>
</div><!--SLIDER-HOLDER-->
<div id="control">
</div>
</div><!--SLIDER-->
The easy possibilities (which you've probably checked, but just to get them out of the way):
you need to allow the field to be viewable by anonymous/authenticated users in User Management - Permissions
Otherwise, it's hard to tell without some code to analyse. Could you post your entire views-view-table--slide.tpl.php and if possible, your exported view or a link to the exported view?
EDIT
Now that I've had a chance to look at your view, I've made a couple of changes that should help.
The reason your link URL isn't showing is that you're including the "Node: Link" field in your View instead of the "Content: Link (field_link_test)" field. The first one will just link back to the original node rather than your custom link. Also, I don't think you can call the $node variable from views-view-table (at least, I don't get anything when I print it. Instead, you can use the $row variable.
I have a version of your template that prints out the URL in the field "link_test" with the label "Learn More." I put the "Learn More" text in the View itself as that'll be easier to edit and works better with the Link CCK type (which by default will want to add a title you add in the node edit screen).
The view export is here: http://pastebin.me/0ed2942f6953cb00cab1bd5386058a13. You can import this back into your site, but you may want to clone your original View first to make a backup, so that if this isn't what you want, you can use your old version.
The updated tpl is:
<div id="slider">
<div class="slider-holder">
<?php foreach($rows as $row): ?>
<div class="slide">
<?php print $row['field_image_fid'] ?>
<div class="info-slide">
<h2><?php print $row['title'] ?></h2>
<p><?php print strip_tags($row['teaser']) ?></p>
<?php print $row['field_link_test_url'] ?>
<?php //print_r($row); ?>
</div><!--INFO-SLIDE-->
</div><!--SLIDE-->
<?php endforeach ?>
</div><!--SLIDER-HOLDER-->
<div id="control">
</div>
</div><!--SLIDER-->
Let me know if you have any issues/questions.
Are you sure the template is getting picked up (add <p>heavymark</p> above the href... does it show up?)?
If above shows up, add a var_dump($node) above the anchor tag and post the output so we can get a better idea of what's there (you probably want to enable XDebug so you get better formatted output, if its not on already).
Make sure you add the link field to the view in the fields section. This should allow it to be themeable from within your template file. If you are still not seeing it, try using
print_r($rows,1);
or some variable of print_r to view all the rows that are available to be themed.

Trying to understand custom fields in WordPress

I'm trying to make my single.php page show more details about each project/item in a portfolio, with multiple larger image files that will display with captions to the right of the project description.
I'm thinking the best way to approach this would be to use the 'featured image' for the thumbnail display on the homepage which seems to be working now, but I've been trying to figure out Custom Fields to use for my other images (image1, image2, image3). I can't figure it out.
I go to "Enter new" under Custom Fields from the admin screen, enter image1 for the name. What goes in the Value field? How do I get it to work? I know I need to add some code to my single.php page... but what? And when/where do I upload the images themselves?
Any help would be greatly appreciated! I'm a little dense in the PHP department...
Have look at your single.php file. You will find the following lines, one near the top, the other lower down.
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
...
...
<?php endwhile; // end of the loop. ?>
Anything between these two lines will be applied over and over, once per blog post that happens to be displayed by that template file. So you should put your magic between those lines.
What you put there will be a mix of HTML and PHP. For instance, you might use this line:
<img src="<?php echo get_post_meta($post->ID, 'image1', true) ?>"/>
which combines
<img src=""/>
with
<?php echo get_post_meta($post->ID, 'image1', true) ?>
The PHP line looks up whatever value you have entered for the custom field named 'image1'. If you look at the HTML I used, you will see that this value is being placed inside the 'src' attribute. In other words, the value associated with 'image1' should be the url of that image.
You can expand upon this basic idea, but that's where your own creativity will have to come in.
[Is this not a repeated question?]

Associating an image with a post in WordPress

What's the best way to associate an image with a post in WordPress?
What I mean by that is when each post has a 'header' image which is not in the body of the post. You'd most likely use it to show a small image next to each post when they're listed somewhere.
I know you can use custom fields with posts, but are there any better ways of doing it?
Thanks in advance.
If you go the custom field route - in your template code you can save the value of the custom field to a variable and then echo it out to your image.
<?php $post_image = get_post_meta($post->ID, post_image, true); ?>
<?php if( $post_image != "" && isset($post_image) ) : ?>
<p><img src="<?php echo $post_image; ?>" alt="<?php echo $post_image; ?>" /></p>
<?php endif; ?>
<?php the_content('read more...'); ?>
The function the_content() may or may not be what you will end up using - for more control, you can try setting a new WP_Query() and then revamping the loop.
Not really any better way than custom fields, although you could always write out a unique class name containing the post ID on the container div and add the images via CSS each time you add a post. You'd end with a potentially huge CSS file though so I'd go for custom fields myself.
If you want a different image per post then use Customs Fields as suggested before - the only problem with that will be that Custom Fields are text fields and so you will have to upload your image and then paste the filename into your Custom Field. You could use the normal Add Image facility in the main post entry form but that will break up your workflow a bit as you will have to: upload the image in main post form, copy image url from HTML of form, remove image from form, paste URL into a custom field.
A bit of a pain!
If you want to use an image per category. Say, for example, you have a thumbnail for news items and a thumbnail for tutorials and so on. You could edit your WP theme template, check the category inside The Loop and show the relevant image from there.
The first option is a bit of a pain and the second requires some PHP coding.
A good way of associating an image with a post is to call the first image attached to that post in your template file.
I've elaborated a bit in this thread:
How would you recommend adding an image as a custom field in WordPress?

Resources