I'm creating a website for a book publishing company. They've books in multiple different languages and want to display some details for each language. To retrieve all the necessary information for each language, I do the following:
<div class="col-lg-3" align="center">
<?php
$fields = get_field_objects();
if ($fields):
foreach ($fields as $name => $field):
if (stripos($name, 'isbn') !== false) : ?>
<?php $lang = substr($field['name'], strpos($field['name'], "_")); ?>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1<?php echo $lang ?>">Deutsch/<?php echo substr($field['label'], strpos($field['label'], " ") + 1); ?></a>
</h4>
</div>
<div id="collapse1<?php echo $lang ?>" class="panel-collapse collapse">
<div class="panel-body">
// ... get all neccessary information ...
</div>
</div>
</div>
</div>
<?php endif;
endforeach;
endif; ?>
</div>
Now my problem is, that if somebody creates a book, forgets to add a language and wants to add it after the book has been saved to the database, the alphabetical order is not correct anymore.
Therefore I'd like to add a sorting function for the $fields array.
sort($fields) doesn't work since after doing this, everything is blank.
Here's a screenshot of how the output looks like.
And sometimes, the order is wrong (e.g. Deutsch/English appears on the bottom). So I'll have to sort the part where I add the second language (next to German). The part where I add this is here:
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1<?php echo $lang ?>">Deutsch/<?php echo substr($field['label'], strpos($field['label'], " ") + 1); ?></a>
</h4>
Does anyone have any ideas? Please let me know if code is missing!
P.S. the plugin I use is "Advanced-Custom-Fields" and the functions like e.g. "get_field_objects()" come with that plugin!
get_field_objects() returns an array of custom field objects. You can sort it alphabetically using uasort like this:
$fields = get_field_objects();
uasort($fields,"sort_alphabetically");
function sort_alphabetically($a,$b) {
return $a['name'] > $b['name'];
}
So what solve my problem is #LeoTahk 's suggestion of adding ksort().
Final code:
<div class="col-lg-3" align="center">
<?php
$fields = get_field_objects();
//working solution
ksort($fields);
if($fields):
....
endif; ?>
</div>
Related
I have setup a small webstore and I want to show this <div class ="data-plan"></div> only for one single product. My database table from where I want to match this id is called sb_files and it has these fields id table_id path type slide_link, so I am trying to get my code to go trough that table, search up the id ( Its 13040100 ) and if it matches then to show the div, else it shows an empty div. I am using the Yii2 framework, which is php based. So far I have tried this
<?php if($product->$this->id('13040100')): ?>
<ul>
<?php
$index = 1;
foreach($product->() as $prd):
if(strpos($prd->getPath(), '13040100') == true) {
?>
<div class="wrap">
<div class="data-plan" style="height:20px; width:65px; background-color:#00a651; float:right;color:white;margin:10px;text-align:center;">A+++ </div>
<div class="data-plan" style="height:20px; width:65px; background-color:#ed1c1c; float:right;color:white;margin:10px;">Datu lapa </div>
</div>
<?php
$index++;
}
endforeach;
?>
</ul>
<?php else: ?>
<div class="wrap">
</div>
<?php endif; ?>
I'm not that familiar with Yii2 framework myself, but are you sure that $product->$this->id('13040100') is correct? It looks weird to me, shouldn't it be something like $product->id('13040100')?
The solution was way easier than I tought, just had to think simple
<?php if ($product->id == '13001100'): ?>
<div class="wrap">
<div class="data-plan" style="height:20px; width:65px; background-color:#00a651; float:right;color:white;margin:10px;text-align:center;">A+++ </div>"
<div class="data-plan" style="height:20px; width:65px; background-color:#ed1c1c; float:right;color:white;margin:10px;">Datu lapa </div>
</div>
<?php else: ?>
<div class="wrap">
</div>
<?php endif;
These two lines of code specifically - ($teamproject as $teamproject) and ($cattypes as $teamproject). Basically it's an image card that displays the catergory (cattypes) and the main image and the title of that specific image card.
<div class="container" id="projectcards">
<div class="row">
<?php
if(!empty($error)){
echo $error;
}
if (!empty($teamproject)) {
foreach ($teamproject as $teamproject): ?>
<a href="/project-single/1" class="col-md-6 col-sm-6 col-xs-12 thumbnailx">
<?php foreach ($cattypes as $teamproject): ?>
<h4 class="post-content"><?= h($teamproject->cattype_title)?></h4>
<?php endforeach; ?>
<img src="<?= h($teamproject->mainimg)?>">
<h3><?= h($teamproject->title)?></h3>
</a>
<?php
endforeach;
}
?>
</div>
</div>
You're misunderstanding how foreach works in PHP. The first item should be the name of an array, which it will iterate over, and the second what you want to call each item. This statement:
foreach ($teamproject as $teamproject):
Shouldn't list the same variable twice.
If it's just a single entity, don't iterate over it at all, just directly access its values.
If it's an array containing a list of teamproject entities, then just give the item a unique name for the foreach loop:
foreach ($teamproject as $project)
The second nested foreach- if this is supposed to be a list of headers for each specific team project, then it probably should be a list inside the single $project entity? As you have it written, it's just iterating over $cattypes array, and on each iteration, printing the same $project title over and over again.
If cattypes is supposed to be a list on each $project, then its something vaguely along the lines of:
foreach($project->cattypes as $cattype)
Replacing "cattypes" with what ever that list is actually called on your teamproject entity.
Rather than adding to foreach conditions, I had to add an IF conditions to get it working.
<div class="container" id="projectcards">
<div class="row">
<?php
if (!empty($teamproject)) {
foreach ($teamproject as $team): ?>
<a href="/projects-single/<?= h($team->id)?>" class="col-md-6 col-sm-6 col-xs-12 thumbnailx">
<?php foreach ($cattypes as $cattype):
if ($team->cattype_id == $cattype->id ) {
?>
<h4 class="post-content"><?= h($cattype->title)?></h4>
<?php
} endforeach; ?>
<img src="<?= h($team->mainimg)?>">
<h3><?= h($team->title)?></h3>
</a>
<?php
endforeach;
}
?>
</div>
I'm using the text helper class in CodeIgniter and want to create an if/else-statement to check if an article is longer than, let's say, 20 words so that I can add a "read the full article" link when articles exceeds this limit. However, I'm not sure how to proceed using this particular function and if it's even possible without creating a new function. Any ideas on how to resolve my issue?
<div class="col-md-8">
<?php foreach ($company_reviews as $review) {
$rating = $review->company_reviews_rating;
$comment = word_limiter($review->company_reviews_comment, 10);
?>
<div class="review-box">
<div class="row">
<div class="col-sm-3">
<?= $review->first_name; ?> <?= $review->last_name; ?>
</div>
<div class="col-sm-9">
<?php echo get_stars($rating); ?>
<h4><?= $review->company_reviews_title; ?></h4>
<?= $comment; ?>
</div>
</div>
</div>
<?php } ?>
</div>
The third parameter is an optional suffix added to the string. By default it adds an ellipsis.
$comment = word_limiter($str, 10, '...read full article');
Having a bit of a problem, i'm using ACF for WordPress, i have defined the color picker on the services template which color codes the page heading in a color you select. This template then has a page link which relates to another template: case studies - the case studies template has a select option where you select the page from services that relates to the case study. I can get those values out as it is on the case studies template. How do i go about getting the color picker value from the services page and apply the color to the case studies page? Here is what i have done so far;
<div class="caseStudies">
<h1 class="pageTitle uppercase"><?php echo the_title(); ?></h1>
<div class="caseStudyIntro"><?php echo the_content(); ?></div>
<?php
foreach($caseStudyChildren as $caseStudies)
{
$cStudyId = $caseStudies->ID;
$cStudyThumbnail = get_field("thumbnail_image", $cStudyId);
$cStudyRelation = get_field("case_study_relation", $cStudyId);
$cStudyContent = get_field("case_study_content", $cStudyId);
?>
<div class="caseStudyItem">
<div class="col-md-3 noPaddingLeft">
<a href="<?php echo get_permalink($cStudyId); ?>">
<img src="<?php echo $cStudyThumbnail['url']; ?>" alt="<?php echo $cStudyThumbnail['alt']; ?>" class="img-thumbnail cStudyThumb" />
</a>
</div>
<div class="col-md-9 noPadding">
<div class="content">
<h1 class="title uppercase">
<a href="<?php echo get_permalink($cStudyId); ?>" class="dark-black">
<?php echo get_the_title($cStudyId); ?> <?php echo '- '.$cStudyRelation; ?>
</a>
</h1>
<div class="study">
<?php echo wp_trim_words($cStudyContent, 40, '...'); ?>
</div>
Read More...
</div>
</div>
</div>
<div class="clearfix"></div>
<?php
}/* foreach end */?>
</div>
This line here is where i am adding the services link that relates to the case study (so it almost acts like a tag)
<?php echo get_the_title($cStudyId); ?> <?php echo '- '.$cStudyRelation; ?>
Backend Configuration
Hope that made some sense, if not let me know and i can add to it more. PHP newbie here so go easy one me, haha!
Thanks.
Managed to figure this out in the end. I've got the relationship to the page coming out now and now i've got the color custom field that was defined on the services template coming out on the case studies page.
So i defined the field name in a variable;
$cStudyRelationship = get_field("service_case_study_relationship", $cStudyId);
Then did the following;
$cStudyRelationshipId = '';
$caseStudyTitleColor = '#000';
if(isset($cStudyRelationship[0])){
$cStudyRelationshipId = $cStudyRelationship[0]->ID;
$caseStudyTitleColor = get_field("case_study_title_colour",$cStudyRelationship[0]->ID);
$caseStudyTitleColor = '#'.ltrim(trim($caseStudyTitleColor),'#');
} ?>
So built up the array for the color field created in ACF and then echoed that out against the ID the case study was related to. Hope that makes sense! Not the best at explaining stuff as you can tell.
The last piece of the jigsaw was to apply the color value against the page relationship page title;
<?php echo get_the_title($cStudyId); ?> <?php echo '- '?> <span class="colouredTitle" style="color:<?php echo $caseStudyTitleColor; ?>;"><?php echo ($cStudyRelationship[0]->post_title); ?></span>
Thank you to those that have taken the time out to contribute to this question.
What I need to do that my latest posts go automatically to top of the page? Now latest post go bottom of the ealier posts.
php:
<div id="container">
<div id="blog">
<div class="grid_9 float-left">
<?php foreach($posts->results as $post): ?>
<div class="post box_shadow">
<h2><?php echo $post->title ?></h2>
<small><?php echo date('d-m-Y',strtotime($post->date)) ?></small>
<p><?php echo Str::limit_word($post->content, 40); ?></p>
<?php echo Lang::line('home.blog_read_more', array(), $lang)->get() ?>
</div>
<?php endforeach ?>
<?php echo $posts->links()?>
</div>
</div>
<div id="footer" class=" box-shadow">
<?php echo stripcslashes($setting->footer)?>
</div>
</div>
I am still newbie so could you give advice what line I need modified or add and where?
If it is exactly the opposite of what your looking for try running array_reverse on it before hitting the loop.
That's how your array is sorted. options: reverse the array or do a for loop starting from the last index going to 0.
Something similar to below:
`$posts->results` has all of your posts
Replace the foreach with a for.
$count = count($posts->results);
for($i = $count-1; $i <= 0; $i--) {
$post = $posts->results[$i];
//display post
}
The simplest but probably less efficient way is to reverse the array right before the foreach loop.