I'm trying to show the ACF repeater image in the dynamic slider in oxygen via the PHP function from the specified page id.
ACF field: slajder
Subfield with img repeater: obraz_slajdera
Page id: 7219
I always get background-img unknown.
My code:
function get_slider() {
$image = get_field( 'img', 7219 )['sizes']['large'];
return $image;
}
Please help.
You may want to do some reading through the ACF developer documentation, your code is fragmented, and the get_field() function isn't being used properly. At any rate, I'll do my best to explain what you should be directing your solution towards. You'll first need to loop through your repeater fields, check to see if there's any "rows" in the repeater field, fetch your value, and then do whatever you need to do with your respective image.
So, technically your function should look something like this:
function fetchSliderImages() {
//Empty array for holding your slider images, assuming there's more than one
$sliderImages = array();
//Check to see if the slider even has rows
if( have_rows('slajder') ):
//Loop through all the rows of your slider
while( have_rows('slajder') ) : the_row();
//Assuming this is coming back as a URL (can be adjusted within ACF)
$imageRepeaterValue = get_sub_field('obraz_slajdera');
//Check to see that we actually got something back that isn't blank
if($imageRepeaterValue != '') {
array_push($sliderImages, $imageRepeaterValue);
}
endwhile;
endif;
return $sliderImages;
}
You can then use this function to return an array of URL's which are slider images.
Used this for reference: https://www.advancedcustomfields.com/resources/repeater/
Related
In the theme there are two files:
single.php
f-template.php → For default post type. It has different designs.
Home.php will fetch first 10 posts, and they can be among any of the above templates.
But, what is needed is when the content on home.php is coming from f-template.php
then these two things should be implemented on home.php
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
and
<script>
(function($) {
// do all your $ based jquery in here.
$(function() {
$('p.class2').prepend('<img src="http:/sample.com/img/folder.svg" width="50" height="25" alt="">');
});
})(jQuery);
</script>
I tried this:
if( is_page_template( 'f-template.php' ) ) {
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
}
the_content();
}
But this didn't work. actually it is flawed because the template that we are dealing is home.php.
So do we have any solution to achive what we wanted to achieve?
Correct me if I'm wrong but it sounds like you want to display a loop with posts where some posts have a different design depending on the Page Template you have selected for it.
You can check which template is being used with the get_page_template_slug() function. Use it inside the loop along with the ID of the post.
// checks if there are any posts that match the query
if (have_posts()) :
// If there are posts matching the query then start the loop
while ( have_posts() ) : the_post();
// Assign postId from within the loop
$postId = the_ID();
if( get_page_template_slug($postId) === 'f-template' ) {
// Display what you want to see when f-template is selected.
} else {
// Display what you want to see by default if no condition is met.
}
// Stop the loop when all posts are displayed
endwhile;
// If no posts were found
else :
echo '<p>Sorry no posts matched your criteria.</p>';
endif;
The is_page_template() function won't work because it will check what the page template for the current page in the Main Query is.
It all depends on your use case but personally I would have added an extra field using Advanced Custom Fields for this effect.
Good luck!
I'm fairly new in using ACF so I used the code that was displayed in their site to display what is inside of my repeater field.
However, when I try to show the repeater's content it is empty??
This is my code, this is still in trial mode just to see if it's working-which it isn't. My repeater field already has 2 rows but it's not showing up any of those and just displays the else:
// check if the repeater field has rows of data
if( have_rows('map_infogrp') ):
// loop through the rows of data
while ( have_rows('map_infogrp') ) : the_row();
// display a sub field value
the_sub_field('google_map');
the_sub_field('branch_name');
//$street = get_sub_field('street');
//$district = get_sub_field('district');
//$phonenum = get_sub_field('phone_number');
//$email = get_sub_field('email');
endwhile;
else:
echo 'why is this empty???';
endif;
You need to specify the page id that you have set the ACF Repeater, otherwise it will get from the current page ID
have_rows($field_name, $post_id); //syntax
So, update your loop inserting the page ID you've entered the repeater data:
if( have_rows('map_infogrp', 'page_id') ):
// loop through the rows of data
while ( have_rows('map_infogrp', 'page_id') ) : the_row();
...
If you have the fields filled in on the specific page it should be showing up. If not, double check the field name(not label) that you used. If you have more than one row make sure you're printing it out as an array too
I have created a repeatable field with CMB2, and created a normal field. This is the function of https://pastebin.com/XUQgkvbi
If you use foreach for repeatable using a post or page then you can show data as: https://pastebin.com/C35vWGDs
And Call normal field without repeatable, then
<?php $ entries = get_post_meta (get_the_ID (), 'yourprefix_group_demo', true); ?>
<?php echo $ entries; ?>
also work.
But the problem is, I do not want to use the above function on any page or post. I want to use it in the Options Page. The above Function option has been added to the option page, but I can not do the data show of those files in any way.
I've tried get_post_meta () and get_option () with two functions, but in no way can I show data from the option page. How can I get the data from the above fields (option page) to the show in the frontend? Please help with a little bit.
I got solution, The options are stored in a single option field. You would loop through the news-section groups with something like this:
$settings = get_option( 'repeatable-news-options.php', array() );
if ( ! empty( $settings['news-section'] ) ) {
foreach ( $settings['news-section'] as $section ) {
echo $section['title'] . '<br/>';
}
}
that link https://wordpress.org/support/topic/how-to-display-data-from-cmb2-option-page/
problem solved.
I'm using the advanced custom field plugin on a Wordpress site. I'm using the Repeater Field Type to be able to attach more than one file.
My Repeater Field Type is called: electronics
with the sub fields name: electronics_files
Here is what I have so far:
<?php
// check if the repeater field has rows of data
if( have_rows('electronics ') ):
// loop through the rows of data
while ( have_rows('electronics ') ) : the_row();
// display a sub field value
the_sub_field('electronics_files');
endwhile;
else :
// no rows found
endif;
?>
I have the return value for electronics_files as a File URL so I can wrap it inside of an a tag to download.
Right now it returns all the file urls as one long string. How would I do it so it first checks to see if I have datasheet then grabs the first one wraps it around a a tag then loops until there are no more electronics_files.
I have something like this maybe?
if( $file ) {
$url = wp_get_attachment_url( $file );
?><a href="<?php echo $url; ?>" >Download File</a><?php
}
Basically I just want it to display the file I attached as links that can be downloaded.
It sounds like to just need to change this...
the_sub_field('electronics_files');
...to this:
echo 'Download File';
In other words, just format the sub field as a link.
I'm trying to use checkbox inside a repeater field to show different images. If user checks "chicken" and "roast" it must display two images to front-end. If nothing is checked, nothing is displayed.
I have tried the Advanced Custom Fields documentation and all I can get is NULL or exactly these names "chicken" and "roast" to front-end. This is my piece of code that is inside repeater loop.
<?php if (get_field('selection') == 'chicken') { ?>
//some piece of code
<?php } else if (get_field('selection') == 'roast') { ?>
//some more code
<?php } ?>
A checkbox creates a list of check-able items. In other words, you need to check that your string is in an array(). For example:
if( in_array( 'chicken', get_field('field_name') ) ) {
//...
}
Read more about checkboxes in the ACF codex: http://www.advancedcustomfields.com/resources/checkbox/