Add an if alternative statement inside another if statement within a loop with php to create two links (one real and one empty)How to add elements to an empty array in PHP?Creating default object from empty value in PHP?PHP Loops within LoopsPHP If statement inside For loopIf Statement within While LoopGetting ID from get_posts within a loop in WordpressExecuting two statements alternatively,inside a looptwo foreach loop on if statementUpdate available posts in all ACF Post Object fields on change (Wordpress/Advanced Custom Fields)

Obtaining database information and values in extended properties

Forgetting the musical notes while performing in concert

How obscure is the use of 令 in 令和?

What does the same-ish mean?

Why didn't Boeing produce its own regional jet?

Standard deduction V. mortgage interest deduction - is it basically only for the rich?

Which ISO should I use for the cleanest image?

Does the Idaho Potato Commission associate potato skins with healthy eating?

Do creatures with a speed 0ft., fly 30ft. (hover) ever touch the ground?

Convert seconds to minutes

Why was Sir Cadogan fired?

Mathematica command that allows it to read my intentions

Why was the shrink from 8″ made only to 5.25″ and not smaller (4″ or less)

Why do I get negative height?

Is it possible to create a QR code using text?

GFCI outlets - can they be repaired? Are they really needed at the end of a circuit?

Why is the sentence "Das ist eine Nase" correct?

How to compactly explain secondary and tertiary characters without resorting to stereotypes?

Processor speed limited at 0.4 Ghz

Finitely generated matrix groups whose eigenvalues are all algebraic

Pact of Blade Warlock with Dancing Blade

In the UK, is it possible to get a referendum by a court decision?

How to remove border from elements in the last row?

Do Iron Man suits sport waste management systems?



Add an if alternative statement inside another if statement within a loop with php to create two links (one real and one empty)


How to add elements to an empty array in PHP?Creating default object from empty value in PHP?PHP Loops within LoopsPHP If statement inside For loopIf Statement within While LoopGetting ID from get_posts within a loop in WordpressExecuting two statements alternatively,inside a looptwo foreach loop on if statementUpdate available posts in all ACF Post Object fields on change (Wordpress/Advanced Custom Fields)













0















First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:



if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>


My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").



Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.



Tha basic code in php:



<?php 

$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];

if ( $posts = get_posts( $args ) )
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post )
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();

echo '</ul>';


?>


Any Idea why it is not working?










share|improve this question
























  • What's broken? What is the result of your code?

    – dave
    Mar 8 at 21:02















0















First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:



if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>


My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").



Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.



Tha basic code in php:



<?php 

$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];

if ( $posts = get_posts( $args ) )
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post )
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();

echo '</ul>';


?>


Any Idea why it is not working?










share|improve this question
























  • What's broken? What is the result of your code?

    – dave
    Mar 8 at 21:02













0












0








0








First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:



if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>


My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").



Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.



Tha basic code in php:



<?php 

$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];

if ( $posts = get_posts( $args ) )
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post )
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();

echo '</ul>';


?>


Any Idea why it is not working?










share|improve this question
















First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:



if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>


My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").



Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.



Tha basic code in php:



<?php 

$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];

if ( $posts = get_posts( $args ) )
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post )
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();

echo '</ul>';


?>


Any Idea why it is not working?







php loops if-statement advanced-custom-fields






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 20:49







user3833012

















asked Mar 8 at 20:41









user3833012user3833012

377




377












  • What's broken? What is the result of your code?

    – dave
    Mar 8 at 21:02

















  • What's broken? What is the result of your code?

    – dave
    Mar 8 at 21:02
















What's broken? What is the result of your code?

– dave
Mar 8 at 21:02





What's broken? What is the result of your code?

– dave
Mar 8 at 21:02












1 Answer
1






active

oldest

votes


















2














From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)






share|improve this answer























  • Exactly, the right snippet is: if (get_field('industry_partner_links')) :

    – user3833012
    Mar 8 at 21:16












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070701%2fadd-an-if-alternative-statement-inside-another-if-statement-within-a-loop-with-p%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)






share|improve this answer























  • Exactly, the right snippet is: if (get_field('industry_partner_links')) :

    – user3833012
    Mar 8 at 21:16
















2














From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)






share|improve this answer























  • Exactly, the right snippet is: if (get_field('industry_partner_links')) :

    – user3833012
    Mar 8 at 21:16














2












2








2







From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)






share|improve this answer













From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 21:05









Damian DziaduchDamian Dziaduch

794612




794612












  • Exactly, the right snippet is: if (get_field('industry_partner_links')) :

    – user3833012
    Mar 8 at 21:16


















  • Exactly, the right snippet is: if (get_field('industry_partner_links')) :

    – user3833012
    Mar 8 at 21:16

















Exactly, the right snippet is: if (get_field('industry_partner_links')) :

– user3833012
Mar 8 at 21:16






Exactly, the right snippet is: if (get_field('industry_partner_links')) :

– user3833012
Mar 8 at 21:16




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070701%2fadd-an-if-alternative-statement-inside-another-if-statement-within-a-loop-with-p%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229