I have a standard widget which I've included in my footer with
<?php get_sidebar(); ?>
The content of the widget is visible only in categories pages. e.g. archive.php.
How can I show it on other pages too? It is included with the footer on all pages, like this:
<?php get_footer(); ?>
In the footer.php I have
<div class="footer-center" data-equalizer-watch>
<div class="baseline">
<div class="newsletter">
<?php get_sidebar(); ?>
</div>
</div>
</div>
UPDATE
<div class="newsletter">
<?php dynamic_sidebar( 'sidebar' ); ?>
</div>
Just changed get_sidebar(); to dynamic_sidebar( 'sidebar' );
Widget function in functions.php
//Stay Connected widget
add_filter( 'widget_text', 'do_shortcode' );
function stay_connected_load_widget() {
register_widget( 'stay_connected_widget' );
}
add_action( 'widgets_init', 'stay_connected_load_widget' );
class stay_connected_widget extends WP_Widget {
function __construct() {
parent::__construct(
'stay_connected_widget',
__('Stay Connected', 'stay_connected_widget_domain'),
array( 'description' => __( 'Social networks and subscribe form', 'stay_connected_widget_domain' ))
);
}
//Widget frontend
public function widget( $args, $instance ) {
$cf7_form = apply_filters( 'widget_title', $instance['cf7_form'] );
echo $args['before_widget'];
// echo $args['before_title'] . '<h4>' . __( 'Stay Connected', 'stay_connected_widget_domain' ) . '</h4>' .$args['after_title'];
?>
<?php
if(!empty( $cf7_form )){
echo '<h4 class="form_title">'. __('Subscribe By Email').'</h4>';
echo '<div class="stay_connected_form">'.do_shortcode(html_entity_decode($cf7_form)).'</div>';
}
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'cf7_form' ] ) ) {
$cf7_form = $instance[ 'cf7_form' ];
}
else {
$cf7_form = __( '', 'stay_connected_widget_domain' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'cf7_form' ); ?>"><?php _e( 'Contact Form:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'cf7_form' ); ?>" name="<?php echo $this->get_field_name( 'cf7_form' ); ?>" type="text" value="<?php echo esc_attr( $cf7_form ); ?>" placeholder="[<?php _e('contact form shortcode'); ?>]"/>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['cf7_form'] = ( ! empty( $new_instance['cf7_form'] ) ) ? strip_tags( $new_instance['cf7_form'] ) : '';
return $instance;
}
}
Update
Array (
[wp_inactive_widgets] => Array ( )
[header] => Array (
[0] => media_gallery-2
)
[footer] => Array (
[0] => custom_html-2
)
[sidebar] => Array (
[0] => stay_connected_widget-3
[1] => search-2
)
[array_version] => 3
)
Add this code in functions.php
function stay_widgets_init() {
register_sidebar( array(
'name' => 'Stay Connected ',
'id' => 'stay_connected_widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => '',
) );
}
add_action( 'widgets_init', 'stay_widgets_init' );
Use this in footer.php file
<div class="newsletter">
<?php dynamic_sidebar( 'stay_connected_widget' ); ?>
</div>
Related
I try to get the names of the pages in a loop.
In the widget, the page titles are displayed correctly, but the values are not saved.
How I can save the checked inputs?
Below Full Widget Code:
class Foo_Widget extends WP_Widget {
/** Register widget with WordPress.*/
function __construct() {
parent::__construct(
'foo_widget', // Base ID
esc_html__( 'Widget Title', 'text_domain' ), // Name
array( 'description' => esc_html__( 'A Foo Widget', 'text_domain' ), ) // Args
);
}
/*** Front-end display of widget.*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
echo esc_html__( 'Hello, World!', 'text_domain' );
echo $args['after_widget'];
}
/*** Back-end widget form.*/
public function form( $instance ) {
$mypages = get_pages( array(
'sort_column' => 'post_date',
'sort_order' => 'desc'
) );
foreach( $mypages as $page ) {
$title = $page->post_title;
$slug = $page->post_name;
?>
<p>
<label>
<input type='checkbox' name='the_pages' id='<?php echo $slug ?>' value='<?php echo esc_attr( $title ); ?>'><?php echo esc_attr( $title ); ?>
</label>
</p>
<? } ?>
<?php }
/*** Sanitize widget form values as they are saved.*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = $new_instance[ 'title' ];
return $instance;
}
}
// register Foo_Widget widget
function register_foo_widget() {
register_widget( 'Foo_Widget' );
}
add_action( 'widgets_init', 'register_foo_widget' );
?>
How should update() function looks like ?
Trying to build a widget for my simple wordpress blog, which will display categories in the sidebar, but not exactly like the native wordpress category widget. Basically, what I am trying to achieve is to be able to mark certain categories as "New!" or something similar, but from within the widget itself.
So far I have the following code that registers my widget and can display categories list in it on the backend with checkbox next to the name.
When I check the box and trying to save it it returns unchecked again. Not sure if my update function is actually working as serialized array in the DB has not changed on save.
Here is what I have do far:
/* CUSTOM BLOG CATEGORIES WIDGETS */
class Spr123_Categories_Widget extends WP_Widget {
public function __construct() {
$widget_options = array(
'classname' => 'widget_custom_categories_widget',
'description' => 'This is a Custom Blog Categories Widget',
);
parent::__construct( 'custom_categories_widget', 'Custom Categories Widget', $widget_options );
}
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$name = ! empty( $instance['name'] ) ? $instance['name'] : '';
$checked = isset( $instance['checked'] ) ? (bool) $instance['checked'] : false;
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
"hide_empty" => 0,
) );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="Categories">Categories:</label>
</p>
<?php print("<pre>".print_r($categories,true)."</pre>"); ?>
<p>
<?php
foreach ( $categories as $category ) {
?>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id($category->slug); ?>" name="<?php echo $this->get_field_name('checked'); ?>"<?php checked( $checked ); ?> />
<label for="<?php echo $this->get_field_id($category->slug); ?>"><?php _e( 'Display as NEW - ' . esc_attr( $category->name )); ?></label><br />
<?php
}
?>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'name' ] = strip_tags( $new_instance[ 'name' ] );
$instance[ 'checked' ] = !empty($new_instance['checked']) ? 1 : 0;
return $instance;
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$category_title = apply_filters( 'widget_title', $instance[ 'name' ] );
echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
<p><?php echo $category_title ?></p>
<?php echo $args['after_widget'];
}
}
function spr123_custom_categories_widget() {
register_widget( 'Spr123_Categories_Widget' );
}
add_action( 'widgets_init', 'spr123_custom_categories_widget' );
Try this code
/* CUSTOM BLOG CATEGORIES WIDGETS */
class Spr123_Categories_Widget extends WP_Widget {
public function __construct() {
$widget_options = array(
'classname' => 'widget_custom_categories_widget',
'description' => 'This is a Custom Blog Categories Widget',
);
parent::__construct( 'custom_categories_widget', 'Custom Categories Widget', $widget_options );
}
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$name = ! empty( $instance['name'] ) ? $instance['name'] : '';
$checked = isset( $instance['checked'] ) ? (bool) $instance['checked'] : false;
$savedcategories = ! empty( $instance['categories'] ) ? $instance['categories'] : '';
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
"hide_empty" => 0,
) );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="Categories">Categories:</label>
</p>
<!-- --><?php //print("<pre>".print_r($categories,true)."</pre>"); ?>
<p>
<?php
foreach ( $categories as $category ) {
$checked = in_array($category->term_id, $savedcategories)?'checked':'';
?>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id($category->slug); ?>" name="<?php echo $this->get_field_name('categories[]'); ?>" <?php echo $checked; ?> value="<?php echo $category->term_id; ?>"/>
<label for="<?php echo $this->get_field_id($category->slug); ?>"><?php echo esc_attr( $category->name ) . _e( 'Display as NEW - ' ); ?></label><br />
<?php
}
?>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'name' ] = strip_tags( $new_instance[ 'name' ] );
$instance[ 'categories' ] = $new_instance['categories'];
return $instance;
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$category_title = apply_filters( 'widget_title', $instance[ 'name' ] );
echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
<p><?php echo $category_title ?></p>
<?php echo $args['after_widget'];
}
}
function spr123_custom_categories_widget() {
register_widget( 'Spr123_Categories_Widget' );
}
add_action( 'widgets_init', 'spr123_custom_categories_widget' );
you can the value in frontend by using this code
$widget_instances = get_option('widget_custom_categories_widget');
print_r($widget_instances);
I was translating the titles and sentences right on the editor. I don't know what went wrong, but when I hit upload I keep getting this:
Parse error: syntax error, unexpected 'Comentários' (T_STRING) in /home2/tkleinow/public_html/wp-content/themes/fashionistas/inc/template-tags.php on line 94
Then I tried to copy the original code for that section (template tags) on the zip files, but I keep getting the same error.
I pasted it on Excel to find what the line was, and this is wat I got:
<?php printf( __( '%s', 'athemes' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
My website was doing just fine, I don't know how I screwed it like that.
This is the whole thing:
<?php
/**
* Custom template tags for this theme.
*
* Eventually, some of the functionality here could be replaced by core features
*
* #package aThemes
*/
if ( ! function_exists( 'athemes_content_nav' ) ) :
/**
* Display navigation to next/previous pages when applicable
*/
function athemes_content_nav( $nav_id ) {
global $wp_query, $post;
// Don't print empty markup on single pages if there's nowhere to navigate.
if ( is_single() ) {
$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous )
return;
}
// Don't print empty markup in archives if there's only one page.
if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
return;
$nav_class = ( is_single() ) ? 'post-navigation' : 'paging-navigation';
?>
<nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">
<h1 class="screen-reader-text"><?php _e( 'Post navigation', 'athemes' ); ?></h1>
<?php if ( is_single() ) : // navigation links for single posts ?>
<?php previous_post_link( '<div class="nav-previous"><span>Artigo Anterior</span>%link</div>', '<span class="meta-nav">' . _x( '←', 'Link do Post Anterior', 'athemes' ) . '</span> %title' ); ?>
<?php next_post_link( '<div class="nav-next"><span>Próximo Artigo</span>%link</div>', '%title <span class="meta-nav">' . _x( '→', 'Link para o próximo artigo', 'athemes' ) . '</span>' ); ?>
<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
<?php if ( get_next_posts_link() ) : ?>
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Artigos Anteriores', 'athemes' ) ); ?></div>
<?php endif; ?>
<?php if ( get_previous_posts_link() ) : ?>
<div class="nav-next"><?php previous_posts_link( __( 'Artigos Recentes <span class="meta-nav">→</span>', 'athemes' ) ); ?></div>
<?php endif; ?>
<?php endif; ?>
</nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
<?php
}
endif; // athemes_content_nav
if ( ! function_exists( 'athemes_comment' ) ) :
/**
* Template for comments and pingbacks.
*
* Used as a callback by wp_list_comments() for displaying the comments.
*/
function athemes_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>
<li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
<div class="comment-body">
<?php _e( 'Pingback:', 'athemes' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', 'athemes' ), '<span class="edit-link">', '</span>' ); ?>
</div>
<?php else : ?>
<li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="clearfix comment-meta">
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
<div class="clearfix comment-author vcard">
<?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
<div class="comment-metadata">
<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
<time datetime="<?php comment_time( 'c' ); ?>">
<?php printf( _x( '%1$s', '1: date, 2: time', 'athemes' ), get_comment_date(), get_comment_time() ); ?>
</time>
</a>
</div><!-- .comment-metadata -->
<?php printf( __( '%s', 'athemes' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
</div><!-- .comment-author -->
<?php if ( '0' == $comment->comment_approved ) : ?>
<p class="comment-awaiting-moderation"><?php _e( 'Seu comentário será aprovado assim que passar pela moderação.', 'athemes' ); ?></p>
<?php endif; ?>
</footer><!-- .comment-meta -->
<div class="comment-content">
<?php comment_text(); ?>
</div><!-- .comment-content -->
</article><!-- .comment-body -->
<?php
endif;
}
endif; // ends check for athemes_comment()
if ( ! function_exists( 'athemes_the_attached_image' ) ) :
/**
* Prints the attached image with a link to the next attached image.
*/
function athemes_the_attached_image() {
$post = get_post();
$attachment_size = apply_filters( 'athemes_attachment_size', array( 1200, 1200 ) );
$next_attachment_url = wp_get_attachment_url();
/**
* Grab the IDs of all the image attachments in a gallery so we can get the
* URL of the next adjacent image in a gallery, or the first image (if
* we're looking at the last image in a gallery), or, in a gallery of one,
* just the link to that image file.
*/
$attachment_ids = get_posts( array(
'post_parent' => $post->post_parent,
'fields' => 'ids',
'numberposts' => -1,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'
) );
// If there is more than 1 attachment in a gallery...
if ( count( $attachment_ids ) > 1 ) {
foreach ( $attachment_ids as $attachment_id ) {
if ( $attachment_id == $post->ID ) {
$next_id = current( $attachment_ids );
break;
}
}
// get the URL of the next image attachment...
if ( $next_id )
$next_attachment_url = get_attachment_link( $next_id );
// or get the URL of the first image attachment.
else
$next_attachment_url = get_attachment_link( array_shift( $attachment_ids ) );
}
printf( '%3$s',
esc_url( $next_attachment_url ),
the_title_attribute( array( 'echo' => false ) ),
wp_get_attachment_image( $post->ID, $attachment_size )
);
}
endif;
if ( ! function_exists( 'athemes_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
*/
function athemes_posted_on() {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
//if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
//$time_string .= '<time class="updated" datetime="%3$s">%4$s</time>';
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
printf( __( '<span class="posted-on">%1$s</span>', 'athemes' ),
sprintf( '%3$s',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
}
endif;
/**
* Returns true if a blog has more than 1 category
*/
function athemes_categorized_blog() {
if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
// Create an array of all the categories that are attached to posts
$all_the_cool_cats = get_categories( array(
'hide_empty' => 1,
) );
// Count the number of categories that are attached to the posts
$all_the_cool_cats = count( $all_the_cool_cats );
set_transient( 'all_the_cool_cats', $all_the_cool_cats );
}
if ( '1' != $all_the_cool_cats ) {
// This blog has more than 1 category so athemes_categorized_blog should return true
return true;
} else {
// This blog has only 1 category so athemes_categorized_blog should return false
return false;
}
}
/**
* Flush out the transients used in athemes_categorized_blog
*/
function athemes_category_transient_flusher() {
// Like, beat it. Dig?
delete_transient( 'all_the_cool_cats' );
}
add_action( 'edit_category', 'athemes_category_transient_flusher' );
add_action( 'save_post', 'athemes_category_transient_flusher' );
I'm using the Project AR2 theme and I'm trying to put a widget area directly underneath the slider. I've already added this to my functions.php file:
if ( function_exists('register_sidebar') ){
register_sidebar(array(
'name' => 'Below Slider Widgets',
'id' => 'below-slider-widgets',
'description' => 'The area below the Featured Posts slider. Great for small banner ads',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
But now I'm stuck on where to add this:
<div class="belowslider">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Below Slider Widgets')) :endif; ?>
</div>
It seems the home page content is rendered dynamically, so it's not as simple as, say, opening a header.php file and pasting it in some static location.
My site isn't live yet, but here's a link to the theme demo:
http://demo.arrastheme.com/
And a link to all of the theme's files:
https://github.com/zyml/project-ar2
I think the key may lie in the home.php file, but I could be wrong.
Any help would be appreciated.
// Creating the widget
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
echo __( 'Hello, World!', 'wpb_widget_domain' );
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here
// Register and load the widget
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
I am adding the register_sidebar function to my functions.php file as shown below:
<?php
add_action( 'after_setup_theme', 'handheld_setup' );
if ( ! function_exists( 'handheld_setup' ) ){
function handheld_setup(){
global $et_mobile_theme_options;
load_theme_textdomain( 'HandHeld', TEMPLATEPATH . '/languages' );
add_action( 'wp_ajax_nopriv_et_show_ajax_posts', 'et_show_ajax_posts' );
add_action( 'wp_ajax_et_show_ajax_posts', 'et_show_ajax_posts' );
if ( isset( $et_mobile_theme_options['bg_color'] ) && '' != $et_mobile_theme_options['bg_color'] ) add_action( 'wp_head','et_add_bgcolor' );
add_filter( 'template_include', 'et_check_homepage_static' );
add_action( 'wp_head', 'et_add_apple_touch_images', 7 );
}
}
function et_add_apple_touch_images(){
global $et_mobile_theme_options;
$webpage_icon_small = isset( $et_mobile_theme_options['webpage_icon_small'] ) && '' != $et_mobile_theme_options['webpage_icon_small'] ? $et_mobile_theme_options['webpage_icon_small'] : get_template_directory_uri() . '/images/ios_icons/apple-touch-icon-precomposed.png';
$webpage_icon_big = isset( $et_mobile_theme_options['webpage_icon_big'] ) && '' != $et_mobile_theme_options['webpage_icon_big'] ? $et_mobile_theme_options['webpage_icon_big'] : get_template_directory_uri() . '/images/ios_icons/apple-touch-icon.png';
$splash_image = isset( $et_mobile_theme_options['splash_image'] ) && '' != $et_mobile_theme_options['splash_image'] ? $et_mobile_theme_options['splash_image'] : get_template_directory_uri() . '/images/ios_icons/splash.png';
echo '<link rel="apple-touch-icon-precomposed" href="' . esc_url( $webpage_icon_small ) . '" />';
echo '<link rel="apple-touch-icon-precomposed" sizes="114x114" href="' . esc_url( $webpage_icon_big ) . '" />';
echo '<link rel="apple-touch-startup-image" href="' . esc_url( $splash_image ) . '" />';
}
function et_check_homepage_static( $template ){
# if static homepage is set ( WP-Admin / Settings / Reading ) and we're on the homepage, load home.php
if ( is_front_page() && ! is_home() ) $template = get_home_template();
return $template;
}
function et_add_bgcolor(){
global $et_mobile_theme_options;
echo '<style>body{ background-color: #'. esc_html( str_replace( '#', '', $et_mobile_theme_options['bg_color'] ) ) .'; }</style>';
}
if ( ! function_exists( 'et_mobile_custom_comments_display' ) ) :
function et_mobile_custom_comments_display($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<article id="comment-<?php comment_ID(); ?>" class="text_block comment clearfix">
<div class="avatar-box">
<?php echo get_avatar($comment,$size='37'); ?>
<span class="avatar-overlay"></span>
</div> <!-- end .avatar-box -->
<?php printf('<span class="fn">%s</span>', get_comment_author_link()) ?>
<div class="comment-content clearfix">
<?php if ($comment->comment_approved == '0') : ?>
<em class="moderation"><?php esc_html_e('Your comment is awaiting moderation.','HandHeld') ?></em>
<br />
<?php endif; ?>
<?php comment_text() ?>
</div> <!-- end comment-content-->
<div class="comment-meta clearfix">
<span class="comment-date"><?php if ( 1 == $depth ) printf( __( 'Posted on %1$s', 'HandHeld' ), get_comment_date() ); else echo get_comment_date(); ?></span>
<?php
$et_comment_reply_link = get_comment_reply_link( array_merge( $args, array('reply_text' => esc_attr__('Reply','HandHeld'),'depth' => $depth, 'max_depth' => $args['max_depth'])) );
if ( $et_comment_reply_link ) echo '<div class="reply-container">' . $et_comment_reply_link . '</div>';
?>
</div> <!-- end .comment-meta -->
</article>
<?php }
endif;
if ( ! function_exists( 'et_list_pings' ) ){
function et_list_pings($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?> - <?php comment_excerpt(); ?>
<?php }
}
if ( ! function_exists( 'et_mobile_regular_post' ) ){
function et_mobile_regular_post(){
global $post; ?>
<article class="post text_block clearfix">
<?php
$thumb = '';
$width = 72;
$height = 72;
$classtext = '';
$titletext = get_the_title();
$thumbnail = et_get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"];
?>
<?php if( $thumb <> '' ){ ?>
<div class="post-thumb">
<a href="<?php the_permalink(); ?>">
<?php et_print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
<span class="overlay"></span>
</a>
<span class="comment_count"><?php comments_popup_link( 0, 1, '%' ); ?></span>
</div> <!-- end .post-thumb -->
<?php } ?>
<div class="post-content">
<h1><?php the_title(); ?></h1>
<p class="meta-info"><?php esc_html_e('Posted on','HandHeld'); ?> <time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_time( 'F jS' ); ?></time></p>
</div> <!-- end .post-content -->
<?php esc_html_e('Read more','HandHeld'); ?>
</article> <!-- end .post -->
<?php }
}
if ( ! function_exists('register_sidebar') ) {
// Register Sidebar
function register_sidebar() {
$args = array(
'id' => 'mobile-sidebar',
'name' => 'Mobile',
'description' => __( 'Sidebar for mobile', 'text_domain' ),
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
);
register_sidebar( $args );
}
// Hook into the 'widgets_init' action
add_action( 'init', 'register_sidebar' );
}
if ( ! function_exists( 'et_mobile_gallery_post' ) ){
function et_mobile_gallery_post(){
global $post; ?>
<a href="<?php the_permalink(); ?>" class="project">
<?php
$thumb = '';
$width = 70;
$height = 70;
$classtext = '';
$titletext = get_the_title();
$thumbnail = et_get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Project');
$thumb = $thumbnail["thumb"];
?>
<?php et_print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
<span></span>
</a>
<?php }
}
add_action( 'template_redirect', 'et_mobile_load_ajax_scripts' );
function et_mobile_load_ajax_scripts(){
wp_enqueue_script( 'et_home_load_more', get_template_directory_uri() . '/js/custom.js', array( 'jquery' ) );
wp_localize_script( 'et_home_load_more', 'etmobile', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'et_load_nonce' => wp_create_nonce( 'et_load_nonce' ) ) );
}
function et_show_ajax_posts() {
global $et_mobile_theme_options;
if ( ! wp_verify_nonce( $_POST['et_load_nonce'], 'et_load_nonce' ) ) die(-1);
$posts_num = (int) $_POST['et_posts_num'];
$posts_offset = (int) $_POST['et_posts_offset'];
$gallery = (int) $_POST['et_gallery'];
$args = array(
'posts_per_page' => $posts_num,
'offset' => $posts_offset,
'post_status' => 'publish'
);
if ( isset( $et_mobile_theme_options['home_blog_categories'] ) && !empty( $et_mobile_theme_options['home_blog_categories'] ) && 0 == $gallery )
$args['category__in'] = $et_mobile_theme_options['home_blog_categories'];
if ( 0 != $gallery && isset( $et_mobile_theme_options['home_project_categories'] ) && !empty( $et_mobile_theme_options['home_project_categories'] ) )
$args['category__in'] = $et_mobile_theme_options['home_project_categories'];
ob_start();
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( 0 == $gallery ) { ?>
<?php et_mobile_regular_post(); ?>
<?php } else { ?>
<?php et_mobile_gallery_post(); ?>
<?php } ?>
<?php endwhile;
wp_reset_postdata();
$posts = ob_get_clean();
$last_query = ( $the_query->found_posts - $posts_offset ) > $posts_num ? false : true;
echo json_encode( array( 'posts' => $posts, 'last_query' => $last_query ) );
die();
} ?>
The register_sidebar function is declared at line 114.
In my home.phpfile, I am trying to call the sidebar as shown below:
<?php dynamic_sidebar( 'mobile-sidebar' ); ?>
But the sidebar never gets displayed.
What am I missing?
Thanks
Code in Functions.php
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Homepage Sidebar',
'id' => 'homepage-sidebar',
'description' => 'Appears as the sidebar on the custom homepage',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
}
Code in your home.php
<?php get_sidebar('homepage'); ?>
Create a file "sidebar-homepage.php"
Paste this code:
<div class="custom">
<?php
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('homepage-sidebar') ) :
endif; ?>
</div>
Go to widgets and add some Text... Enjoy..