Quick PHP echo help - php

What is wrong with this?
echo '<a title="Last Chance" href="'.the_permalink().'" class="status open">Last Chance</a>';
As it's putting the the_permalink() before the <a instead of inside it.

Wordpress often echo's the content out of the function instead of returning it.
Use get_permalink() instead.
echo '<a title="Last Chance" href="'.get_permalink().'" class="status open">Last Chance</a>';
http://codex.wordpress.org/Function_Reference/get_permalink
http://codex.wordpress.org/Function_Reference/the_permalink

Actually it looks good to me (but see my edit comment).
Better is to embed PHP into HTML:
<a title="Last Chance" href="<?php the_permalink(); ?>" class="status open">
Last Chance
</a>
Edit: As #Marwelln found out, the_permalink() is already echoing data. Still, this is a better solution than echoing the HTML.

I guess you have echo "abc" in the the_permalink function. In order for this to work as you wish, you have to return "abc" instead of using echo.

Use it like this (not inside echo )
<a title="Last Chance" href=" <?php the_permalink() ?> " class="status open">Last Chance</a>
See http://codex.wordpress.org/Function_Reference/the_permalink for more details.

Related

Multilingual HTML page with PHP - how to add titles to links

I implemented text switching system as portrayed here http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html .
Every link is defined like so:
<li><?php echo $lang['MENU_REGISTER']; ?></li>
By this logic, adding a title/description to a link would work like so:
<a title=<?php echo $lang['MENU_REGISTER']; ?> href="#">
<?php echo $lang['MENU_REGISTER']; ?></a>
I'm under impression that this does not work the way as I wish it would.
Any suggestions?
You are missing opening and closing quotes:
title="<?php echo $lang['MENU_REGISTER']; ?>"
and preferably addslashes for other quotes:
title="<?php echo addslashes($lang['MENU_REGISTER'];) ?>"

WP is_front_page and displaying images

I'm working on a new WordPress theme (haven't touched WP themes since like 2.8...) and I'm attempting to make an if/else statement that changes the behavior of my top navigation bar based on whether the page is a front_page or not.
Essentially what I'm after is if the page is a front_page, then display the logo image. If the page is any other kind of page, then display a simple H1 element with the company name:
<li class="name">
<?php
if (is_front_page()) {
echo '<a href="<?php bloginfo("url"); ?>';
echo '<img src="' .bloginfo( "template_directory" ). '/images/logo-dsi.png" alt="DSI" />';
echo '</a>';
} else {
echo '<a href="' .home_url(). '">';
echo '<h1 class="logo">DSI</h1>';
echo '</a>';
}
?>
</li>
The else statement is working fine. But my if statement is not producing the img tag correctly. It echoes the literal bloginfo('template_directory') url and not inserting that into the img src, which is what I want it to do. It's trying to find an image at this url: localhost/images/logo-dsi.png
Obviously my syntax on line 5 is wrong somewhere, but I'm also not sure if I'm going about this the right way. Is there a better solution to what I'm trying to accomplish here?
Thanks!
The later is correct however
echo '<a href="<?php bloginfo("url"); ?>';
Should be
echo '<a href="' . bloginfo("url") . '">';
I tried this out and it worked:
<li class="name">
<a href="<?php bloginfo('url'); ?>">
<?php
if (is_front_page()) { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/logo-dsi.png" alt="DSI" />
<?php
} else { ?>
<h1 class="logo">DSI</h1>
<?php } ?>
</a>
</li>
Apologies, I should have hacked away at it a little longer. But maybe this will become helpful to someone else.
I knew echos in WordPress looked bad for a reason... ;-)
Also I'm sure there's a prettier way to write this out, but for now, that code does the trick.

Avoid showing icons that don't contain a value

I've already been searching for a couple of hours for a solution. What I want to do is make icons that don't got a value (in the php) not show up.
Here's an image of what I currently have.
So for instance if only twitter and facebook got values, they should only appear on the screen.
Below is the code, and I hope someone got a solution for this.
Cheers!
<ul class="social-buttons">
<div class="wrapping">
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<a class="social" href="http://facebook.com/<?php echo $profile_data['facebook']; ?>" target="_blank"><li class="facebook"></li></a>
<a class="social" href="skype:<?php echo $profile_data['skype']; ?>?chat"><li class="skype"></li></a>
<a class="social" href="http://instagram.com/<?php echo $profile_data['instagram']; ?>"><li class="instagram"></li></a>
<a class="social" href="http://dribbble.com/<?php echo $profile_data['dribbble']; ?>"><li class="dribbble"></li></a>
<a class="social" href="http://linkedin.com/in/<?php echo $profile_data['linkedin']; ?>"><li class="linkedin"></li></a>
</div>
</ul>
You need to use the if statement with !empty(). The !empty() checks if a variable is NOT empty. Than proceed with the code. Like the example given here:
<?php
if(!empty($profile_data['twitter'])){
?>
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<?php
}
?>
If the variable is empty, it wont give the outputed code, in your case the <a> with the icon.
i think you can do like:
<ul class="social-buttons">
<div class="wrapping">
<?php if $profile_data['twitter'] {?>
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<?php } ?>
....
</ul>
Imo, a probably better way to do this is to perform a pre-processing of the data i.e. $profile_data, before you use it in your view so the view would no longer need to handle the processing logic. After which, the view can output your links by using a more concise construct e.g. for loop, that does not use any conditional branching.

