I can't seem to display only the content I request. I am using the Advanced Custom Fields (ACF) plugin with my Wordpress site, and using a repeater field with a multiple select field for "departments \ job titles".
With the code below, I am looking to display only the content which has the value that equals what I select. Maybe I am looking at this wrong but the content displays whether the strpos is true or not.
<?php if (get_field('policy_links') ): ?>
<center><h3><a name="All"></a>All Employees</h3></center>
<hr>
<ul class="bullets">
<?php while (has_sub_field('policy_links')) : ?>
<?php $jobtype = get_sub_field('who_is_this_for'); ?>
<?php if (strpos ($jobtype, 'ma') !== false) { ?>
<li><a href="<?php the_sub_field('link_to_the_document'); ?>">
<?php the_sub_field('display_name'); ?></a><span> - <small>
<?php the_sub_field('link_notes'); ?></small></span>
<?php the_sub_field('who_is_this_for'); ?></li>
<?php } else { ?><p>fail </p>
<?php }; // end of the loop. ?>
<?php endwhile; // end of the loop. ?>
</ul>
<?php endif; ?>
Your if statement should be done like this:
<?php if(get_sub_field('who_is_this_for') == 'strpos') { ?><?php }?>
Where "strpos" is the value you select (if I follow correctly).
It would probably be beneficial to actually see your fields.
I was on the right track. It turns out the ACF is converted to an array when multiple values are selected. I performed an implode to the variable then used the converted string performed the validation.
thanks for the help
Related
I’ve been wracking my brain tonight trying to figure out how to display multiple labels from a select field.
First I tried it with a single field and that is working. Now I want to make use of multiple fields but can't get it working. Can someone help me?
Thanks!
<?php if ( get_field( 'locationCompany', $joboffer->ID) ) : ?>
<span class="company-compact"><? echo (get_field('locationCompany', $joboffer->ID))?></span>
<?php endif ?>
Make sure that you enable the Select multiple values? in the ACF settings. Here's your code:
<?php
$locationcompany = get_field( 'locationCompany', $joboffer->ID);
if ( $locationcompany ) :
foreach ($locationcompany as $value): ?>
<span class="company-compact"><?php echo $value; ?></span>
<?php
endforeach;
endif;
?>
You can use the same approach as above but you need to wrap the whole div with the foreach:
<?php
$locationcompany = get_field( 'locationCompany', $joboffer->ID);
if ( $locationcompany ) :
foreach ($locationcompany as $value): ?>
<div id="job-offer-filter"
class="col-xl-4 col-lg-6 col-md-6 col-sm-12 col-xs-12 height-130 job-offer-card all
<? echo (get_field('company', $joboffer->ID))?>
<? echo ($value) /*** HERE ***/?>
<? echo (get_field('workingtime', $joboffer->ID) === "fulltime" ? "Fulltime" : "Parttime")?>
<? echo (get_field('jobCategories', $joboffer->ID))?>">
<?php
endforeach;
endif;
?>
Please note if this div is in the same file with the span tag from above, you can just put this div inside the foreach. To avoid repetition of code.
I am trying to list authors on a page, but exclude the admin (myself). I am using a child theme of squirrel and this is my code so far:
<?php
$authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts);
if($authors):
foreach($authors as $author):
?>
<div class='author' id='author-<?php the_author_meta('user_login', $author->post_author); ?>'>
<h3><?php the_author_meta('display_name', $author->post_author); ?></h3>
<?php if(get_the_author_meta('description', $author->post_author)): ?>
<div class='description'>
<?php echo get_avatar(get_the_author_meta('user_email', $author->post_author), 80); ?>
<p><?php the_author_meta('description', $author->post_author); ?></p>
</div>
<?php endif; ?>
<?php
$recentPost = new WP_Query('author='.$author->post_author.'&showposts=1');
while($recentPost->have_posts()): $recentPost->the_post();
?>
<h4>Recent Article: <a href='<?php the_title();?>'><?php the_title(); ?></a></h4>
<?php endwhile; ?>
</div>
<?php endforeach; endif; ?>
I tried using the solution from this discussion, but I don't think I am doing it right because when I add this line of code:
if(get_the_author_meta('display_name', $author->post_author) != 'admin'):
under:
foreach ($authors as $author):
it just breaks the entire site (the screen is white). This is all new to me, so can someone please help me figure out what I am doing wrong?
Thanks a lot!
The white screen you are experiencing is a fatal PHP error. You are not seeing what the error is, for security reasons.
However, during development, you want this feature off. Just edit wp-config.php and set WP_DEBUG to true.
As for your question, you might want something like:
if($author->post_author == 1) continue;
...as the first line inside the foreach. The id 1 should be your userid because the first user created in WP has 1, and the keyword continue jumps to the end of the foreach, thus skipping your user.
If you prefer doing this by username, use this:
if(get_the_author_meta('user_login', $author->post_author) == 'admin') continue;
I would like to use a PHP echo as a condition inside a PHP if statement.
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this case, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
What could be an option is to save all author within an array
// if article->author() isn't within the array
$authors[] == $article->author();
After that you could go as the following:
<?php foreach($authors as $author){ ?>
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
<?php } ?>
That should work, even if you must do 2 foreachs
<?php if( $article->author() == $page->author() ) { ?>
<p><?php echo $article->title(); ?></p>
<?php } ?>
should work, but you can also try
<?php
if( $article->author() == $page->author() ) {
echo "\n<p>", $article->title(), "</p>\n";
}
?>
which to me looks "cleaner"; but you'd have to have a look for missing whitespaces
I suggest trying to set it equal too a variable and then using that variable in the if statement.
<?php foreach(page('magazine')->children() as $article): ?>
<?php $condition = $page->author()?>
<?php if($article->author() == $condition ?>'): ?>
echo "\n<p>", $article->title(), "</p>\n";
<?php endif ?>
<?php endforeach ?>
I am not sure if my syntax is correct but i think it is something along them lines.
You cannot use echo in condition because it is special language construct that sends given contents to the output stream and it returns no value.
Are you sure you shouldn't have this?
<div id="automatic_list_of_articles">
<?php $page = page('magazine'); ?>
<?php foreach($page->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
I have reconstructed an approximation of what looks to be your data, and you can see it working at the link below. It correctly echo's multiple article titles.
Working example:
http://ideone.com/jvLVhF
In this example you can see the PHP as above works correctly, and it is likely a data issue (ie. you should perhaps be using $page and not calling a function in the foreach statement).
One of the conditions in my if statement does not work anymore when the if statement is inside a foreach statement. I understand where does the issue come from, but don't know how to solve it:
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this example, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
I've picked up a project from another developer and I'm trying to finish and clean up a bit. This part of the code renders products from a database after checking for a language cookie:
<?php if (get_cookie('spanish')) : ?>
<?php if ($product['details_spanish'] != '') : ?>
<?php $details = explode(':',$product['details_spanish']); ?>
<h3>Especificaciones</h3>
<?php else : ?>
<?php if ($product['details'] != '') : ?>
<?php $details = explode(':',$product['details']); ?>
<h3>Especificaciones</h3>
<?php endif ?>
<?php endif; ?>
<?php else : ?>
<?php if ($product['details'] != '') : ?>
<?php $details = explode(':',$product['details']); ?>
<h3>Specifications</h3>
<?php endif; ?>
<?php endif; ?>
<ul>
<?php if ($details) : ?>
<?php foreach ($details as $detail) : ?>
<?php $detail = split(',',$detail); ?>
<?php if ($detail[0] != '' && $detail[1] != '') : ?>
<li>
<strong><?=ucwords($detail[0])?></strong> : <?=$detail[1]?>
</li>
<?php endif; ?>
<?php $i++; ?>
<?php endforeach; ?>
<?php endif; ?>
Sorry, I know this is hard to read due to inconsistency and bad coding (not my project initially. What it's doing is checking for a spanish cookie on the users machine. If it finds it, it pulls the values from the spanish column of the table instead of the regular. It falls back on the english value if there is no spanish value in the table. Also, it creates a $details variable that contains one of the values of the database, and splits it where the : is.
The problem I'm having is on some products, it gives me an error saying it can't find the $details variable, I'm assuming it is because the variable doesn't live outside the scope of the first if statement?
I'm a php noob really but even I know this isn't good practice. is there a better way to clean this up and have my $detail variable available?
Quick workaround is to pre-initialize that variable at the very top:
<?php $details = ''; ?>