Displaying an image via acf in the theme header - php

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 . '';
?>

Related

wordpress php can't combine string with code

add_filter('wp_nav_menu_objects', 'my_wp_nav_menu_objects', 10, 2);
function my_wp_nav_menu_objects( $items, $args ) {
// loop
foreach( $items as &$item ) {
// vars
$image = get_field('menu_item_image', $item);
// append image
if( $image ) {
$item->title .= '<img class="ttl" src="' . <?php echo
$image['url']; ?> . '" alt="' . <?php echo $image['alt']; ?> . '" />';
}
}
// return
return $items;
}
What am I doing wrong? New to PHP and WordPress functions. The problem seems to be in the append image section.
replace this:
$item->title .= '<img class="ttl" src="' . <?php echo
$image['url']; ?> . '" alt="' . <?php echo $image['alt']; ?> . '" />';
for this
$item->title .= '<img class="ttl" src="' .$image['url']. '" alt="' . $image['alt'] . '" />';
you cannot use this <?php echo $image['alt']; ?> inside echo '...'
Reference: http://php.net/manual/en/function.echo.php

Conditional featured image with youtube thumbnail in Wordpress

Basically what I am trying to do here is: Display the youtube thumbnail. If no youtube thumbnail -> Display the Featured image for the post. If no featured image -> Display the fallback image.
However it seems I am doing something wrong because the webpage displays blank.
<?php
// Check if the post has a Youtube thumbnail (using custom field video_url)
if get_post_meta($post->ID,'video_url',true)
echo '<img src="http://img.youtube.com/vi/' . get_post_meta($post->ID,'video_url',true) . '/0.jpg"/>';
// Check if the post has a Post Thumbnail assigned to it
else ( has_post_thumbnail() ) {
echo '<a href="' . get_permalink($post->ID) . '" >';
the_post_thumbnail('frontpage-thumb');
echo '</a>';
// If the post does not have a featured image then display the fallback image
} else {
echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
?>
The general code for displaying the youtube thumbnail with custom fields is <img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/> I just can't get it to work with the conditional statements...
In your first if condition, you have wrong syntax and the PHP must be closed
if (get_post_meta($post->ID,'video_url',true)) //
Correct code
<?php
if (get_post_meta($post->ID,'video_url',true)) ?>
echo '<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/>';
// Check if the post has a Post Thumbnail assigned to it
else ( has_post_thumbnail() ) {
echo '<a href="' . get_permalink($post->ID) . '" >';
the_post_thumbnail('frontpage-thumb');
echo '</a>';
// If the post does not have a featured image then display the fallback image
} else {
echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
?>

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

Joomla menu: putting text BEFORE image

I'm building a website with a vertical drop-down menu. One of the menu items should contain an image and the menu title.
Joomla puts the image before the text, in the following way:
<li class="item-153 current active"><a class="menu_immagine" href="whatever" ><img src="whatever.png" alt="whatever" /><span class="image-title">whatever</span></a></li>
What I'd like to do is putting the text before the image, like this:
<li class="item-153 current active"><span class="image-title">whatever</span><a class="menu_immagine" href="whatever" ><img src="whatever.png" alt="whatever" /></a></li>
How can I do this in Joomla?
Thank you very much for your help...
Thanks to Lodder, I made a template override. Then I modified the file myTemplate/html/mod_menu/default_component.php.
I changed this
if ($item->menu_image)
{
$item->params->get('menu_text', 1) ?
$linktype = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" /> <span class="image-title">' . $item->title . '</span> ':
$linktype = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" />';
}
else {
$linktype = $item->title;
}
to this:
if ($item->menu_image)
{
$item->params->get('menu_text', 1) ?
$linktype = '<span class="image-title">' . $item->title . '</span> <img src="' . $item->menu_image . '" alt="' . $item->title . '" /> ':
$linktype = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" />';
}
else
{
$linktype = $item->title;
}
That's it! That was so easy, after all, that's a matter of knowing how to do it! ;)

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