ACF plugin, displaying as list instead of comma separated - php

A plugin for wordpress called Advanced Custom Fields uses a
<?php the_field('field_name'); ?
function to display information that I have determined inside WP admin in posts.
However, it outputs it as a comma separated horizontal list (e.g. cow, milk, farm).
I want to output it like this, in an unordered list:
cow
milk
farm
How would I go by doing this?

add css class to your php call, when you creating list, make list and ul, then as cotent call fields..
For more please share your htm/php file, and css also...

In your php file, save the field in an array, and use foreach to create an li foreach string.
What kind of 'Field type' do u use ?

<?php
$arr = get_the_field('field_name');
$str = explode (",", $arr);
echo '<ul>';
foreach($str as $s){
echo '<li>'.$s.'</li>';
}
echo '</ul>';
?>
OR use this code
https://www.advancedcustomfields.com/resources/get_fields/

Related

How to get a foreach statement output in an array with if statement to implode with commas in Wordpress + ACF?

I am making a artist portfolio site in wordpress. I am working on a single page for a custom post type Artwork, which contains numerous metadata fields created using ACF. I have run into trouble displaying the contents of an ACF repeater field with the names of collaborators, if there are any, who contributed to the artwork linked to their personal sites if they have one. This repeater is cloned as one element into the Artwork Field group.
I have been able to display the collaborators' names with and without links using the code below. So it will display "Co-creators: Name1 (with href)Name2 (no href)". But the commas don't show up between the collaborators. I think it is because I am not creating the arrays properly.
Any help would be greatly appreciated.
<?php
while (have_posts()) : the_post();
// lots more code here
$artwork_collaborators = get_field('artwork_collaborators'); // get the value of the acf clone field
if ($artwork_collaborators) : // check if has data ?>
<dd id="single-artwork-collaborators"><?php
_e('Co-creators: ', 'aopa'); // multilanguage label for translation
foreach ($artwork_collaborators as $artwork_collaborator) : // foreach loop to get all collaborators
if ($artwork_collaborator['artwork_collaborator_url']) : // check if there is a url for the collaborator
$array_collaborators = array('' . $artwork_collaborator['artwork_collaborator_name'] . ''); // create array with URL
else :
$array_collaborators = array($artwork_collaborator['artwork_collaborator_name']); // create array without url
endif;
$collaborator_string = implode(", ", $array_collaborators); // implode the array and add commas
echo $collaborator_string; // echo the collaborators with link or no link separated by commas if there is more than one
endforeach; ?>
</dd>
<?php endif;
// lots more code here
endwhile?>
I ended up getting some help on this problem from a programmer friend. Now the output is "Co-creators: Name1 (with href), Name2 (no href)" with the commas in the right place and the link in place when needed and not there when not.
One of the main problems was that the implode and the echo were inside the loop. This was causing them to repeat too many times.
We also created an empty array off the top, then added all the strings into that. See the comments in the code below:
<dd id="single-artwork-collaborators"><?php
_e('Co-creators: ', 'aopa'); // multilanguage label for translation
$collaborators_all = []; // Create an empty Array
foreach ($artwork_collaborators as $artwork_collaborator) : // foreach loop to get all collaborators
if ($artwork_collaborator['artwork_collaborator_url']) : // check if there is a url for any of the collaborators
$collaborator = '' . $artwork_collaborator['artwork_collaborator_name'] . ''; // add rows in loop to the variable
else : // check if there are any collaborators without URLS
$collaborator = $artwork_collaborator['artwork_collaborator_name']; // add these rows in loop to the variable
endif;
$collaborators_all[] = $collaborator; // combine in the array created above all the rows of collaborator names, with and without and links, if there are any of each
endforeach;
$collaborator_string = implode(", ", $collaborators_all); // implode the array and add commas
echo $collaborator_string; // echo the collaborators separated by commas if there is more than one ?>
</dd><?php
endif; ?>

Dynamically populating wordrpess shortcode from an array

