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.
Related
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;
}
I'm trying to wrap my head around the wp_add_inline_style function in WordPress.
I've managed to output my customiser CSS using this function and it works perfectly.
Here's how I am using the wp_add_inline_style function to output the customiser styles;
function theme_custom_css() {
$header_height = get_theme_mod( 'header_height', '96' );
$output .= '.site-header { height: ' . esc_attr( $header_height ) . 'px; }';
if ( ! empty( $output ) ) {
wp_add_inline_style( 'custom-style', $output );
}
}
add_action( 'wp_enqueue_scripts', 'theme_custom_css' );
This works exactly as expected. It adds a style tag to the head below the style.css file with all the custom css inside. Great, that's exactly what I wanted.
However, if I have another file where there is CSS that can be changed by the user and I want to add the changed CSS to the style tag printed by the wp_add_inline_style function. This doesn't work. Here's the code where I'm attempting this.
function theme_slider_css() {
if ( get_field( 'slider_padding_top' ) ) {
$slider_css .= '.slider-inner-wrap { padding-top: ' . get_field( 'slider_padding_top' ) . ';px }';
}
if ( ! empty( $slider_css ) ) {
wp_add_inline_style( 'custom-style', $slider_css );
}
}
add_action( 'wp_enqueue_scripts', 'theme_slider_css' );
The theme_slider_css function is being completely ignored.
Am I completely missing the point here? Is there anything obvious I am missing?
I would be happy to have two separate style tags printed in the head but I cannot achieve that either?
Hopefully I've explained this well, if not, please ask.
Any help will be appreciated.
Thank you in advance.
The last ' is missing.
Change it from:
"$output .= '.site-header { height: ' . esc_attr( $header_height ) . 'px; };"
to:
"$output .= '.site-header { height: ' . esc_attr( $header_height ) . 'px; }';"
I want to add a image to the left of the site-title: mysimpledevotion.com
How can I do that?
Would it be easier to make an image of both the text and pic, rather than trying to just add a small pic (icon) next to the text? I'm not talking about the favicon but rather the site-title MY SIMPLE DEVOTION on the front page.
I found two options to achieve this.
1. Using CSS before to inject the image
The major problem with this approach is that you cannot use relative paths; i.e, the image url must be absolute, otherwise it will not work consistently on all pages of the website.
To do so either add the following CSS to the "Additional CSS" part of your theme (when customizing it), or add it in the style.css of your custom child-theme (and make sure to add the style.css using the functions.php correctly).
.site-title a:before{
content:"";
background-image:url('http://path/to/my/image.jpg');
display: inline-block;
background-size: 1em 1em;
width: 1em;
height: 1em;
}
This will make the image correspond to the text size as well, and you can add additional CSS properties such as border-radius: 50%.
2. Using theme's filters to inject image
This approach heavily relies on the used theme to define appropriate filters where you could inject the image HTML. Here is an example of injecting an image to the Astra theme using a custom child-theme.
functions.php:
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_uri() );
}
if( ! ( function_exists( 'wp_get_attachment_by_post_name' ) ) ) {
function wp_get_attachment_by_post_name( $post_name ) {
$args = array(
'posts_per_page' => 1,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'name' => trim( $post_name ),
);
$get_attachment = new WP_Query( $args );
if ( ! $get_attachment || ! isset( $get_attachment->posts, $get_attachment->posts[0] ) ) {
return false;
}
return $get_attachment->posts[0]->guid;
}
}
add_filter( 'astra_site_title', 'add_image_to_site_title' );
function add_image_to_site_title( $original_title ) {
$image = wp_get_attachment_by_post_name( 'my-image-post-name' );
$html = '<img src="' . $image . '" alt="" class="title-image">';
return $html . $original_title;
}
?>
style.css:
/*
Theme Name: My Astra
Template: astra
*/
.title-image {
display: inline-block;
width: 1em;
height: 1em;
}
I hope this helps :)
That is called favicon and can be added, if you are using theme which has that option included then can be checked by Theme Options within your wordpress admin (check on the left side). If it's isn't there then check Appearance >> Customize.
Even you cannot find there then check theme documentation. Try avoiding adding any additional code, it might conflict with existing.
Even it's not in theme documentation then do following steps to add it
Go to Appearance >> Editor
Click on header.php
Add the following code between <head></head>
<link rel="shortcut icon" type="image/x-icon" href="your-image-link" />
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'] . '" />';
}
}
all. This question probably has a devilishly simple answer but it has kept me occupied for several hours.
I have my main menu and it's corresponding block in a Drupal site I am building. Like all other Drupal menus it contains a bunch of links to various parts of the site. I can assign it's block to a region and the menu links come out all nice and formatted with a title thing and little bullet points. The problem though is that I am making a custom theme for this website and I need to be able to work with the links without all the cruft added, preferably in something simple like an ul.
Is there any function that takes a menu and produces an ul containing all the links?
Maybe there is some way you can reduce the menu's block to just an ul.
I have been experimenting with theme_menu_tree(...) and theme(...) to no avail.
Thank you!
I find you can do most changes through CSS such as setting <H2> titles to display: none and setting <LI> tags to float: left for a horizontal navbar.
But... if you want to build your own menu from the Drupal data, here is some code from a site I'm working on. It builds a two-level menu. I'm sure you could simplify this code even further if you need.
//----------- primary menu (horizontal with drop-downs) -------------------------
$params = array('max_depth' => 3);
$menu = menu_build_tree('main-menu', $params);
$variables['menu'] = $menu;
$html = '<ul>';
foreach($menu as $item_menu) { //for each main element
$isSecondLevel = isset($item_menu['below']) && !empty($item_menu['below']);
if ($isSecondLevel) {
$html.= '<li>';
} else {
$html.= '<li class="sg">';
}
$html.= '<a class="topLevel" href="'.url($item_menu['link']['link_path']).'">';
$html .= $item_menu['link']['link_title'];
$html .= '</a>';
//is there any sub elements to display
if ($isSecondLevel) {
$html.= '<ul>';
foreach($item_menu['below'] as $item_submenu) { //for each sub element
$isThirdLevel=isset($item_submenu['below']) && ! empty($item_submenu['below']) ? 'main-menu-third_level' : '';
$html.= '<li>';
$html.= '<a href="'.url($item_submenu['link']['link_path']).'">';
$html.= $item_submenu['link']['link_title'];
$html.= '</a>';
$html.= '</li>';
}
$html.= '</ul>';
}
$html.= '</li>';
}
$html.= '</ul>';
$variables['main_menu_html'] = $html;
This code was placed inside function pinkribbon_process_page(&$variables) in template.php. Menu is printed in the template by calling <?php echo $main_menu_html ?>
Simon.
P.S. Others, please feel free to edit this code for clarity/simplicity.
I advice you to use
menu_tree_output
like this:
print render(menu_tree_output(menu_build_tree('main-menu', $parameters)));
You could call menu_build_tree and look at it's output and build a ul from it. However, despite the default menu output having loads of "cruft" it is a ul and should be themeable with CSS.
If you really want to build the menu yourself, I would reverse engineer another module that does so like Nice Menus