I created a header widget. I used this code:
First, I placed this code into Functions.php
function wpb_widgets_init() {
register_sidebar( array(
'name' => 'Header Widget',
'id' => 'header-widget',
'before_widget' => '<div class="hw-widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="hw-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'wpb_widgets_init' );
Then I added this code into header.php
<?php
if ( is_active_sidebar( 'header-widget' ) ) : ?>
<div id="header-widget-area" class="hw-widget widget-area" role="complementary">
<?php dynamic_sidebar( 'header-widget' ); ?>
</div>
<?php endif; ?>
Now my width of the widget seems to be smaller than my container width. I'd like to change that.
I tried using this code:
.header-widget {
clear: both;
margin: 0 auto;
width: 1200px;
}
But it does not seem to do anything. What am I doing wrong?
There is no class named header-widget in front end. header-widget is an ID of widget in backend. If you want to write CSS for your widget in front end then you can use header-widget-area or hw-widget widget-area as below. I tried and it worked for me in localhost.
#header-widget-area {
clear: both;
margin: 0 auto;
width: 1200px;
}
or
.header-widget {
clear: both;
margin: 0 auto;
width: 1200px;
}
Related
I'm building a custom WP Theme and am working on adding to The Customizer. I previously added a background image setting, and am trying to also implement an opacity slider. I looked up how to use the :before or :after pseudoclass to make opacity apply correctly to my background-image, but the children are still affected by the opacity setting. Is there something wrong with my code? I went off this site's example on how to format my html and css: https://scotch.io/quick-tips/how-to-change-a-css-background-images-opacity
HTML:
<div id="bg-image-div" class="image-opacity">
<section id="category-section">
<div class="container">
<div class="row">
<div class="col-xs-10 col-xs-offset-1">
<h2 class="section-title">Section Title</h2>
</div>
</div>
<div>
<p>
Content Content Content Content Content Content Content Content Content Content Content
</p>
</div>
</div> <!-- /.container -->
</section>
</div> <!-- /.image-opacity -->
CSS:
#bg-image-div {
position: relative;
overflow: hidden;
background: #5C97FF;
}
#bg-image-div:after {
content: ' ';
display: block;
position: absolute;
left: 0;
top: 0;
z-index: 1;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-position: 50% 0;
-ms-background-size: cover;
-o-background-size: cover;
-moz-background-size: cover;
-webkit-background-size: cover;
background-size: cover;
}
#category-section {
z-index: 2;
position: relative;
}
PHP adding the Control and Settings:
function themename_customizer_register( $wp_customize ) {
// Theme Customizer -- Background Image CSS
$wp_customize->add_section(
'themename_recipe_category_area',
array(
'title' => __( 'Category Area Image', 'themename' ),
'priority' => 30,
'capability' => 'edit_theme_options',
'description' => __( 'Change the background image in the Recipe Category Area of the Home Page', 'themename' ),
)
);
$wp_customize->add_setting(
'themename_background_image',
array(
'default' => get_template_directory_uri() . '/images/rustic-bg.jpg',
'transport' => 'refresh'
)
);
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
'themename_background_image',
array(
'label' => __( 'Background Image', 'themename' ),
'settings' => 'themename_background_image',
'section' => 'themename_recipe_category_area',
'description' => __( 'Recommended image size is approximately 1650x1650 pixels', 'themename' ),
)
)
);
// Theme Customizer -- Background Image CSS Opacity Slider
$wp_customize->add_setting('themename_image_opacity');
$wp_customize->add_control( 'themename_image_opacity',
array(
'type' => 'range',
'priority' => 20,
'section' => 'themename_recipe_category_area',
'label' => __( 'Set Background Image Opacity', 'themename' ),
'description' => '<span style="float: left; margin-top: 10px;">' . __( '← Less', 'themename' ) . '</span><span style="float: right; margin-top: 10px;">' . __( 'More →', 'themename' ) . '</span>',
'input_attrs' => array(
'min' => .1,
'max' => 1,
'step' => .1,
'class' => 'image-opacity',
'style' => 'width: 100%; padding-top: 0; z-index: 0;',
),
)
);
}
add_action( 'customize_register', 'recipedia_customizer_register' );
PHP/CSS hooking the style into wp_head:
function themename_customizer_css() {
?>
<style>
/*== Style from The Customizer Colors Section - Categories Section Background Image ==*/
<?php if(get_theme_mod('themename_recipe_category_area')) : ?>
#bg-image-div {
background-image: url("<?php echo get_theme_mod( 'recipedia_recipe_category_area' ) ?>");
}
<?php else : ?>#bg-image-div {
background-image: url("<?php echo get_template_directory_uri() . '/images/rustic-bg.jpg'; ?>");
}
<?php endif;
?>
/*== Style from The Theme Customizer Background Image - Adjusting Opacity ==*/
.image-opacity {
opacity: <?php echo get_theme_mod('themename_image_opacity') ?>;
}
</style>
<?php
}
add_action( 'wp_head', 'recipedia_customizer_css' );
Your CSS output for image opacity in the wp_head action hook looks incorrect. It should be
.image-opacity:after { ... }
Although, to match your original style, it's better to just go with
#bg-image-div:after { ... }
for the opacity properties so that there's no confusion between usage of CSS ID or class selectors if you have to edit this later.
I've actually utilized this very technique -- from that same article source -- and it works perfectly.
I have this code that retrieves all images for the active page:
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
I need some help to open the images in a lightbox with the post/page title and navigation such as this https://simplelightbox.com/ one.
I am just learning the code so I desperately need some help.
I tried using fancy lightbox plugin but that did not work as the lightbox did not launch. For now I am hiding and showing the images using javascript.
From your code, I've created the following shortcode:
function so_all_images_lightbox() {
$output = '';
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
$output .= '<a class="lightboximage" href="'.wp_get_attachment_url($attachment_id).'">';
$output .= wp_get_attachment_image($attachment_id, 'thumbnail');
$output .= '</a>';
}
return $output;
}
add_shortcode('all_images_lightbox','so_all_images_lightbox');
Add [all_images_lightbox] to the site you want the gallery to be on. And it will display the images with a link to the image file.
Then you'll also need a lightbox-container somewhere in your documents (preferably short before the footer to avoid stacking problems):
function so_output_lightbox_container() { ?>
<div id="imagelightbox" class="image-lightbox">
<a id="close-lightbox" title="Close Lightbox" class="close-lightbox"></a>
<div id="imagecontainer"></div>
</div>
<?php }
add_action('get_footer', 'so_output_lightbox_container');
and the styling for it (you could also put this in the customizer css or your child theme css)
function so_output_lightbox_container_css() { ?>
<style>
.image-lightbox {
display: none;
z-index: -1;
background-color: rgba(0,0,0,.90);
width: 0;
height: 0;
position: fixed;
top: 0;
left: 0;
}
.image-lightbox.active {
display: block;
z-index: 99;
width: 100%;
height: 100%;
}
#imagecontainer {
display: flex;
align-items: center;
justify-content: center; height: 100vh;
position: relative;
}
</style>
<?php }
add_action('wp_head', 'so_output_lightbox_container_css');
Finally, you'd need some javascript to load the according images to the lightbox instead of follwing the link:
function so_output_lightbox_container_js() { ?>
<script>
( function() {
lightboxInit = function () {
const lightboximage = document.getElementsByClassName('lightboximage');
const imagelightbox = document.getElementById('imagelightbox');
const imagecontainer = document.getElementById('imagecontainer');
const lightboxbclose = document.getElementById('close-lightbox');
Array.prototype.forEach.call(lightboximage, function (el) {
el.addEventListener('click', function () {
event.preventDefault();
let imgfile = this.getAttribute("href");
imagelightbox.classList.add("active");
imagecontainer.innerHTML = '<img alt="Click to close" src="' + imgfile + '">';
});
});
lightboxbclose.addEventListener('click', function () {
imagelightbox.classList.remove("active");
imagecontainer.style.height = '';
imagecontainer.style.paddingBottom = '';
});
imagelightbox.addEventListener('click', function () {
imagelightbox.classList.remove("active");
});
}
lightboxInit();
} )();
</script>
<?php }
add_action('wp_footer', 'so_output_lightbox_container_js');
Tested by quickly throwing all of it in my functions.php and adding the shortcode to a site: Works. Click anywhere to close the lightbox. The styling of the additional close-button, I'll leave to you ;-)
Update/Edit:
To run the function from a template, just call the function within the template instead of running the shortcode. Add this to the template where needed:
echo so_output_lightbox_container();
You can then remove the line
add_shortcode('all_images_lightbox','so_all_images_lightbox');
of the first code block then, or leave it in place in case you may need the shortcode as well later.
Update 2:
To use an icon as a link, try replacing the first block with:
function so_all_images_lightbox() {
$output = '';
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
$output .= '<span class="imgbx"><a class="lightboximage" href="'.wp_get_attachment_url($attachment_id).'">';
$output .= '<i class="fas fa-camera"></i>';
$output .= '</a>';
$output .= wp_get_attachment_image($attachment_id, 'thumbnail').'</span>';
}
return $output;
}
add_shortcode('all_images_lightbox','so_all_images_lightbox');
The default formatting puts the date after the title, I want the date to be displayed first and then the title, I also want to be able to separate them a little bit and make them look nice and lined up with a left justification.
The code is set to display a list of all the subpages of page.
<?php
wp_list_pages( array(
'title_li' => '',
'child_of' => 701,
'show_date' => '1',
) ); ?>
I could not find option to modify it from PHP. So, here is small CSS tweak so that you can get date in the front. Check this:
<style>
#page-list li{
float: left;
display: inline-block;
clear: both;
}
#page-list li a{
float: right;
margin-left: 10px;
}
</style>
<ul id="page-list">
<?php
wp_list_pages( array(
'title_li' => '',
'child_of' => 701,
'show_date' => '1',
) ); ?>
</ul><!-- #page-list -->
I want to have two widgets on my home page next to each other. However I want the first widget to be 650 px, while the other to be 290px. This I am not being able to do.Can someone please help me?
The code I registered for my widgets in my theme functions.php file:
register_sidebar(array(
'name' => 'Slider 1',
'before_widget' => '<div id="%1$s" class="widget-area1 %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
register_sidebar(array(
'name' => 'Side Column',
'before_widget' => '<div id="%1$s" class="widget-area2 %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
Then I placed in my home page template file:
<?php
/**
* Template Name: Home Page
*/
get_header();
include('_functions/get-options.php');
?>
<?php /* Widgetised Area */ if(!function_exists('dynamic_sidebar') ||!dynamic_sidebar('Homepage')); ?>
**<div class="widget-area1">
<?php /* Widgetised Area */ if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('Slider 1')); ?>
</div>
<?php /* Widgetised Area */ if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('Side Column')); ?>**
<?php /* Widgetised Area */ if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage 1')); ?>
<?php /* Widgetised Area */ if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage 2')); ?>
<div class="clear"></div>
<?php get_footer(); ?>
And the css styling I placed in the stylesheet:
div.widget-area1 {
clear: both;
margin: 0 auto;
width: 650px;
}
div.widget-area3 {
clear: both;
margin: 0 auto;
width: 290px;
}
Can you please tell me what I am doing wrong?The theme that I am using is Compare if that might help.
The styles in your question like like they should work, but this is what I see in your stylesheet
#widget-area1 {
clear: both;
margin: 0 auto;
width: 650px;
}
#widget-area2 {
clear: both;
margin: 0 auto;
width: 290px;
}
These rules are in reference to an ID not to a class like you have defined in your question.
Also, you may want to remove the clear:both if you want them to sit side-by-side.
i'm trying to display my portfolio items in a gird, but can't quite figure it out. ideally, i'd like the thumbnails to display 3 or 4 across(with the rest forming a second and/or third line) and once the thumbnails are clicked on, i'd like it to go to my normal post page.
css-
/* Portfolio */
#content {
width: 603px;
}
#portfolio-item {
display: inline;
float: left;
margin: 0 0 0 60px;
width: 280px;
}
#portfolio-item h1 {
font-size: 16px;
margin: 20px 0 5px 0;
}
#portfolio-item img {
height: 50%;
width: 50%;
}
portfolio.php:
<?php
/*
Template Name: Portfolio
*/
?>
<?php get_header(); ?>
<div id="portfolio">
<?php
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 10));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$screenshot_url = $custom["screenshot_url"][0];
$website_url = $custom["website_url"][0];
?>
<div id="portfolio-item">
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
functions.php:
<?php // sidebar functions
if(function_exists('register_sidebar'))
{
register_sidebar();
}
// portfolio functions
add_action('init', 'create_portfolio');
function create_portfolio() {
$portfolio_args = array(
'label' => __('Portfolio'),
'singular_label' => __('Portfolio'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'thumbnail')
);
register_post_type('portfolio',$portfolio_args);
}
// custom input- portfolio backend
add_action("admin_init", "add_portfolio");
add_action('save_post', 'update_website_url');
function add_portfolio(){
add_meta_box("portfolio_details", "Portfolio Options", "portfolio_options", "portfolio", "normal", "low");
}
function portfolio_options(){
global $post;
$custom = get_post_custom($post->ID);
$website_url = $custom["website_url"][0];
}
function update_website_url(){
global $post;
update_post_meta($post->ID, "website_url", $_POST["website_url"]);
}
// detail columns- portfolio backend
add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");
add_action("manage_posts_custom_column", "portfolio_columns_display");
function portfolio_edit_columns($portfolio_columns){
$portfolio_columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Project Title",
"description" => "Description",
);
return $portfolio_columns;
}
function portfolio_columns_display($portfolio_columns){
switch ($portfolio_columns)
{
case "description":
the_excerpt();
break;
}
}
// add thumbnail support
add_theme_support('post-thumbnails');
// disable autoformat with raw
function my_formatter($content) {
$new_content = '';
$pattern_full = '{(\[raw\].*?\[/raw\])}is';
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'my_formatter', 99);
?>
http://www.cssbakery.com/2010/07/image-grid-using-css-floats_6950.html
This should be what you're looking for.
You can hardcode it was well with PHP if you'd like using a table if you can't get that CSS working, but that's not good coding technically.