I am trying to customize output of wordpress smart grid plugin. My aim is to add list of images into a section and output them through the plugin. The image list has to be wrapped by the plugin shortcode. Which I have done, only problem is looping. Here is my code
<div class="p_details_right">
<?php
//print_r($partner_pictures);
$pic_bucket = []; //empty array to hold list of image id to be used within the wordpress default gallery shortcode
?>
<?php
foreach ($partner_pictures as $key => $item) {
array_push($pic_bucket, $key); // populating the array which holds image id
?>
<?php
}
//print_r($pic_bucket);
?>
<?php echo do_shortcode("
[smart-grid]
[gallery ids='$pic_bucket[0],$pic_bucket[1],$pic_bucket[2]'] // <----- PROBLEM . Currently doing it statically but I need to be able to add populate the id element of the shortcode dynamically based on the $pic_bucket array.
[/smart-grid]
"); ?>
</div>
Currently outputting the gallery by statically adding array element but I need to be able to add populate the id element of the shortcode dynamically based on the $pic_bucket array. Tried to do looping but does not work. I am missing something very basic.
Sollution
convert Array to string and assign ids variable with that.
<div class="p_details_right">
<?php
//print_r($partner_pictures);
$pic_bucket = [];
?>
<?php
foreach ($partner_pictures as $key => $item) {
array_push($pic_bucket, $key);
?>
<?php
}
//print_r($pic_bucket);
$str = implode(',',$pic_bucket);
?>
<?php echo do_shortcode("
[smart-grid]
[gallery ids='$str']
[/smart-grid]
"); ?>
</div>
You can join an array of ID's together with implode(',',$array)

Split Multiple Options in Single Attribute in Magento

I have multiple options in an attributes in Magento and when I call the attribute, all of the options show in a string and I would like to show them on separate lines (p tags or li's).
<?php echo $this->getChildHtml('spamodel') ?>
The above is my code, I think i need to use explode but I'm a newbie at php.
Thanks for any help.
<?php $spamodel = $this->getChildHtml('spamodel');
$spamodel_explode = explode(",",$spamodel);
echo $spamodel_explode[0];
?>
Here you can go with this code. you can get single the names with $spamodel_explode[0] and if you want to get all the names than you can use forloop
Here You can able to get the answer
<?php
$variable = $this->getChildHtml('spamodel')
$variable_exp = explode(",",$variable)
foreach($variable_exp as $var){
echo $var;
}
?>
In above coding you can explode based on your requirement.Just replace the ',' tag and get the results based on your requirement.
Find the below link for more details.
http://www.brandammo.co.uk/output-magento-custom-attributes-to-front-end-from-multi-select-dropdowns/

Wordpress meta tag challenge

I'm currently trying to create a site for TV shows and due to certain wordpress limitations this is becoming a challenge.
However I bypass that with the use of implementing custom meta fields in the functions.php file, now my problem is that I need it to actively create new fields when I submit information in the current field. For example custom metabox names are
(Episode Name="This is It") (Episode Number="1") (Season Number="5")
Instead of having to create all the boxes from the beginning I would like the use Javascript (jQuery) or any solution to automatically create a new set of these 3 boxes
(Episode Name="") (Episode Number="") (Season Number="")
so I can just enter the new information as they come. Thank you in advance for your help!
Note: I have invested too much time into wordpress to just switch to another cms, so that is not an option at this point in time.
from what I understand of your question, you are looking for a simple solution to automate the input process. I have a general idea of what it is you nee to achieve as I have had to do something similar on a brochure type of website.
I have tried answering your question using Jquery, but I find it to increase the amount of text input required when creating your post, there is unfortunately no completely automated method of doing it, but hopefully below would provide you with a solution.
first I found the following plugin: Types - Complete Solution for Custom Fields and Types here: http://wordpress.org/extend/plugins/types/
This allows you to create custom meta feilds when creating a new post/page. The custom feilds are brought added with a perfix of "wpcf-" and then the name of the field, e.g. "Episode Name" becomes "wpcf-episode-name" in the database.
The following is an edit of wordpress's get_meta function:
function get_specifications(){
if ( $keys = get_post_custom_keys() ) {
echo '<div class="entry_specifications">';
foreach ( (array) $keys as $key ) {
$keyt = trim($key);
if ( '_' == $keyt[0] )
continue;
$values = array_map('trim', get_post_custom_values($key));
$value = implode($values,', ');
//remove "wpcf-"
$key = str_replace('wpcf-','',$key);
//convert "-" to a space " "
$key = str_replace('-',' ',$key);
//check if it has a value, continue if it does, skip if it doesn't:
if ($value <> ''){
echo apply_filters('the_meta_key', "
<div class='meta_wrapper'>
<div class='meta_title'>$key</div>
<div class='meta_value'>$value</div>
</div>\n", $key, $value);
};
}
}
// echo '</div>'; comment out and remove the line below if you are not using floats in your css
echo '</div><div class="clear"></div>';
}
In my page.php (or your template-page.php) I have added the following code when/where I want the meta to be produced:
<?php
//check to see if there is meta data, as you only want the meta data on TV Program pages
$met_check = get_post_meta($post->ID, 'wpcf-features', true);
if ($met_check <> ''){
//if it has a value, spit out "About EpisodeName" ?>
<h2 class="post-title clear">About <?php echo $title ?></h2>
<?php //perform the function from functions.php and return results:
echo get_specifications();
}
?>
I've styled the result with the following CSS:
.meta_entry {
clear:both;
}
.meta_title {
float:left;
text-transform:capitalize;
width:200px;
}
.meta_value {
float:left;
width:480px;
}
Hope this helps mate!
There is a wonderful plugin for wordpress called Pods which might be a viable solution.
Try http://wordpress.org/extend/plugins/custom-field-template/

Iterating through a foreach loop - only show 1 item rather than all

I've setup a Google map that has several plotted markers, each one when clicked on has a little popup with info within.
I'm using the following code to show the company name per plotted marker, but it's going through the foreach loop and showing ALL the company names within one popup.
How can loop through and show one company name per plotted marker? I need it to show the first company in the list on the first marker, the second company in the list on the second marker and so on.
// Setting the content of the InfoWindow
infowindow.setContent('
<?php $pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order')); foreach($pages as $post) {
setup_postdata($post);
$fields = get_fields(); ?>
<p><?php echo $fields->company_name; ?></p>
<?php } wp_reset_query(); ?>
');
Look at your code. what a mess! You would never have missed these obvious mistakes, if you would care to structure your code well! Don't write all your code on one line! Use best practice for code style!
Don't put all that PHP code inside the javascript function.
Instead, use a variable $contentMarkup, store everything in there and in the end echo this variable into the javascript code.
<?php
$contentMarkup = '';
//do all your stuff like
$contentMarkup .= '<p>';
$contentMarkup .= $fields->companyName;
$contentMarkup .= '</p>';
?>
infowindow.setContent('<?php echo $contentMarkup; ?>');
Regarding your actual question: If you want to generate more than one tooltip/window/younameit, they have to have unique identifiers so you can create one for each company. But to say more I'd need to know a bit more about what exactly you're trying to do. What information is available and from what source.
show what you want based on if statement inside foreach loop

Categories