Tag Archives: widget

Different widget in sidebar for different post and page

Add required widget in sidebar

Create Sidebar


register_sidebar(array(
'name' => __('Contact Attorny', 'legal'),
'id' => 'contact-attorny',
'description' => __('Contact Attorny sidebar that appears on the right.', 'legal'),
'before_widget' => '<aside id="%1$s" class="sidebar-widget widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<div class="sidebar-widget"><h3>',
'after_title' => '</h3></div>',
));

register_sidebar(array(
'name' => __('Twitter', 'legal'),
'id' => 'twitterfids',
'description' => __('Twitter that appears on the right.', 'legal'),
'before_widget' => '<aside id="%1$s" class="sidebar-widget widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<div class="sidebar-widget"><h3>',
'after_title' => '</h3></div>',
));

register_sidebar(array(
'name' => __('Blog Category', 'legal'),
'id' => 'bcats',
'description' => __('Blog Category that appears on the right.', 'legal'),
'before_widget' => '<aside id="%1$s" class="sidebar-widget widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<div class="sidebar-widget"><h3>',
'after_title' => '</h3></div>',
));
register_sidebar(array(
'name' => __('Practice Area', 'legal'),
'id' => 'practicearea',
'description' => __('Practice Area that appears on the right.', 'legal'),
'before_widget' => '<aside id="%1$s" class="sidebar-widget widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<div class="sidebar-widget"><h3>',
'after_title' => '</h3></div>',
));

Sidebar.php


