PHP for a line break - php

I currently have the below code outputting a custom field with a title and image but I'm trying to add a line break. As I understand it the echo "/n"; at the end there should be doing the trick.
It's still outputting the data but with no line break - can anybody help out a rather hopeless case like myself :-)
<?php
$value = get_post_meta( get_the_ID(), 'ecpt_location2', true);
if( !empty($value)) {
echo '<h5>Location</h5>';
echo '<img src="/wp-content/uploads/2013/02/Address1.gif">';
echo $value;
echo "\n";
}
?>

Try nl2br.
echo nl2br($yourTextData);
hope this helps

Related

How to echo Wordpress default thumbnail via php inside php if else

Hey guys i have this code
<?php $jthumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ($jthumb> "0")
{
echo $jthumb;
}
else
{
echo "http://placehold.it/350x250&text= Jedcore";
}
?>);background-attachment:fixed;">
What i wanted to do is to replace the
http://placehold.it/350x250&text= Jedcore with an image inside my wordpress theme like
<?php bloginfo('template_url'); ?>/images/theimage.png
But i just cant replace it liek that i will prompt an error,
I'm no expert with php :)
try this:
echo '<img src="'. bloginfo('template_url') . '/images/img.jpg" alt="">';
try after this
change
echo "http://placehold.it/350x250&text= Jedcore";
to
echo get_bloginfo('template_url').'images/theimage.png';

Using php function as a link which is in an echoed function

i am trying to insert a link in an echoed line,
regular links work, but not this one, cant figure it out what's wrong
if($gallery_images != ''){
foreach ($gallery_images as $gallery_image){
$thumb = wp_get_attachment_image_src($gallery_image[SN.'gallery_post_image']['id'], 'post-thumb', false);
echo '<li><a <img src="'.$thumb[0].'" alt="'.$gallery_image[SN.'gallery_post_title'].'" /><p class="flex-caption">'.$gallery_image[SN.'gallery_post_title'].'</p></li>';
}
}
the_permalink() is a not a return function, it echoes the permalink. Replace it with get_permalink, which returns the permalink.
if($gallery_images != ''){
foreach ($gallery_images as $gallery_image){
$thumb = wp_get_attachment_image_src($gallery_image[SN.'gallery_post_image']['id'], 'post-thumb', false);
echo '<li><a <img src="'.$thumb[0].'" alt="'.$gallery_image[SN.'gallery_post_title'].'" /><p class="flex-caption">'.$gallery_image[SN.'gallery_post_title'].'</p></li>';
}
}
Your first problem is that. You don't include a function on a string that way.
echo '<li><a href="**<?php the_permalink(); ?>**">
Try this:
echo '<li><a href="'.the_permalink().'">
Then
.$gallery_image[SN.'gallery_post_title'].
You got a syntax error there.
SN.'gallery_post_title' // notice SN
It's fine if you define SN though.
Also, why do you have a close curly brace }?
Did you just copy and paste your code here sloppily or is that intentional? It's confusing if it is.

reading metadata from php file using php

After looking around for something like octopress in php and not finding anything, I decided to create something myself in php that would do the trick.
I'd like to start with writing some code in php that reads php files and can extract meta-data from them, so I can build an archive page of blog posts, etc.
I thought I could create yaml files, and include php/html in these files for the main content of the blog posts, but it's not clear to me if this is possible at all? Googling around for "use php in yaml" didn't really get me much further.
So I thought I'd ask here what the best approach would be for doing something like this.
Can anyone help?
Thanks
B
I am not familiar with yaml - can you simply use PHP's get meta tags?
<?php
// Assuming the above tags are at www.example.com
$tags = get_meta_tags('http://www.example.com/');
// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.
echo $tags['author']; // name
echo $tags['keywords']; // php documentation
echo $tags['description']; // a php manual
echo $tags['geo_position']; // 49.33;-86.59
var_dump($tags);// See any and all meta tags that have been picked up.
?>
Edit: I added the var_dump in so you can see all the tags you get. Test it out on the page you want to hit.
<?php
header('Content-Type:text/html; charset=utf-8');
$tags = get_meta_tags('http://www.narenji.ir');
var_dump($tags);
?>
Output is
array
'keywords' => string 'اخبار, تکنولوژی, نارنجی, گجت, فناوری, موبایل, خبر, تبلت, لپ تاپ, کامپیوتر, ربات, مانیتور, سه بعدی, تلویزیون' (length=186)
'description' => string 'مکانی برای آشنایی با ابزارها و اخبار داغ دنیای فناوری' (length=97)
Or you can use following code
<?php
$url = 'http://www.example.com/';
if (!$fp = fopen($url, 'r')) {
trigger_error("Unable to open URL ($url)", E_USER_ERROR);
}
$meta = stream_get_meta_data($fp);
print_r($meta);
fclose($fp);
?>
If your source file is image then you can try with it
<?php
echo "test1.jpg:<br />\n";
$exif = exif_read_data('tests/test1.jpg', 'IFD0');
echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";
$exif = exif_read_data('tests/test2.jpg', 0, true);
echo "test2.jpg:<br />\n";
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
echo "$key.$name: $val<br />\n";
}
}
?>

