How to hide "image" custom field if empty - php

I created an unordered list of custom fields and I wish to hide them if they are empty. For text custom fields I used the code:
<?php if (get_field('phone') != '') { ?>
<li><strong>Phone: </strong><?php the_field('phone'); ?></li>
<?php } ?>
However, I have a custom field which is for images, like this:
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
How can I hide the field if no image was uploaded (obviously, the above code won't work)?
Thanks in advance.

I think it should be
<?php if (get_field('logo') != ''): ?>
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
<?php endif; ?>

Assuming the_field('logo') will return a falsy value if there are no images
if (the_field('logo')) {
?>
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
<?php
}

<?php if( get_field('field_name') ): ?>
<p>My field value: <?php the_field('field_name'); ?></p>

Related

If statement to hide a <Div>

I'm pulling both the 'book_series' and 'series_link' from custom fields within Wordpress.
The link shows up correctly when a name and URL are included but it shows "Books Series:' on blank pages (that don't include links or series). How can I hide the content when left blank?
Here is what I have so far:
<?php if (('books_series') == true) : ?>
<div class="series"><b>Book Series: </b><?php the_field( 'books_series' ); ?></div>
<?php else: ?>
<div class="series" hidden></div>
<?php endif; ?>
Thank you.
Maybe,
if (('books_series') == true)
Should be:
if (the_field('books_series') == true)
This is what I came up with and it seems to work, although I'm not sure how functional it is overall - `
<?php if ($series_link) { ?>
<div class="series"><b>Book Series: </b><?php the_field( 'books_series' ); ?></div>
<?php } ?>`
Thank you everyone for your help and suggestions.

Wordpress Advanced Custom Fields if statment

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

ACF - change link based on backend select choice in a repeater field

am using Advanced Custom Fields for many things on my site. One thing in particular is for a staff profile page. I have a select field where the staff can add social icons or an email icon. The repeater field is 'social' and if they choose to 'add a row' there is 'social channel' select field and a 'social_link' test field. My current code is this:
<?php if ( have_rows('social')): ?>
<div class="staff-social">
<?php while ( have_rows('social')) : the_row() ?>
<li><img src="<?= get_template_directory_uri(); ?>/img/footer-<?php the_sub_field('social_channel') ?>.svg" alt="social icon" /></li>
<?php endwhile; ?>
</div><!--end staff-social-->
<?php endif; ?>
I need to prepend a 'mailto:' to my anchor tag if the user selects 'mail' from the 'social_channel' dropdown in the backend. I have tried doing:
<?php while ( have_rows('social')) : the_row() ?>
<li>
<?php $select = get_sub_field_object('social_channel');
$choices = $select['choices'];
foreach ($choices as $choice) {
if ($choice == 'mail') {
echo '<a href="mailto:'.the_sub_field('link').'">';
} else echo '<a href="'.the_sub_field('link').'">';
} ?>
<img src="<?= get_template_directory_uri(); ?>/img/footer-<?php the_sub_field('social_channel') ?>.svg" alt="social icon" />
</a>
</li>
<?php endwhile; ?>
But this of course spits something out for all choices, whether or not they are selected by the user in the backend. Can anyone help me with this? I think this is pretty basic PHP but I'm not sure how to do it. Any help will be much appreciated!
Your Select field should return just a single string, not an array, (make sure you set the 'social_channel' field to NOT allow multiple values) so change your code to this:
<?php while ( have_rows('social')) : the_row() ?>
<li>
<?php $select = get_sub_field('social_channel');
if($select == 'mail'){
$linkURL = 'mailto:'.get_sub_field('link');
}else{
$linkURL = get_sub_field('link');
} ?>
<img src="<?= get_template_directory_uri(); ?>/img/footer-<?php the_sub_field('social_channel') ?>.svg" alt="social icon" />
</li>
<?php endwhile; ?>

PHP echo as condition inside PHP if statement

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).

WordPress Next and Prev posts - confused by categories

I've set up a WordPress navigation for my single.php template that gets the previous and next post's thumbnails like this:
<?php
// Newer posts
$nails_next_post = get_next_post('%link', '', FALSE, 3 ); // Get the previous post
$nails_next_post_thumbnail = get_the_post_thumbnail($nails_next_post->ID); // Get thumbnail
?>
<?php if ($nails_next_post != null) : ?>
<div class="post-nav-next">
<?php if ($nails_next_post_thumbnail != null): ?>
<?php echo $nails_next_post_thumbnail; ?>
<?php else : ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-90x90.gif" />
<?php endif; ?>
<?php next_post_link('%link', 'Forward' , TRUE, 3 ); ?>
</div>
<?php endif; ?>
<?php
// Older posts
$nails_prev_post = get_previous_post('%link', '', FALSE, 3 ); // Get the previous post
$nails_prev_post_thumbnail = get_the_post_thumbnail($nails_prev_post->ID); // Get thumbnail
?>
<?php if ($nails_prev_post != null) : ?>
<div class="post-nav-previous">
<?php if ($nails_prev_post_thumbnail != null): ?>
<?php echo $nails_prev_post_thumbnail; ?>
<?php else : ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-90x90.gif" />
<?php endif; ?>
<?php previous_post_link('%link', 'Back' , TRUE, 3 ); ?>
</div>
<?php endif; ?>
The problem I am having is that the links continue to point to the next or previous post in the current post's category, rather than just the next or previous post in the chronology (except posts in category 3 of course). I'm out of my deoth here. Does anyone have any thoughts? Thanks :-)
I think you are using the wrong parameters on the get_next_post and get_previous_post functions. You probably accidentally used the parameters which go with next_post_link/previous_post_link
You only need two parameters and both are optional:
http://codex.wordpress.org/Function_Reference/get_next_post
Simply try this:
$nails_next_post = get_next_post();

Categories