i have a write panel in magic fields with a group that can be duplicated, within this is an image field that can be duplicated.
So far i have the code for the duplicate group but i'm a little stuck on how to display the duplicate field within this.
I'm using Magic Fields 1.6.2.1
My code for the duplicate group is
<?php
$gallerys = getGroupOrder('gallery_section_title');
foreach($gallerys as $gallery) {
?>
<?php echo get('gallery_section_title',$gallery);?>
<?php echo get('gallery_section_text',$gallery);?>
<?php echo get('gallery_section_image',$gallery);?>
<?php } ?>
This part is the part that needs to be duplicated with in the duplicate group
<?php echo get('gallery_section_image',$gallery);?>
I did a few searches but can't find anything that works.
Does anyone have any ideas how to implement this, i'm stumped.
Thank you
I've worked this out and the code below works for me, there a probably a better more efficient way of doing it, but this works.
<?php $groups = getGroupOrder('gallery_section_title');
foreach($groups as $group) { ?>
<?php echo get('gallery_section_title', $group); ?>
<?php echo get('gallery_section_text', $group); ?>
<?php $fields = get_field_duplicate('gallery_section_image',$group);
foreach($fields as $field)
{ ?>
<?php echo $field[o]; ?>
<?php } ?>
<?php } ?>
Related
Enrol table:
From this table I try get value from active_shop
Here I have code:
<?php
$get_config_details = $this->db->get('enrol',$per_page, $this->uri->segment(1));
?>
<?php if ($get_config_details->num_rows() > 0):
foreach($get_config_details->result_array() as $get_config_detail):
$course_details = $this->crud_model->get_course_by_id($get_config_detail['course_id'])->row_array();?>
<?php echo $get_config_detail['active_shop']; ?>
<?php endforeach; ?>
<?php endif; ?>
Now I get the result: 1
But I copied this function from another function. I don't need the foreach function and if and so much code here. I need some simple code to squeeze this table and echo the result. Can anyone help me to correct this code?
Is there a way of adding a where class to a foreach equation in PHP.
At the moment I am adding an if to the foreach like this.
<?php foreach($themes as $theme){
if($theme['section'] == 'headcontent'){
//Something
}
}?>
<?php foreach($themes as $theme){
if($theme['section'] == 'main content'){
//Something
}
}?>
Presumably the PHP has to loop through all results for each of these. Is there are more efficient way of doing this. Something like
foreach($themes as $theme where $theme['section'] == 'headcontent')
Can this be done
A "foreach-where" would be exactly the same as a "foreach-if", because anyway PHP has to loop through all items to check for the condition.
You can write it on one line to reflect the "where" spirit:
foreach ($themes as $theme) if ($theme['section'] == 'headcontent') {
// Something
}
This becomes really the same as the construct suggested at the end of the question; you can read/understand it the same way.
It does not, however, address the fact that in the question's specific scenario, using any kind of "foreach-where" construction would in effect loop through all items several times. The answer to that lies in regrouping all the tests and corresponding treatments into a single loop.
Use a SWITCH Statement.
<?php
foreach($themes as $theme)
{
switch($theme['section'])
{
case 'headcontent':
//do something
break;
case 'main content':
//do something
break;
}
}
?>
You better use for loop for that like
<?php
$cnt = count($themes);
for($i = 0;$i < $cnt,$themes[$i]['section'] == 'headcontent' ;$i++){
}
?>
If someone uses MVC framework ,the answer to "foreach where" is here
<?php foreach ($plans as $plan) if ($plan['type'] == 'upgrade'): ?>
// Your code here
<?php endif; ?>
Remeber there is no need for the endforeach; statement after endif;
If someone wants to write more code between endif; and endforeach; then the above should be:
<?php foreach ($plans as $plan): if ($plan['type'] == 'upgrade'): ?>
// Your code here
<?php endif; ?>
// More of your code
<?php endforeach; ?>
I need a code that pulls out the listings count for a product in order to have them in the navigation. I am trying to get the product and listing number in the navigation (if there is any), but I don't want to display something if the listings count is equals to 0.
Here follows my code:
<?php if($listingsCount = getListingsCount(0,0,2,2,1,86) > 0): ?>
Bakery
<?php echo $listingsCount['totalRecords'] ?>.
<?php else: ?>
It should be
if ( ($listingsCount = getListingsCount(0,0,2,2,1,86)) > 0)
In this case you the assignment is done first and is then compared to 0. So $listingCount contains the real count.
The way you did it, the comparison is done first and the return value (true/false) is assigned to $listingCount.
Nevertheless. If you never reach the else-part, maybe something in your method getListingsCount() is broken.
Try this:
<?php if (($listingsCount = getListingsCount(0,0,2,2,1,86)) > 0) { ?>
Bakery
<?php echo $listingsCount['totalRecords']; ?>
<?php } else { ?>
Other text
<?php }?>
Thanks guys for your answers. managed to get it working with this...
<?php
$listingsCount = getListingsCount(0,0,2,2,1,86);
if($listingsCount['totalRecords'] > 0):
?>
<dd>
Bakery
[<?php echo $listingsCount['totalRecords'] ?>]
</dd>
<?php endif; ?>
How do you when using custom fields in Wordpress echo just the first value using foreach?
Currently the code is:
<?php for(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php endforeach; ?>
This takes the field from the wordpress page (the field is a link to another page), creates a link to that page using get_permalink but when I want to echo the page title it does it, but then it also echos all other values that are not needed.
If you just want the execute the first iteration of the loop, try this:
<?php foreach(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php break; ?>
<?php endforeach; ?>
Wouldn't it then be easier just to use the first element of the returned array? Maybe Wordpress offers other filters that return the the page's title only.
you can just add
$counter = 0;
<?php for(get_field('venue_event') as $post_object): ?>
$counter++;
if($counter == 1)
{
<?php echo get_the_title($post_object) ?>
}
<?php endforeach; ?>
I store post IDs in an array. I would like to loop through the array and display the IDs within a <div> containing <p> and <ul> tags, but only when at least one ID is in the array. If the array is empty no html can be returned. This implies that I should use some kind of if statement before the loop. Needless to say, my php skills are pretty basic and after two days of trying hard I am getting nowhere. Grateful for help!
My code (using Wordpress)
$postids = array();
...
$postids [] = $post->ID; //stores the post IDs in the array
Here is an update. I apologize for posting all this code as its quite messy with many things going on. It's the second loop of three (or more). The IDs displayed in a near identical first loop have been passed on. Only those IDs which have not been retrieved by the previous loop are displayed in order not to show any duplicate posts.
I have tried to remove all HTML markup and query the $postids with a new WP_Query after but that retrieves all posts I have ever created. I am pretty sure that's the right way to continue although I am obviously doing something wrong.
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[1]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5, //Display this number of related posts
'ignore_sticky_posts'=>1
);
$postids = array();
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<ul id="relatedposts">';
while ($my_query->have_posts()) : $my_query->the_post(); if (!in_array($post->ID, $ids)) {; $postids [] = $post->ID; ?>
<li><?php the_title(); ?></li>
<?php }
$ids[]= $post->ID;
endwhile;
}
}
?>
</ul>
<?php if ($postids){ //$postids has at least one value set
echo '<div>Related posts</div>'; //Outputting the header text. This works! If there are no IDs in the array nothing is shown.
};
?>
This should work:
<?php
// assuming you have an array of ids called $postids
if(count($postids)){
echo "<div><ul>";
foreach($postids as $id){
echo "<li>$id</li>";
}
echo "</ul></div>";
}
?>
To break it down:
if(count($ids)){
count() returns the number of elements in the array $ids. Any number other than zero will evaluate to true and enter the if statement, zero will evaluate to false and the whole thing will be skipped.
foreach($ids as $id){
This loops through each element in the array $ids and assigns it to the variable $id. Hopefully the echo statements are self explanatory.
There are a couple of ways to do it.
if ($postids){ //$postids is TRUE (ie $postids is not an empty array)
//do your output
}
OR
if(count($postids) > 0){ //$postids has at least one value set
//do your output
}
Getting used to simple tests of true and !false is your friend
Mayhaps something like this? You should customize this code to retrieve the contents of the posts, or whatever you'd like to do.
<?php
$postIds = array(1, 2, 3, 4, 5);
?>
<html>
<head>
<title>Post IDs!</title>
</head>
<body>
<h1>Post IDs!</h1>
<?php if(empty($postIds)): ?>
<p>There are no post IDs :(</p>
<?php else: ?>
<ul>
<?php foreach($postIds as $postId): ?>
<li><?php echo $postId; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</body>
</html>
Thanks to the great help by Gordon, I now have a working solution. All html is removed from the messy original code above. The following if statement and foreach loop echoes out the html in a simple and convenient way. Styling the and tags is now very straightforward.
<?php
if(count($postids)){
echo "<div>Related posts<ul>";
foreach($postids as $id){
echo '<li>'.get_the_title( $id ).'</li>';
}
echo "</ul></div>";
}
?>