For some reason I can't get my if-statement to echo out the HTML.
<?php
$my_description = meta('description');
if (!empty($my_description)): echo '<p class="description">'.$my_description.'</p>';
echo '<br>'; ?>
<?php endif; ?>
It only outputs the text, nothing else.
meta('description') is from a plugin in Wordpress that should output the text I placed in the backend. The above code ONLY outputs the following: Lorem Ipsum...
Update: I would like it to output:
<p class="description">Lorem Ipsum...</p>
After a discussion in chat, we discovered that the meta() function was not returning the value we expect. The correct function is get_post_meta
<?php
$my_description = get_post_meta(get_the_ID(), 'description', 1);
if (!empty($my_description)):
?>
<p class="description"><?php echo $my_description; ?></p>
<br>
<?php endif; ?>
Ugh... sooo ugly, just do it like this:
<?php
echo !empty($my_description) ? '<p class="description">'.$my_description.'</p>' : '<br />';
?>
Use the ternary operator, don't use that messy if formatting...
Related
i have this code :
<?php include("Hedar.php"); ?>
<?php
$q=mysql_query("select * from news2 where id = $news2_id ");
list($news2_ides,$news_id,$name,$text,$stext,$ktext,$dtext,$date,$vews,$path)=mysql_fetch_array($q);
?>
<img src="upload1/<?php echo $path ?>"/>
<h2 class="_title"><?php echo $name ?></h2>
<?php echo $text ?>
<?php include("Footers.php"); ?>
------------
so as you can see i have a site in PHP code
so i want to insert 2 adsense code into $text
i tried to separat this $text to many but i fail with it
so anyone can help me to separate this ( <?php echo $text ?> )
thanks a lot
Regards
If am right you want to display adsense code with the $text variable
so you do:
<?php
$text.=$adsense
echo $text;
?>
If you are storing your adsense code in $adsense variable
How can i show some default code only if BOTH if statements are false ?
I've got this code -
<?php if( get_sub_field('cta_phone')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_phone');?>"><?php the_sub_field('cta_phone');?></a></span>
<?php else: ?><?php endif; ?>
<?php if( get_sub_field('cta_mobile')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_mobile');?>"><?php the_sub_field('cta_mobile');?></a></span>
<?php else: ?><?php endif; ?>
<?php else: ?>
<img src="http://my-domain/image.jpg">
<?php endif; ?>
i'm not using the else bit at the end at the moment because i only want that to show if both 'if's are false ?
hope that it makes sense
You can use 1 php tag only to make your syntax much more readable and
avoiding
multiple <?php ?> tags.
you can also use echo to print your html markup using php scripting like this.
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a></span>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a></span>';
} else {
//do what you want to do here, if they are false.
}
Ok thanks , the 'else' works if neither of the if's are true , but if i have one that is true or both are true , it only displays the first entry but shows it twice ?
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a>';
} else {
echo '<img src="http://placehold.it/350x150">';
}
?>
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).
I can't get this script to work for nothing. I tried many different ways to make this work but it's not working.
<?php
if (!empty ($image->alttext )) : ?>
<div class="thumbtitle"><?php echo $image->alttext ?></div>
<?php endif; ?>
Any help would be appreciated!
It always shows true that there is alttext when there isn't sometimes.
Your question is vague but try the following
<?php
if (strlen(trim($image->alttext)) > 0) : ?>
<div class="thumbtitle"><?php echo $image->alttext ?></div>
<?php endif; ?>
As said your string is probably not empty but with whitespaces.
use var_dump() or strlen() to find out. With trim you remove the whitespaces.
The value in $image->alttext is probably not blank, maybe there are white spaces in it.
You can trim it first before testing.
$imgText = trim($image->alttext);
if (!empty ($imgText)) : ?>
<div class="thumbtitle"><?php echo $image->alttext ?></div>
<?php endif; ?>
Note: use var_dump($image->alttext) to see that it has the value you are expecting.
Argh, this code is not pulling through my custom meta.
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
if (!empty($post_meta)) {
?>
<div class='client-testimonial'><?php echo $my_meta['testimonial']; ?></div>
<div class='client-name'><?php echo $my_meta['name']; ?></div>
<?php
}
?>
But the one below works, the only reason I am not using it is because it still shows the speach marks and dash when the fields are left empty in the admin panel
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
echo "<div class='client-testimonial'>". "'".$my_meta['testimonial']."'". "</div>";
echo "<div class='client-name'>". "-" .$my_meta['name']."</div>";
?>
Please help me on why the first code is not echoing the info. I am at the end of my tether!
You're checking if $post_meta is not empty, you don't have a variable named $post_meta
Change:
if (!empty($post_meta))
to
if (!empty($my_meta))
i think u have checked wrong variable.
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
if (isset($my_meta) && !empty($my_meta)) {
?>
<div class='client-testimonial'><?php echo $my_meta['testimonial']; ?></div>
<div class='client-name'><?php echo $my_meta['name']; ?></div>
<?php
}
?>