<?php
$legal_options = get_option('legal_theme_options');
?>
<?php if ( is_singular('practice-areas') ) { ?>
<div class="sidebar-box">
<?php if (is_active_sidebar('contact-attorny')) {
dynamic_sidebar('contact-attorny'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('twitterfids')) {
dynamic_sidebar('twitterfids'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('practicearea')) {
dynamic_sidebar('practicearea'); }?>
</div>
<?php } ?>

<?php if ( is_singular('post') ) { ?>
<div class="sidebar-box">
<?php if (is_active_sidebar('contact-attorny')) {
dynamic_sidebar('contact-attorny'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('twitterfids')) {
dynamic_sidebar('twitterfids'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('bcats')) {
dynamic_sidebar('bcats'); }?>
</div>
<?php } ?>

<?php if(is_home()) { ?>
<div class="sidebar-box">
<?php if (is_active_sidebar('contact-attorny')) {
dynamic_sidebar('contact-attorny'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('twitterfids')) {
dynamic_sidebar('twitterfids'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('bcats')) {
dynamic_sidebar('bcats'); }?>
</div>
<?php } ?>

<?php if (is_page_template('page-template/testimonials.php')) { ?>
<div class="sidebar-box">
<?php if (is_active_sidebar('contact-attorny')) {
dynamic_sidebar('contact-attorny'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('twitterfids')) {
dynamic_sidebar('twitterfids'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('practicearea')) {
dynamic_sidebar('practicearea'); }?>
</div>
<?php } ?>

<?php if (is_page_template('page-template/case-results.php')) { ?>
<div class="sidebar-box">
<?php if (is_active_sidebar('practicearea')) {
dynamic_sidebar('practicearea'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('twitterfids')) {
dynamic_sidebar('twitterfids'); }?>
</div>
<?php } ?>

<?php if (is_page(204)) { ?>
<div class="sidebar-box">
<?php if (is_active_sidebar('contact-attorny')) {
dynamic_sidebar('contact-attorny'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('twitterfids')) {
dynamic_sidebar('twitterfids'); }?>
</div>
<?php } ?>

<?php if (is_page(269)) { ?>
<div class="sidebar-box">
<?php if (is_active_sidebar('contact-attorny')) {
dynamic_sidebar('contact-attorny'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('twitterfids')) {
dynamic_sidebar('twitterfids'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('bcats')) {
dynamic_sidebar('bcats'); }?>
</div>
<?php } ?>

<?php if (is_page(206)) { ?>
<div class="sidebar-box">
<?php if (is_active_sidebar('contact-attorny')) {
dynamic_sidebar('contact-attorny'); }?>
</div>
<div class="sidebar-box">
<?php if (is_active_sidebar('twitterfids')) {
dynamic_sidebar('twitterfids'); }?>
</div>
<div class="sidebar-box">
<div class="address_info">
<h4> <?php echo esc_attr($legal_options['contacttitles']); ?> </h4>
<div class="textwidget">
<ul>
<li>
<?php echo esc_attr($legal_options['contactaddresses']); ?><br>
Phone: <?php  echo esc_url($legal_options['pnumbers']); ?><br>
Fax: <?php  echo esc_attr($legal_options['faxnumbs']); ?><br>
E-mail: <a href="mailto:<?php echo sanitize_email($legal_options['cemails']); ?>"><?php echo sanitize_email($legal_options['cemails']); ?></a><br>
Website: <a href="<?php echo esc_url($legal_options['cweb']); ?>"><?php echo esc_url($legal_options['cweb']); ?></a> </li>
</ul>
</div>
</div>
</div>

<?php } ?>

Simple Contact Widget

functions.php


<?php
 /*
 * Contact information widget
 */
 add_action( 'widgets_init', 'medics_widget_contact_info' );

function medics_widget_contact_info() {
 register_widget('medics_contact_info_widget');
 }

class medics_contact_info_widget extends WP_Widget {

function medics_contact_info_widget() {
 $medics_widget_ops = array( 'classname' => 'medics_info', 'description' => __('A widget that displays the title,contact information and socia links. ', 'medics_info') );

$medics_control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'medics_contact_info_widget' );

$this->WP_Widget( 'medics_contact_info_widget', __('Medics Contact Information', 'medics_info'), $medics_widget_ops, $medics_control_ops );
 }

function widget( $medics_args, $medics_instance ) {
 extract( $medics_args );

//Our variables from the widget settings.
 $medics_title = apply_filters('widget_title', $medics_instance['title'] );
 $medics_address = $medics_instance['address'];
 $medics_country = $medics_instance['country'];

$medics_phone = $medics_instance['phone'];
 $medics_fax = $medics_instance['fax'];
 $medics_email = $medics_instance['email'];
 $medics_website = $medics_instance['website'];

echo $before_widget;

Display widget
?>
<h1><?php if(!empty($medics_instance[‘title’])) echo $medics_instance[‘title’]; else echo “Contributor”; ?></h1>
<div class=”footer-title-line”></div>
<div class=”contct-widget”>
<?php if(!empty($medics_instance[‘address’])) { ?>
<p><?php echo $medics_instance[‘address’]; ?>

<?php if(!empty($medics_instance[‘country’])) { ?>
<?php echo “,”.$medics_instance[‘country’]; ?>
<?php } ?>
</p>
<?php } ?>
<?php if(!empty($medics_instance[‘phone’])) { ?>
<p>Phone: <?php echo $medics_instance[‘phone’]; ?></p>
<?php } ?>

<?php if(!empty($medics_instance[‘fax’])) { ?>
<p>Fax: <?php echo $medics_instance[‘fax’]; ?></p>
<?php } ?>

<?php if(!empty($medics_instance[’email’])) { ?>
<p>Email: <a href=”mailto:<?php echo $medics_instance[’email’]; ?>”><?php echo $medics_instance[’email’]; ?></a></p>
<?php } ?>

<?php if(!empty($medics_instance[‘website’])) { ?>
<p>Web: <a href=”<?php echo esc_url($medics_instance[‘website’]); ?>”><?php echo esc_url($medics_instance[‘website’]); ?></a></p>
<?php } ?>
</div>

<?php
echo $after_widget;
}

//Update the widget

function update( $new_instance, $old_instance ) {
$medics_instance = $old_instance;

//Strip tags from title and name to remove HTML
$medics_instance[‘title’] = strip_tags( $new_instance[‘title’] );
$medics_instance[‘address’] = strip_tags( $new_instance[‘address’] );
$medics_instance[‘country’] = strip_tags( $new_instance[‘country’] );
$medics_instance[‘phone’] = strip_tags( $new_instance[‘phone’] );
$medics_instance[‘fax’] = strip_tags( $new_instance[‘fax’] );
$medics_instance[‘website’] = esc_url_raw(strip_tags( $new_instance[‘website’]));
$medics_instance[’email’] = sanitize_email(strip_tags( $new_instance[’email’] ));

return $medics_instance;
}

function form( $medics_instance ) {
?>
<p>
<label for=”<?php echo $this->get_field_id( ‘title’ ); ?>”>
<?php _e(‘Widget Title:’, ‘medics_info’); ?>
</label>
<input id=”<?php echo $this->get_field_id( ‘title’ ); ?>” name=”<?php echo $this->get_field_name( ‘title’ ); ?>” value=”<?php if(!empty($medics_instance[‘title’])) { echo $medics_instance[‘title’]; } ?>” style=”width:100%;” />
</p>
<p>
<label for=”<?php echo $this->get_field_id( ‘address’ ); ?>”>
<?php _e(‘Address:’, ‘medics_info’); ?>
</label>
<input id=”<?php echo $this->get_field_id( ‘address’ ); ?>” name=”<?php echo $this->get_field_name( ‘address’ ); ?>” value=”<?php if(!empty($medics_instance[‘address’])) { echo $medics_instance[‘address’]; } ?>” style=”width:100%;” />
</p>
<p>
<label for=”<?php echo $this->get_field_id( ‘country’ ); ?>”>
<?php _e(‘Country:’, ‘medics_info’); ?>
</label>
<input id=”<?php echo $this->get_field_id( ‘country’ ); ?>” name=”<?php echo $this->get_field_name( ‘country’ ); ?>” value=”<?php if(!empty($medics_instance[‘country’])) { echo $medics_instance[‘country’]; } ?>” style=”width:100%;” />
</p>
<p>
<label for=”<?php echo $this->get_field_id( ‘phone’ ); ?>”>
<?php _e(‘Phone:’, ‘medics_info’); ?>
</label>
<input id=”<?php echo $this->get_field_id( ‘phone’ ); ?>” name=”<?php echo $this->get_field_name( ‘phone’ ); ?>” value=”<?php if(!empty($medics_instance[‘phone’])) { echo $medics_instance[‘phone’]; } ?>” style=”width:100%;” />
</p>
<p>
<label for=”<?php echo $this->get_field_id( ‘fax’ ); ?>”>
<?php _e(‘Fax:’, ‘medics_info’); ?>
</label>
<input id=”<?php echo $this->get_field_id( ‘fax’ ); ?>” name=”<?php echo $this->get_field_name( ‘fax’ ); ?>” value=”<?php if(!empty($medics_instance[‘fax’])) { echo $medics_instance[‘fax’]; } ?>” style=”width:100%;” />
</p>
<p>
<label for=”<?php echo $this->get_field_id( ’email’ ); ?>”>
<?php _e(‘Email Address:’, ‘medics_info’); ?>
</label>
<input id=”<?php echo $this->get_field_id( ’email’ ); ?>” name=”<?php echo $this->get_field_name( ’email’ ); ?>” value=”<?php if(!empty($medics_instance[’email’])) { echo $medics_instance[’email’]; } ?>” style=”width:100%;” />
</p>
<p>
<label for=”<?php echo $this->get_field_id( ‘website’ ); ?>”>
<?php _e(‘Website:’, ‘medics_info’); ?>
</label>
<input id=”<?php echo $this->get_field_id( ‘website’ ); ?>” name=”<?php echo $this->get_field_name( ‘website’ ); ?>” value=”<?php if(!empty($medics_instance[‘website’])) { echo $medics_instance[‘website’]; } ?>” style=”width:100%;” />
</p>
<?php
}
}

?>

[/code]

Recent Posts Widget with thumbnail

functions.php


<?php
add_action('widgets_init', 'legal_recentpost_widget');
function legal_recentpost_widget() {
register_widget('legal_recent_widget');
}
class legal_recent_widget extends WP_Widget {
function legal_recent_widget() {
$legal_widget_ops = array('classname' => 'legal_recent', 'description' => __('A widget that display recent posts. ', 'legal'));
$legal_control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'legal-recent-widget');
$this->WP_Widget('legal-recent-widget', __('Legal Recent Posts', 'legal'), $legal_widget_ops, $legal_control_ops);
}
function widget($legal_args, $legal_instance) {
extract($legal_args);
//Our variables from the widget settings.
$legal_title = apply_filters('widget_title', $legal_instance['title']);
$legal_noofpost = $legal_instance['noofpost'];
$legal_hidedate = $legal_instance['hidedate'];
if (empty($legal_noofpost) || $legal_noofpost == 0)
$legal_noofpost = 2;

$legal_post_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $legal_noofpost,
'order' => 'DESC',
'orderby' => 'post_date',
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
)
);
global $wp_query;
$legal_query = new WP_Query($legal_post_args);
echo $before_widget;
//Display widget
?>
<?php if ($legal_query->have_posts()):
if (!empty($legal_instance['title'])):
echo $legal_args['before_title'] . apply_filters( 'widget_title', $legal_instance['title'] ). $legal_args['after_title'];
else:
echo $legal_args['before_title'] . apply_filters( 'widget_title', 'Recent Post'). $legal_args['after_title'];
endif;

while ($legal_query->have_posts()): $legal_query->the_post(); ?>
<?php
$apusfeatured = wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()));
if (!empty($apusfeatured)):
?>
<div class="footer-recent-post">
<ul>
<li>
<div class="footer-recent-img">
<?php $legal_image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'legal-widget-image'); ?>
<a href="<?php echo get_permalink(); ?>">
<img src="<?php echo esc_url($legal_image[0]); ?>" width="<?php echo $legal_image[1]; ?>" height="<?php echo $legal_image[2]; ?>" alt="<?php echo get_the_title(); ?>" class="img-responsive" />
</a>
</div>
<div class="post-date">
<a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
<?php if (empty($legal_instance['hidedate'])) { ?>
<p><?php echo get_the_date("M d,Y", get_the_ID()); ?></p>
<?php } ?>
</div>
</li>
</ul>
</div>

<?php endif; ?>
<?php endwhile; ?>
<?php endif;
wp_reset_query(); ?>
<?php
echo $after_widget;
}
//Update the widget
function update($new_instance, $old_instance) {
$legal_instance = $old_instance;
$legal_instance['title'] = strip_tags($new_instance['title']);
$legal_instance['noofpost'] = strip_tags($new_instance['noofpost']);
$legal_instance['hidedate'] = strip_tags($new_instance['hidedate']);
return $legal_instance;
}
function form($legal_instance) {
if (empty($legal_instance['noofpost']) || $legal_instance['noofpost'] == 0)
$legal_instance['noofpost'] = 2;
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">
<?php _e('Title', 'legal');
echo ":"; ?>
</label>
<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php if (!empty($legal_instance['title'])) {
echo $legal_instance['title'];
} ?>" style="width:100%;" type="text" />
</p>
<p>
<label for="<?php echo $this->get_field_id('noofpost'); ?>">
<?php _e('Number of posts to show', 'legal');
echo ":"; ?>
</label>
<input id="<?php echo $this->get_field_id('noofpost'); ?>" name="<?php echo $this->get_field_name('noofpost'); ?>" value="<?php if (!empty($legal_instance['noofpost'])) {
echo $legal_instance['noofpost'];
} ?>" size="3" type="text" />
</p>
<p>
<input id="<?php echo $this->get_field_id('hidedate'); ?>" name="<?php echo $this->get_field_name('hidedate'); ?>" value="yes"
<?php if (!empty($legal_instance['hidedate'])) { ?> checked="checked" <?php } ?>size="3" type="checkbox" />
<label for="<?php echo $this->get_field_id('hidedate'); ?>">
<?php _e('Check to hide post date', 'legal');?>
</label>
</p>
<?php
}
}
?>

PHP code applay in text widget

Paste in functions.php

function php_text($text) {
 if (strpos($text, '<' . '?') !== false) {
 ob_start();
 eval('?' . '>' . $text);
 $text = ob_get_contents();
 ob_end_clean();
 }
 return $text;
}
add_filter('widget_text', 'php_text', 99);
%d bloggers like this: