I have a wordpress with acfpro. I use it the options page for to create option page header and footer, next I declare custom field map on option page footer and paste code in footer.php.
when I do it this, i have some pages where acf map not working and other pages is good!
wordpress 5.2, acfpro 5.7.13
<div class="left">
<div class="zto-containMap">
<?php
$location = get_field('map');
$long = $location['lng'];
$lat = $location['lat'];
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $lat; ?>" data-lng="<?php echo $long; ?>"></div>
</div>
<?php else: ?>
<div class="acf-map">
not working
</div>
<?php endif; ?>
</div>
</div>
I don't have a error message, and acf map is broken on 3 page on my site and the other pages are working nice width acf map
If you want to get the field value for an option, you have to declare the option scope in your second paramter of get_field($field, $scope). If no $scope is passed in the get_field() function it will automatically use the ID value of get_post().
This is what you're after:
$location = get_field('map', 'option');
Here's the docs on getting the value of an Option field in ACF.
While this is a little out of the scope of your question, these docs explain a little further about how the $scope variable can be used in the second parameter.
Related
I have the normal WP loop for showing blog posts on home.php. Im trying to get a random div to populate after each blog post. So it will be:
BLOG POST -> RANDOM DIV -> BLOG POST -> RANDOM DIV
and so on...I've tried to create "testimonials" post type and randomize them with WP_Query, but it still will only pull one PER PAGE. I need multiple to display, but differently below each post. I am also using ACF, so I created testimonial custom field blocks, but I still cant them to randomize display multiple items.
PHP (within loop):
<a class="row blogRoll"href="<?php the_permalink(); ?>">
<div class="blogImg col-sm-3">
<?php the_post_thumbnail(); ?>
</div>
<div class="col-md-9 blogBlack">
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
</a>
<div class="testimonial">
<?php the_field('testimonial_content1', 'option');
the_field('testimonial_author1','option'); ?>
</div>
<div class="testimonial">
<?php the_field('testimonial_content2', 'option');
the_field('testimonial_author2', 'option'); ?>
</div>
<div class="testimonial">
<?php the_field('testimonial_content3', 'option');
the_field('testimonial_author3', 'option'); ?>
</div>
</div>
JS:
var elems = $(".testimonial");
if (elems.length) {
var keep = Math.floor(Math.random() * elems.length);
for (var i = 0; i < elems.length; ++i) {
if (i !== keep) {
$(elems[i]).hide();
}
}
}
Obviously, the loop is causing me the headache, because it will pull most recent posts and I just need to pull and not DUPLICATE the testimonial divs below a blog post. Any ideas will be appreciated.
Instead of shuffling and outputing the result straight away, you could shuffled an array (pool of testimonials) and then apply the first value to a first .testimonial class for example, and a second value from the shuffled array to the second .testimonial class, ... etc.
That article might be of some help, Randomly iterate through array without pulling duplicate values. You could replace numbers by testimonials... Regards.
I would like that my php detects seperatly images (media) and text, from a same wordpress post, in regard to display them in different columns. I am using Bootstrap, and I would like different display of each (text and image) according to the screen size.
Here is a code I have that displays all of the content of my post (image and text) at once, it works perfectly :
<div class="container">
<?php if (have_posts()): ?>
<div class="container">
<?php while(have_posts()): the_post(); ?>
<div class="row">
<div class="col-xs-12 col-md-12 col-lg-12">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
I would like the same way of loading content but instead of having just
<?php the_content(); ?>
I would like something like this to place text and image (of same post) in different div class:
<?php the_image(); ?>
AND
<?php the_text(); ?>
I know this is wrong code but I am trying to be clear in my explanation. I need that my client does not touch any of code, also shortcode in Wordpress post. Any input or advice would be very appreciated!
Many thanks!
Thank you for your answer! Lars Beck, you solved my problem!
Here is the code I was looking for:
<?php
//get content from back office , filter imgs except featured img to place them later in <div id='img_content_right'>
$content2 = get_the_content();
$removed_imgs = array();
$content2 = preg_replace_callback('#(?!<img.+?id="featured_img".*?\/>)(<img.+? />)#',function($r) {
global $removed_imgs;
$removed_imgs[] = $r[1];
return '';
},$content2);
//add filter to the_content to strip images except img_menu et featured_img
add_filter('the_content', 'strip_images',2);
function strip_images($content){
$content=preg_replace('/(?!<img.+?id="img_menu".*?\/>)(?!<img.+?id="featured_img".*?\/>)<img.+?\/>/','',$content);
return $content;
}
the_content();
?>
<div id='img_content_right'>
<?php
//append the images stripped from content
foreach($removed_imgs as $img){
echo $img;
}
?>
</div>
There is no way, to separate out images from the content section if they have just been included as part of the content.
Wordpress has featured image functionality built in for you. so you can use the_post_thumbnail() to echo out the image. Maybe this question can help you Wordpress featured image code
If this isn't enough for you and you need more flexibility you might want to look at advanced custom fields plugin - https://www.advancedcustomfields.com/ it makes it easy for you to add your own fields of any type you want.
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.
I'm quite new to magento and i'm struggling trying to add a custom attribute after the short description in products page
<?php if ($_product->getShortDescription()):?>
<div class="short-description">
<div class="std"<?php if($richSnippets['status'] == 1): ?> itemprop="description"<?php endif; ?>><h2><?php echo $this->__('Details:') ?></h2><?php echo $_helper>productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
</div>
<?php endif;?>
<?php $_item = $this->getProduct()->getId();
$_resource = $this->getProduct()->getResource();
$CustomAttribute = $_resource->getAttributeRawValue($_item, 'ratebeer', Mage::app()->getStore());
echo $CustomAttribute; ?>
Anyway the attribute does not appear after the short description...the same code works in another section of the same page so i don't really know what's going on
You should be able to just
echo $_product->getRatebeer();
But you may have to add the attribute to your flat tables first. You can do this by setting Include in product listing to Yes.
This is bad practice, but you could try this just to see if the attribute is set up correctly
echo $_product->load($_product->getId())->getRatebeer();
I am using Advanced Custom Fields. I want to link the values (eg. thriller, suspense, etc.) of Genre custom field so that when the user clicks on one of the values they will get a filtered list of posts. For eg. when they click Thriller they will get all the posts in the Thriller category.
My code so far for the ACF is as follows:
<!-- Entry Content Start -->
<article <?php post_class('entry clearfix fitvids'); ?>>
<div class="inner-post">
<div class="cover">
<div class="post-image">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
<div class="custom-fields">
<?php the_meta(); ?>
<div class="ad">
<img src="http://lorempixel.com/160/600" alt="">
</div>
</div>
</div>
<div class="the-content">
<?php the_content(); // This is your main post content output ?>
</div>
</div><!-- /inner-post -->
</article><!-- /entry -->
Is there a solution to this?
You should create a page with custom page template. In this template you should pull posts based on the custom field passed via $_GET parameter. For example: $movies = get_posts(array('meta_key'=>'your_custom_key', 'meta_value'=>$_GET['genre'])); Then you should generate appropriate links to access this page, which should link to our custom page including genre in our $_GET variable. For example the page's slug is movies. Then our links will have the following structure: /movies/?genre=custom_field_value.