PHP function return. Cheats allowed

OK.
I have a very long and pretty complicated function.
It looks almost like this one:
<?php
function hello() {
echo 'My Function!' ?>
<ul>
<li> Blablabla </li>
<ul>
(...)
<?php } ?>
The HUGE problem here is that I'm UNABLE to echo anything.
My function HAVE to return it's contents instead of echoing or direct outputting (it has to be that way, it's a Wordpress shortcode and when I echo - the contents are getting displayed at the top of the page - ALWAYS, not in the place where I want them):
<?php
function hello() {
$output .= 'My Function!';
$output .= '<ul>';
$output .='<li> Blablabla </li>';
$output .='<ul>';
(...)
return $output;
} ?>
I hope it's easy till now.
Now, the real problems are:
I have tons of direct input code like:
?>
<div>
<span>
<p>Smth</p>
<a>smth</a>
</span>
</div>
<?php
Adding $output everywhere kills the nice paragraphs/whitespace and code is getting VERY HARD to read and understand (and all HTML elements are parts of variable now, so even my php editor is not treating them well and coloring them as PHP elements).
And another thing, I have tons of lines like this one:
<a href="<?php bloginfo('template_directory'); ?>/includes/php/timthumb.php?src=<?php echo $url; ?>&h=<?php if($items=="one") echo 320; elseif($items=="two") echo 230; elseif($items=="three") echo 180; elseif($items=="four") echo 130; ?>&w=<?php if($items=="one") echo 600; elseif($items=="two") echo 420; elseif($items=="three") echo 277; elseif($items=="four") echo 203; ?>" title="<?php the_title(); ?>" class="link">
(yes, this is a single line)
And I have absolutely no idea how to add such lines to $output.
$output .= '<a href="<?php bloginfo('template_directory'); ?>/includes/php/timthumb.php?src=<?php echo $url; ?>&h=<?php if($items=="one") echo 320; elseif($items=="two") echo 230; elseif($items=="three") echo 180; elseif($items=="four") echo 130; ?>&w=<?php if($items=="one") echo 600; elseif($items=="two") echo 420; elseif($items=="three") echo 277; elseif($items=="four") echo 203; ?>" title="<?php the_title(); ?>" class="link"> ';
Doesn't work of course (even with \'s before ' and ").
I believe there MUST be an easier way to attach all the code to return, but how?
I've tried with ob_start(); before code and return ob_get_clean(); after, but it outputs shortcode name instead of contents.
Its very hard to imagine what the problem you are trying to solve here is - although I'm not familiar with wordpress. Can't you just call the function where the output is supposed to go?
You could use output buffering - use echo/print as usual but...
ob_start();
hello();
$output=ob_get_contents();
ob_end_clean();
But that doesn't solve the problem that you still need to send to the browser at the right place in the page - and if you can do:
print $output;
in the right place, then you can surely do:
hello();
in the same place.
I agree with symcbean, but this might be a more practical approach at integrating with Wordpress: if you now have a single function called hello( ) which displays HTML, you might want to consider renaming that function to hello_content( ) (or something similar) and replace the hello( ) function with the suggestion symcbean gave you:
function hello_content( ) {
echo "foo";
}
function hello( ) {
ob_start( );
hello_content( );
return ob_get_clean( );
}
That should fix your immediate issue.
PHP Heredoc syntax will keep things looking neat and tidy and you can return the output to a variable. As long as you don't require any constants it will work fine.
You use it in this fashion:
function bar() {
$var = <<<EOV
anything here
anything there
EOV;
return $var;
}

Can't make PHP function work inside HTML inside PHP

I wrote this code, it gets an image from a link that varies according to where you are:
<img src='http://chusmix.com/Imagenes/grupos/<?php echo substr(get_search_query(), 1); ?>.jpg'>
I want to make that code run if a PHP condition proves true, but I cannot make it work. It seems that the function doesn't return a value instead it takes the link textually. I mean it goes to http://chusmix.com/Imagenes/grupos/.jpg literally. However the code works correctly by itself.
This is the PHP code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/<?php echo substr(get_search_query(), 1); ?>.jpg'>";
}
?>
You are already inside the php tag. So there is no need for <?php and ?>.
Try:
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr($search,1).".jpg'>";
Replace line
echo "<img src='http://chusmix.com/Imagenes/grupos/<?php echo substr(get_search_query(), 1); ?>.jpg'>";
with
echo "<img src='http://chusmix.com/Imagenes/grupos/" . substr(get_search_query(), 1) . ".jpg'>";

Categories