How to assign a variable to a GET value in PHP

<img src="phone01.jpg">
When I write the code like this it's working fine. But when I edit this to assign a php variable to GET value, it's not working.
<a href="Details.php?phoneid=<?php $Pid_arr[count($Pid_arr)-1]?> " class="product" title="Product 1">
Can someone pleas explain why ?
Thanks in advance.
You need to echo the variable:
<a href="Details.php?phoneid=<?php echo $Pid_arr[count($Pid_arr)-1]; ?>" >
You can also do it this way:
<?=$Pid_arr[count($Pid_arr)-1]?>
You have to either echo it, like this:
<a href="Details.php?phoneid=<?php echo $Pid_arr[count($Pid_arr)-1]; ?> " class="product" title="Product 1">
Or use shorttags:
<a href="Details.php?phoneid=<?=$Pid_arr[count($Pid_arr)-1]?> " class="product" title="Product 1">
Be sure to echo it:
<a href="Details.php?phoneid=<?php echo $Pid_arr[count($Pid_arr)-1] ?> class="product" title="Product 1">
Try;
<a href="Details.php?phoneid=<?php echo $Pid_arr[count($Pid_arr)-1]; ?> " class="product" title="Product 1">
I would try writing this with short tags. I believe it gives better readability
<a href="Details.php?phoneid=<?=$Pid_arr[count($Pid_arr)-1]?>" class=.......

Is there a better way to write this php code?

return'
<li>
<img class="avatar" src="images/' . $picture . '" width="48" height="48" alt="avatar" />
<div class="tweetTxt">
<strong>' . $username . '</strong> '. autolink($tweet).'
<div class="date">'.relativeTime($dt).'</div><div class="date">'. $reply_info . '</div> <a class ="reply" href="home.php?replyto=#'. $username .'&status_id='. $id .'&reply_name=' .$username.'"> reply </a>
</div>
<div class="clear"></div>
</li>';
I was wondering if there is a cleaner way to write this code, and taking in mind processing time, if that really means anything.
p.s. this code is part of a function, this is the return statement!
Yes. Use double quotes for the PHP string (and single quotes for the HTML attributes), then you can just use PHP variables in the string, like so:
"<a href='nano.com/$username'>";
Is processing time really an issue? I doubt it, but profile to be sure.
Edit: If anyone is unsure about using single quotes in HTML attributes, have a look at this question. It's pretty unanimously agreed that single quotes are fine. If anyone can give a decent counter-argument I'd be happy to hear it.
You could use HEREDOC syntax :
$auto = autolink($tweet);
$rel = relativeTime($dt);
return <<<ENDOFRETURN
<li>
<img class="avatar" src="images/$picture" width="48" height="48" alt="avatar" />
<div class="tweetTxt">
<strong>$username</strong> $auto
<div class="date">$rel</div><div class="date">$reply_info</div> <a class ="reply" href="home.php?replyto=#$username&status_id=$id&reply_name=$username"> reply </a>
</div>
<div class="clear"></div>
</li>
ENDOFRETURN;
Cleaner template and php code -> use MVC
Yes, there is one, and you don't need MVC (only a template):
<li>
<a href="nano.com/<?=$username ?>">
<img class="avatar" src="images/<?=$picture ?>" width="48" height="48" alt="avatar" />
</a>
<div class="tweetTxt">
<strong><?=$username ?></strong>
<? echo autolink($tweet) ?>
<div class="date"><?=relativeTime($dt) ?></div>
<div class="date"><?=$reply_info ?></div>
<a class="reply" href="home.php?replyto=#<?=$username?>&status_id=<?=$id?>&reply_name=<?=$username?>">
reply
</a>
</div>
<div class="clear"></div>
</li>
Must read: http://wiki.yet-another-project.com/php/the_one_single_step_for_a_cleaner_code . It describes how you have to use the code above.
I would cut in several pieces and use sprintf() to tie it all together.
You can use a template engine i.e. smarty, twig, ...

Categories