Output text/link with variables inside - php

How can i make this output properly? Im using the right variables but i think im doing something wrong with quotations or what not.
<?php echo '<img src="' SITE_URL . 'img/' . $code '" title="Created by website.com" />' ?>

You need to join all the pieces of the string with ., the concatenation operator. You're missing several.
<?php echo '<img src="' . SITE_URL . 'img/' . $code . '" title="Created by website.com" />' ?>
echo also accepts multiple arguments, so you may use commas instead of periods:
<?php echo '<img src="', SITE_URL, 'img/', $code, '" title="Created by website.com" />' ?>

<?php echo '<img src="'. SITE_URL . 'img/' . $code .'" title="Created by website.com" />' ?>

You can do it multiple ways, inline with the HTML
<img src="<?= $SITE_URL ?>" title="..."/>
Or by echoing the whole string in PHP
<?php
echo '<image src="' . $_SITE_URL . '" title="..."/>';
?>
If you do it in PHP, you need to make sure you use quotations correctly, escape characters as needed, and join all strings with a .

Related

Displaying an image via acf in the theme header

I am using AFC Pro and created an options pafe so tjhat the user can upload a new header logo whenever he wants to.
The ACF field is called "headerlogo".
What I want now is that the Logo gets replaced by my theme automatically.
My Variables are:
$headerlogo = wp_get_attachment_image_src(get_field('headerlogo', 'option'), 'full');
$default_logo = '<img src="'echo .$headerlogo[0].'" alt="SITE Logo">';
they get called in:
echo '<a href="'. esc_url( home_url( '/' ) ) .'">
' . $default_logo . '
</a>';
But the Output is:
<a href="http://www.xxx.de/">
<img src="" alt="SITELogo">
</a>
What am I doing wrong here?
Thank in advance.
This should work:
<?php
$headerlogo = get_field('headerlogo');
if( !empty($headerlogo) ):
$default_logo = '<img src="'. $headerlogo['url'] . '" alt="' . $headerlogo['alt'] . '" />';
endif;
echo '' . $default_logo . '';
?>

echo html and wordpress php variable in string

I am trying to echo html and php in a string but I get a syntax error with the code below.
$post_title .= '
<div id="show_neil" class="box five columns" data-target="#member_neil">'.
'<h4 class="name">'.
'<img src="' . get_bloginfo('template_directory') . '/img/team/neil.png">'.
.get_the_title($post_id).
'</h4>'.
'</div>
';
How do I echo this?
Try to use get_bloginfo('template_directory') instead of bloginfo('template_directory').

Apostrophes in php code

Can I have the following code:
foreach ( $categories as $category ) {
echo '<div class="wwd">
<img src="/images/' . $category->name . $category->term_id . '.jpg" alt="' . $categories->category_name . '" />
<div class="wwd-title">' . $category->name . '</div><br/>
</div>';
}
I am trying to add this code before /images/
<?php bloginfo('template_directory'); ?>
At present the code generates the img src="/images/..." but I want the sites url to go before the /images, when I have tried it I cant seem to get it to either work or display the correct path. The problem seems to with the apostrophe types used but I cant seem to figure out what should go where.
Thanks
Paul
the wordpress bloginfo function outputs the data to the browser. Instead use get_template_directory_uri to save the result to a variable:
$templateurl = get_template_directory_uri();
foreach ( $categories as $category ) {
echo '<div class="wwd">
<img src="' . $templateurl . '/images/' . $category->name . $category->term_id . '.jpg" alt="' . $categories->category_name . '" />
<div class="wwd-title">' . $category->name . '</div><br/>
</div>';
}
bloginfo will try to echo out the value of the parameter specified so you should try another method of just getting the information you want it. Say for example bloginfo returned your template directory you could do this:
foreach ( $categories as $category ) {
echo '<div class="wwd">
<img src="' . bloginfo('template_directory') . '/images/' . $category->name . $category->term_id . '.jpg" alt="' . $categories->category_name . '" />
<div class="wwd-title">' . $category->name . '</div><br/>
</div>';
}
The get_template_directory function might be more appropriate: http://codex.wordpress.org/Function_Reference/get_template_directory

adding a php custom field into a php code

I'm using a PHP code on my wesbiste to get thumbnails of vimeo videos.
here is the code :
<?php
echo '<img src="' . get_vimeo_thumb('43096888', 'thumbnail_large') . '" class="image_page_texte_et_medias" style="cursor:pointer" >';
?>
I'm using a ACF custom field with the video ID
<?php the_sub_field('vimeo'); ?>
What I'm trying to do is to replace the ID in my first php code ('43096888') by :
<?php the_sub_field('vimeo'); ?>
can anybody help me with this,
thanks a lot,
aren't you allowed to use variables as arguments? something like:
<?php
$foo = the_sub_field('vimeo');
echo '<img src="' . get_vimeo_thumb($foo, 'thumbnail_large') . '" class="image_page_texte_et_medias" style="cursor:pointer" >';
?>
<?php
echo '<img src="' . get_vimeo_thumb(the_sub_field('vimeo'), 'thumbnail_large') . '" class="image_page_texte_et_medias" style="cursor:pointer" >';
?>
<?php
echo '<img src="' . get_vimeo_thumb(the_sub_field('vimeo'), 'thumbnail_large') . '" class="image_page_texte_et_medias" style="cursor:pointer" >';
?>
You can simply replace the string '43096888' with your function the_sub_field('vimeo') if that's something what you want.

conditional logic in the header.php

So I am using Wordpress and I have to have a specific logo on a specific page. From research I have to use conditional logic to swap the existing logo with another depending on the current page. Everything I have tried seems to just break the theme.. Any help on guiding me in the correct direction? So basically every page except page_id=79 would have the same logo in the header.
<a id="logo" href="<?php echo home_url(); ?>">
<?php
if(!empty($options['use-logo'])) {
$default_logo_id = (!empty($options['retina-logo'])) ? 'id="default-logo"' : null;
echo '<img '.$default_logo_id.' alt="'. get_bloginfo('name') .'" src="' . $options['logo'] . '" />';
if(!empty($options['retina-logo'])) echo '<img id="retina-logo" alt="'. get_bloginfo('name') .'" src="' . $options['retina-logo'] . '" />';
} else { echo get_bloginfo('name'); }
?>
</a>
<?php if ( is_page(79) ) { ?>
What to displayed on page 79.
<?php } else { ?>
What will be displayed everywhere else.
<?php } ?>
This should work.
Try using get_queried_object_id();
<a id="logo" href="<?php echo home_url(); ?>">
<?php
if(!empty($options['use-logo']) && get_queried_object_id() != 79) {
$default_logo_id = (!empty($options['retina-logo'])) ? 'id="default-logo"' : null;
echo '<img '.$default_logo_id.' alt="'. get_bloginfo('name') .'" src="' . $options['logo'] . '" />';
if(!empty($options['retina-logo'])) echo '<img id="retina-logo" alt="'. get_bloginfo('name') .'" src="' . $options['retina-logo'] . '" />';
} else { echo get_bloginfo('name'); }
?>
</a>
The url of your logo image is contained within $options['logo']. You should be able to modify this in the admin section of your WordPress installation (try looking in "Appearance -> Header").

Categories