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)
Related
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; ?>
I'd like to have 2 foreach loops for 2 arrays like that, I know it's wrong:
$properties has 6 items and $set_properties has 11 items
<?php if(!empty($properties)) : foreach ($properties as $prop) : ?>
<?php if(!empty($set_properties)) : foreach ($set_properties as $set_prop) : ?>
<?php endforeach; endif; ?>
<?php endforeach; endif; ?>
What would be the right way, because right now I'm getting too many loops if I want to echo something out ($prop->ID).
My goal is to make dropdown selectors($properties) and have $set_properties as options.
Try the following snippet, which I commented to hopefully help you understand.
//check if properties is empty, if not, loop through them.
if(!empty($properties)):
foreach ($properties as $prop):
//check if $set_properties is empty, if not create select element
if(!empty($set_properties)):
//loop through properties, generate select element for current index
//obviously you can do proper select element naming and everything I just don't know your markup
//notice I do this after checking if the options fields are empty, so that way it only appears if there are options associated with it.
echo $prop->ID.": <select>";
//loop through set_properties, generate options
foreach ($set_properties as $set_prop):
echo "<option value='{$set_prop->ID}'>{$set_prop->Name}</option>";
endforeach;
//end select element, so next iteration will make a new one.
//again, inside the check for $set_properties because the select element will only exist if options exist
echo "</select>";
endif;
endforeach;
endif;
In your original code you switch in and out of PHP very often, and that is really not necessary, it makes the code look messy and it's harder to format. Just write it all in PHP as there isn't much HTML that goes into it. You should only switch in/out of PHP if you are only trying to insert a variable into an element, or if there is a lot of HTML.
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/
I've managed to render static blocks through a custom attribute on a per product basis, which is great, however it only seems to work with drop downs, I would like to use a multi select so i could allow the administrator to select multiple static blocks in one are, rather than have multiple drop down menus.
here's the code for the drop down
<?php
$cmsstatic=$_product->getResource()->getAttribute('attributename')->getFrontend()->getValue ($_product);
echo $this->getLayout()->createBlock('cms/block')->setBlockID($cmsstatic)->tohtml();
?>
I managed to get the value of the attribute options out for a multi select:
<?php if($_product->getResource()->getAttribute('product_featured_attribute_3')->getFrontend()->getValue($_product)): ?>
<ul><li><?php
$_comma = ",";
$_list = "</li><li>";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('attributename')->getFrontend()->getValue($_product)) ?>
</li></ul>
<?php endif; ?>
but i'm having problems getting it to output the value as a static block. Any ideas?
as i understood you want to echo all the block content which id's are defined in product attribute.
For that you can try this:
<?php
$cmsstatic=$_product->getResource()->getAttribute('attributename')->getFrontend()->getValue($_product);
$blockids = explode(",", $cmsstatic);
foreach($blockids as $kry=>$value)
{
echo $this->getLayout()->createBlock('cms/block')->setBlockID($value)->tohtml();
}
?>
I have an array $category['transactions']. It stores data as follow:
ID Name Phone
1 Test Test2
2 Test3 Test4
It's because I use the same array for different purpose, at one of the scenario is to show only the first record in this array. I don't what to change the coding in php nor creating a different parameter. What can I improve based on the following coding in html to get the first record only in this array?
<?php foreach($category['transactions'] as $transaction) { ?>
<div><?php echo $transaction['id']; ?></div>
<div><?php echo $transaction['name']; ?></div>
<?php } ?>
replace your code with.
<?php $firstRow=reset($category['transactions']);
echo '<div>',$firstRow['id'],'</div>';
echo '<div>',$firstRow['name'],'</div>';
?>
You don't need to iterate through the array to get the first element.
You don't even need the foreach to get the first element. Just use array_values():
$first = array_values($category['transactions')[0]
try this..
<?php
foreach($category['transactions'] as $transaction)
{
echo $transaction['id'];
break;
}
?>
and no need to use multiple php tags...