Wordpress default logo - wordpress

I am working a new Wordpress theme.
The theme will have the option to set a logo in the customizer as normal.
This is working.
However, I want the theme to automatically set a DEFAULT logo.
If the user doesn't define a logo in the customizer, then the default logo will display automatically.
I have an image called logo.jpg in a folder call img in the theme root.
I am using this code to set the default logo but it is not working:
add_filter('get_custom_logo',function($html){
if(empty($html)) {
$html = '<img src = get_template_uri() . "/img/logo.jpg" >';
}
return $html;
});
Any ideas? Is it my syntax with a mix of " and '?
Thanks!

You had a mistake in the string concatenation.
P.S. Please, don't use anonymous functions in filters. Because it's not possible to remove or change this filter in child-theme for example.
add_filter( 'get_custom_logo', 'apply_default_logo' );
function apply_default_logo( $html ){
if( empty( $html ) ) {
$html = '<img src="' . get_template_directory_uri() . '"/img/logo.jpg">';
}
return $html;
}

Related

WordPress RSS list add featured image link

I want wordpress post rss list featured image link
Ex:
<title>Test title</title>
<desc>Test desc</desc>
<image>imagelink</image>
I'm not using plugin.
Thanks
This will do. Add it to functions.php file:
// add the namespace to the RSS opening element
add_action( 'rss2_ns', 'custom_prefix_add_media_namespace' );
function custom_prefix_add_media_namespace() {
echo "xmlns:media="http://search.yahoo.com/mrss/"n";
}
// add the requisite tag where a thumbnail exists
add_action( 'rss2_item', 'custom_prefix_add_media_thumbnail' );
function custom_prefix_add_media_thumbnail() {
global $post;
if( has_post_thumbnail( $post->ID )) {
$thumb_ID = get_post_thumbnail_id( $post->ID );
$details = wp_get_attachment_image_src($thumb_ID, 'thumbnail');
if( is_array($details) ) {
echo '<media:thumbnail url="' . $details[0] . '" width="' . $details[1] . '" height="' . $details[2] . '" />';
}
}
}
Also when you add this, be sure to reset the permalinks (flush them), by going to Settings > Permalinks, and either change permalinks to default, and back to your defined settings, or just re-saving.
After that, check your feed and the media should be there.

Trying to use ACF Options Page in Genesis to output a Image Logo

I have created an options page in ACF Pro and Have added a image field called company logo. Now I want to remove the genesis site title. I have genesis removing the site title but when i add in the ACF call it does not work. If someone can take a look at what I have and give me some advice that would be great.
remove_action( 'genesis_site_title', 'genesis_seo_site_title' );
add_action( 'genesis_site_title', 'child_seo_site_title' );
/* Then add logo to header. */
function child_seo_site_title() {
$logo = get_field('company_logo', 'option');
if($logo) {
$output .= "<img src='". $logo['url']."' alt='". $logo['alt'] ."' />";
}
}
Should be simple but can seem to get it to work.
You're appending the image to an undefined variable, $output, and then doing nothing with it.
Output the image instead:
function child_seo_site_title() {
$logo = get_field( 'company_logo', 'option' );
if ( $logo ) {
echo '<img src="' . $logo['url'] . '" alt="' . $logo['alt'] . '" />';
}
}

how to make images in wordpress theme responsive using Bootstrap?

