How do I do If Post Meta DOES NOT exist? - php

I have the following code:
<?php $buycheck = get_post_meta($post->ID, 'buy-link', true); ?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
Later in my code I want to be able to say that if buy-link does not exist - i.e. there is no data in that field - then do something, else do something different.
Not sure how to do this! Help appreciated!
(By the way, I posted this question to Wordpress Stack Exchange first. It was voted closed there because it apparently concerns PHP boolean logic more than Wordpress - https://wordpress.stackexchange.com/questions/60387/how-do-i-do-if-post-meta-does-not-exist#comment78412_60387)

<?php if($buycheck ==''){ /*stuff*/ } ?>
this will render $buycheck, and if it is empty == is equal to '' nothing.

You can set a global variable that you can check later to see if the buylink exists:
<?php
$buycheck = get_post_meta($post->ID, 'buy-link', true);
$GLOBALS['buy_link_exists'] = !empty($buycheck);
?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
Then later on in your code:
<?php if ($GLOBALS['buy_link_exists'])): ?>
it exists, do one thing
<?php else: ?>
it does not exist, do something else
<?php endif; ?>
If you need the actual value, you can set a global containing the return value from get_post_meta so you can use the actual value.

Related

How to Wrap A Condition Inside Another Condition in PHP

Can anyone please help me wrap the condition inside another condition in php.
I have this code #1 that I want to be inside code #2.
Here's code #1
<?php if( get_field('highlights') ): ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
Here's code #2
<?php if(strstr($_SERVER['HTTP_REFERER'],'www.example.com'))
{
echo '**CODE #1 should be placed here**';
}
?>
Sorry, I don't haev any knowledge in PHP.
Wrapping code 1 inside code 2
After several trial and error, here's what I have to make it work. Please correct me if there's something wrong or to improve.
<?php if (strstr($_SERVER['HTTP_REFERER'], 'www.google.com')){ ?>
<?php if( get_field('highlights') ):?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
<?php } ?>
This should work in theory. Though I'm unsure what the highlights field is for.
<?php
if(strstr($_SERVER['HTTP_REFERER'],'www.example.com')){
if( get_field('highlights') ){ ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php
}
}
?>
You may want to replace <?php the_field('highlights'); ?> with something like <?=highlight ?> and iterate through the highlights in a loop. That depends on the situation though.

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.

Change output last item in loop (ACF)

I'm still learning php, but can't get my head on this one.
For a loop in Wordpress I want to output a list with places, seperated with a comma and ending with a point.
Here's my code:
<?php if ( have_rows('subplaats') ): ?>
<section id="subplaats">
<div class="subplaats-container">
<h3 class="support">Wij bestrijden ook in...</h3>
<p>
<?php while( have_rows('subplaats') ): the_row(); ?>
<?php $plaats = get_sub_field('plaats'); ?>
<?php echo $plaats; ?>,
<?php endwhile; ?>
</p>
</div>
</section>
<?php endif; ?>
Could anyone tell me how to accomplish this? I'm using Advanced Custom Fields. Am I also doing right on hierarchical level?
You need to count the total fields in the repeater:
count(get_field('subplaats'));
then have a field counter to check if the current "counted" field is the last one.
I edited and tested your code and It's working good:
<?php
if (have_rows('subplaats')):
$all_fields_count = count(get_field('subplaats'));
$fields_count = 1;
?>
<section id="subplaats">
<div class="subplaats-container">
<h3 class="support">Wij bestrijden ook in...</h3>
<p>
<?php
while (have_rows('subplaats')): the_row();
$plaats = get_sub_field('plaats');
echo $plaats;
if ($fields_count == $all_fields_count) {
echo ".";
} else {
echo ", ";
}
$fields_count++;
endwhile;
?>
</p>
</div>
</section>
<?php
endif;
?>
Cheers!

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