I already have a wordpress website with many images in posts. I'm creating a new theme using bootstrap but it seems like the images are crossing the container area.
Bootstrap provides a built-in class :
class="img-responsive"
but this code needs to be inserted in all the <img> tags which is a lot of work.
What is the other way to make the images responsive?
Also, this code doesn't work -
img
{
border: 0 none;
max-width: 100%;
vertical-align: middle;
}
It just squeezes the image inwards.
There is a gist showing how to do this at https://gist.github.com/mkdizajn/7352469
The technique is to add the following to your functions.php (from gist):
function bootstrap_responsive_images( $html ){
$classes = 'img-responsive'; // separated by spaces, e.g. 'img image-link'
// check if there are already classes assigned to the anchor
if ( preg_match('/<img.*? class="/', $html) ) {
$html = preg_replace('/(<img.*? class=".*?)(".*?\/>)/', '$1 ' . $classes . ' $2', $html);
} else {
$html = preg_replace('/(<img.*?)(\/>)/', '$1 class="' . $classes . '" $2', $html);
}
// remove dimensions from images,, does not need it!
$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
return $html;
}
add_filter( 'the_content','bootstrap_responsive_images',10 );
add_filter( 'post_thumbnail_html', 'bootstrap_responsive_images', 10 );
You should consider creating a child theme to make this modification if you are using a stock theme.

Inserting variable into header

I have setup a custom template and functions.php such that which users can insert an image url. The is for this image to be displayed in the header.
In the custom template, I use an add_action to place the image into the header. My problem is that whilst I can supply a url directly in the custom template with this image showing up, the same url inserted as a variable does not work.
So, the following extract from my custom template works (where $featimage is set to the url of the image)
$page_id = $wp_query->get_queried_object_id();
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
function img(){
$featimage="http://etc.jpg";
return $featimage;
}
function memberland_featured_image() {
$catchkathmandu_featured_image = '<div id="header-featured-image">';
$catchkathmandu_featured_image .= '<img id="main-feat-img" class="wp-post-image" alt="" src="'.img().'" />';
$catchkathmandu_featured_image .= '</div><!-- #header-featured-image -->';
echo $catchkathmandu_featured_image;
}
add_action( 'catchkathmandu_after_hgroup_wrap', 'memberland_featured_image', 10 );
get_header();
but here where $featimgage is set to the variable from the metabox it does not work.
$page_id = $wp_query->get_queried_object_id();
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
function img(){
$featimage=$memoptions['memberland_ftd_hder_image'];
return $featimage;
}
function memberland_featured_image() {
$catchkathmandu_featured_image = '<div id="header-featured-image">';
$catchkathmandu_featured_image .= '<img id="main-feat-img" class="wp-post-image" alt="" src="'.img().'" />';
$catchkathmandu_featured_image .= '</div><!-- #header-featured-image -->';
echo $catchkathmandu_featured_image;
}
add_action( 'catchkathmandu_after_hgroup_wrap', 'memberland_featured_image', 10 );
get_header();
p.s. I am using the function img() as in this post as without it, inserting $featimage directly into the src, even if pre-defined as the url, did not work.
I know that the variable options from them the metaboxes are working as images etc which I want displaying under the header all function.
Is this something to do with the order of wordpress functions? Thanks!
the img() function does not know about $memoptions variable as the variable is defined outside the scope of the img() function.
you can define $memoptions in img() function as below:
function img(){
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
$featimage=$memoptions['memberland_ftd_hder_image'];
return $featimage;
}

Trying to insert a linked featured image into the content

I am using filter hook to insert a featured image into the_content, that works, but when I try to wrap the featured image in an anchor tag, the link tag ends up directly after the content(not wrapping the image at all)
Is there something I am missing as far as understanding how to filter the_content()? Here's my code:
add_filter('the_content', 'add_img_to_ps_archive');
function add_img_to_ps_archive($content) {
if (is_post_type_archive('past_symposia') ) {
echo $content . '<a href ="#" "alignleft">' . the_post_thumbnail('symposia-thumb') .
'</a>';
} elseif( is_singular('past_symposia') ) {
echo $content . '<br />';
} else {
return $content;
}
}
Try to use commas ',' and not dots '.' for concatenating - donĀ“t ask me why, but wordpress does that sometimes...
This happened because the_post_thumbnail() outputs the image tag directly to the output buffer. You need to use get_the_post_thumbnail() to return the image tag so you can concatenate it with $content.

Resources