I have a widget that shows posts with the highest number of views first. However, it actually show the posts in all categories. I would like to only show most popular posts in a particular category, how do i do that?
Here is the code:
<?php
class anthemes_hotposts extends WP_Widget {
function anthemes_hotposts() {
$widget_ops = array('description' => 'Hot Posts by Views' );
parent::WP_Widget(false, $name = 'Custom: Hot Posts by Views',$widget_ops);
}
function widget($args, $instance) {
extract( $args );
$number = $instance['number'];
$title = $instance['title'];
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul class="article_list">
<?php $inntop = new WP_Query(array('meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'posts_per_page' => $number )); ?>
<?php while ($inntop->have_posts()) : $inntop->the_post(); ?>
<li><a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail()) { ?>
<?php echo the_post_thumbnail('thumbnail-widget'); ?>
<?php } else { ?><img src="http://placehold.it/80x80&text=No+Image" width="80" height="80" alt="article image" /><?php } ?></a>
<div class="an-display-time"><?php echo time_ago(); ?> <?php _e('ago', 'anthemes'); ?></div><br />
<h3><?php the_title(); ?></h3>
<div class="sub-article-widget">
<div class="an-display-view"><i class="fa fa-eye"></i> <?php echo getPostViews(get_the_ID()); ?></div>
<div class="an-display-like"><?php if( function_exists('zilla_likes') ) zilla_likes(); ?></div>
<div class="an-display-comm"><i class="fa fa-comments"></i> <?php comments_popup_link('0', '1', '%'); ?></div><div class="clear"></div>
</div>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
<?php echo $after_widget; ?>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = strip_tags($new_instance['number']);
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">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 if( isset($instance['title']) ) echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('number'); ?>">Number of Posts:</label>
<input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php if( isset($instance['number']) ) echo $instance['number']; ?>" />
</p>
<?php } }
add_action('widgets_init', create_function('', 'return register_widget("anthemes_hotposts");')); // register widget
?>
Related
I want to create a widget which simply loop five posts from a category.
User can select the category from the widget options.
Right now I create widget and the loop with a hard coded value (Cat ID) successfully.
What I need and I'm stuck in is changing this hard coded value to dynamic select/option which pulls from the categories drop-down list so user choose from the widget options.
<?php
// Initiate roms_widget_menu ends here
class roms_widget_menu extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID
'roms_widget_menu', __('Romaisa Mege menu widget', 'roms_menu_widget_domain'), array('description' => __('Designed to display a category post in mega menu (Widgets in Menus plugin must be installed)', 'roms_menu_widget_domain'),));
}
public function widget($args, $instance) {
$title = apply_filters('widget_title', $instance['title']);
$social_menu_title = apply_filters('widget_title', $instance['title']);
// before and after widget arguments are defined by themes
echo $args['before_widget'];
// Romaisa Widget user front Content
?>
<div class="row small-up-1 medium-up-5 large-up-5">
<?php
$args = array(
'cat' => 2,
'posts_per_page' => 5, //This is the hard coded value
);
$widget_module_menu_qry = new WP_Query( $args );
?>
<?php if ( $widget_module_menu_qry->have_posts() ) : while ( $widget_module_menu_qry->have_posts() ) : $widget_module_menu_qry->the_post(); ?>
<div class="column column-block">
<div class="megamenu-item">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(array(230, 130)); ?>
</a>
<?php else: ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img src="<?php echo get_template_directory_uri(). '/romisa-assets/img/sample/230x130.png'; ?>" alt="">
</a>
<?php endif; ?>
<div class="megamenu-item-typo text-left">
<h5 class="text-left">
<a class="megamenu-item-typo-title trans1" href="<?php the_permalink(); ?>"><b><?php the_title(); ?></b></a>
</h5>
<p><?php echo excerpt(22); ?></p>
<h6 class="megamenu-item-date"><i class="fa fa-calendar"></i> <?php the_time('F j, Y'); ?> </h6>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
<?php
// /Romaisa Widget user front Content
echo $args['after_widget'];
}
// Widget Backend
public function form($instance) {
if ( isset( $instance[ 'title' ] ) ) { $title = $instance['title']; }
else { $title = __( 'Custom HTML for Menu', 'roms_menu_widget_domain' ); }
?>
<!-- Example for widget category select dropdown that need to be dynamic -->
<select>
<option>Category 1</option>
<option>Category 2</option>
</select>
<?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 roms_widget_menu ends here
Please check the code below:
class roms_widget_menu extends WP_Widget
{
function __construct()
{
parent::__construct(
// Base ID
'roms_widget_menu', __('Romaisa Mege menu widget', 'roms_menu_widget_domain'), array('description' => __('Designed to display a category post in mega menu (Widgets in Menus plugin must be installed)', 'roms_menu_widget_domain'),));
}
public function widget($args, $instance)
{
$title = apply_filters('widget_title', $instance['title']);
$social_menu_title = apply_filters('widget_title', $instance['title']);
// before and after widget arguments are defined by themes
echo $args['before_widget'];
// Romaisa Widget user front Content
?>
<div class="row small-up-1 medium-up-5 large-up-5">
<?php
if ( ! empty( $title ) )
{
echo $args['before_title'] . esc_html($title) . $args['after_title'];
}
$q_args = array();
if (isset($instance['cat'])) {
$q_args["cat"] = $instance['cat'];
}
$q_args['posts_per_page'] = (isset($instance['number_of_posts']) ? (int)$instance['number_of_posts'] : 5);
$widget_module_menu_qry = new WP_Query($q_args);
?>
<?php if ($widget_module_menu_qry->have_posts()) : while ($widget_module_menu_qry->have_posts()) : $widget_module_menu_qry->the_post(); ?>
<div class="column column-block">
<div class="megamenu-item">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(array(230, 130)); ?>
</a>
<?php else: ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img
src="<?php echo get_template_directory_uri() . '/romisa-assets/img/sample/230x130.png'; ?>"
alt="">
</a>
<?php endif; ?>
<div class="megamenu-item-typo text-left">
<h5 class="text-left">
<a class="megamenu-item-typo-title trans1"
href="<?php the_permalink(); ?>"><b><?php the_title(); ?></b></a>
</h5>
<!-- <p>--><?php //echo excerpt(22); ?><!--</p>-->
<h6 class="megamenu-item-date"><i class="fa fa-calendar"></i> <?php the_time('F j, Y'); ?>
</h6>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
endif; ?>
</div>
<?php
// /Romaisa Widget user front Content
echo $args['after_widget'];
}
// Widget Backend
public function form($instance)
{
if (isset($instance['title'])) {
$title = $instance['title'];
} else {
$title = __('Custom HTML for Menu', 'roms_menu_widget_domain');
}
if (isset($instance['cat'])) {
$cat = $instance['cat'];
} else {
$cat = '';
}
if (isset($instance['number_of_posts'])) {
$np = $instance['number_of_posts'];
} else {
$np = 5;
}
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:', 'text-domain'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>"
name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text"
value="<?php echo esc_attr($title); ?>"/>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('cat')); ?>"><?php esc_html_e('Categories:', 'text-domain'); ?></label>
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('cat')); ?>"
name="<?php echo esc_attr($this->get_field_name('cat')); ?>">
<?php
$terms = get_terms('category', array(
'hide_empty' => false,
));
if (count($terms) > 0) {
foreach ($terms as $term) {
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
}
}
?>
</select>
</p>
<script>
document.getElementById("<?php echo esc_attr($this->get_field_id('cat')); ?>").value =
"<?php echo $cat; ?>";
</script>
<p>
<label for="<?php echo esc_attr($this->get_field_id('number_of_posts')); ?>"><?php esc_html_e('Number Of Posts:', 'text-domain'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('number_of_posts')); ?>"
name="<?php echo esc_attr($this->get_field_name('number_of_posts')); ?>" type="text"
value="<?php echo esc_attr($np); ?>"/>
</p>
<?php
}
// Updating widget replacing old instances with new
public function update($new_instance, $old_instance)
{
$instance['title'] = strip_tags($new_instance['title']);
$instance['number_of_posts'] = $new_instance['number_of_posts'];
$instance['cat'] = $new_instance['cat'];
return $instance;
}
} // Class roms_widget_menu ends here
Ok so I have a sidebar widget that pulls in from a list of posts from a custom post type, which itself pulls in from all other custom post types. I need to display the list of posts from tab 2 in this sidebar on a page as a list of posts like an archive. This is the code that pulls in the posts for the sidebar:
<?php
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;
class ChemTabs extends WP_Widget {
function __construct() {
parent::__construct(
'tabs', _('Tabs Widget'),
array(
'description' => _('A tabs widget.'),
));
}
private function ce_getPostClass($style, $index) {
switch ($style) {
case 'first-up':
if ( $index == 0 ) return 'first-up';
else return 'list-type';
break;
default:
return $style;
}
}
private function ce_has($listStyle, $index, $post) {
$class = $this->ce_getPostClass($listStyle, $index);
$has = array(
'thumb' => false,
'terms' => true,
'date' => true,
'content' => false,
'excerpt' => false,
'title' => true,
'video' => false,
'webinarDate' => false
);
switch ($class) {
case 'first-up':
$has['thumb'] = true;
$has['excerpt'] = true;
break;
case 'list-type':
$has['terms'] = false;
break;
case 'thumblist':
$has['thumb'] = true;
break;
case 'single': // no need for this anymore probably
$has['content'] = true;
break;
}
switch ($post->post_type) {
case 'video':
$has['video'] = true;
break;
case 'webinar':
$has['webinarDate'] = true;
break;
}
return $has;
}
public function widget( $args, $instance ) {
global $post;
$instance['tab_1_title'] = isset($instance['tab_1_title'])?$instance['tab_1_title']:'Tab 1';
$instance['tab_2_title'] = isset($instance['tab_2_title'])?$instance['tab_2_title']:'Tab 2';
$instance['storylist_id_1'] = isset($instance['storylist_id_1']) ? $instance['storylist_id_1'] : '';
$instance['storylist_id_2'] = isset($instance['storylist_id_2']) ? $instance['storylist_id_2'] : '';
$instance['n'] = isset($instance['n']) ? $instance['n'] : 4;
$title = isset($instance['title'])?$instance['title']:'';
$type = isset($instance['type'])?$instance['type']:'color';
$class = isset($instance['class'])?$instance['class']:'brand-primary';
// Post list configuration
// Note: posts temporarily rendered with ajax
$list_style = isset($instance['listStyle'])?$instance['listStyle']:'thumblist';
$list_post_type = isset($instance['post_type'])?$instance['post_type']:'post';
$list_filter = isset($instance['filter'])?$instance['filter']:'{}';
$list_display = isset($instance['display'])?$instance['display']:5;
$list_channel = isset($instance['channel'])?$instance['channel']:0;
$list_division = isset($instance['division'])?$instance['division']:0;
$readmore = isset($instance['readmore'])?$instance['readmore'] : '';
$list_args1 = array( "post_type" => "any", "post__in" => get_post_meta($instance['storylist_id_1'], 'storylist', true), "orderby" => "post__in" );
$posts1 = new WP_Query();
$posts1 = $posts1->query($list_args1);
$list_args2 = array( "post_type" => "any", "post__in" => get_post_meta($instance['storylist_id_2'], 'storylist', true), "orderby" => "post__in" );
$posts2 = new WP_Query();
$posts2 = $posts2->query($list_args2);
// Note: posts temporarily rendered with ajax
echo $args['before_widget'];
$instance['tab_2_title'] = get_the_title($instance['storylist_id_2']);
//#TODO: Use get_template_part('content/post', $modifier); on tabs too.
?>
<div class="module tabs">
<div class="striped striped-gray">
<!-- Nav tabs -->
<ul class="nav nav-tabs nav-justified" role="tablist">
<li class="active">
<a href="#tab-pane-1" role="tab" data-toggle="tab">
<h2 class="h4"><?= $instance['tab_1_title'] ?></h2>
</a>
</li>
<li>
<a href="#tab-pane-2" role="tab" data-toggle="tab">
<h2 class="h4"><?= $instance['tab_2_title'] ?></h2>
</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane active" id="tab-pane-1">
<div class="post-list no-c-paginator" max="<?= $instance['n'] ?>">
<?php
$n = 0;
foreach((array)$posts1 as $post): setup_postdata($post);
$n++;
if ( $n % 3 == 1) {
?> <div class="no-c-container<?php echo $n == 1 ? ' active' : '';?>"> <?php
}
?>
<article class="article thumblist no-c-paginator-item">
<!-- thumbnail -->
<?php echo the_post_thumbnail('cm_thumb', array('class' => 'img-thumbnail pull-left', 'style' => 'margin-right: 0.5em;')) ?>
<header>
<!-- terms -->
<?php if (!empty($terms) ): ?>
<p class="terms">
<?= $terms[0]['name'] ?>
</p>
<?php endif; ?>
<!-- title -->
<h3 class="title"><?php echo apply_filters('the_title', the_title()); ?></h3>
<!-- Time -->
<div class="date-author">
<?php include(locate_template('UI/byline.php')); ?>
<span class="topic"><?php echo cm_get_single_topic() ?></span>
</div>
</header>
<div class="clearfix"></div><!-- to clear the thumbnail's left floating -->
</article>
<?php
if ( $n % 3 == 0) {
?> </div> <?php
}
endforeach; wp_reset_postdata();
if ( $n % 3 != 0) {
?> </div> <?php
}
?>
<?php if($readmore): ?>
<div class="actions">
<button href="<?php echo $readmore; ?>" class="btn btn-primary btn-xs">View More</button>
</div>
<?php endif; ?>
</div>
<div class="actions">
<button class="btn btn-primary prev-item" disabled="disabled"><i class="fa fa-chevron-circle-left"></i> Prev</button>
<button class="btn btn-primary next-item">Next <i class="fa fa-chevron-circle-right"></i></button>
</div>
</div>
<div class="tab-pane" id="tab-pane-2">
<div class="post-list no-c-paginator" max="<?= $instance['n'] ?>">
<?php
$n = 0;
foreach((array)$posts2 as $post):
$n++;
if ( $n % 3 == 1) {
?> <div class="no-c-container<?php echo $n == 1 ? ' active' : '';?> "> <?php
}
?>
<article class="article thumblist no-c-paginator-item">
<!-- thumbnail -->
<?php echo get_the_post_thumbnail($post->ID, 'cm_thumb', array('class' => 'img-thumbnail pull-left', 'style' => 'margin-right: 0.5em;')) ?>
<header>
<!-- terms -->
<?php if (!empty($terms) ): ?>
<p class="terms">
<?= $terms[0]['name'] ?>
</p>
<?php endif; ?>
<!-- title -->
<h3 class="title"><?php echo apply_filters('the_title', $post->post_title); ?></h3>
<!-- Time -->
<div class="date-author">
<time datetime="<?php the_time('c'); ?>" pubdate="pubdate" ><?php the_time('F j, Y'); ?></time>
<?php include(locate_template('UI/byline.php')); ?>
<span class="topic"><?php echo cm_get_single_topic() ?></span>
</div>
</header>
<div class="clearfix"></div><!-- to clear the thumbnail's left floating -->
</article>
<?php
if ( $n % 3 == 0) {
?> </div> <?php
}
endforeach;
if ( $n % 3 != 0) {
?> </div> <?php
}
?>
<?php //if($readmore): ?>
<div class="actions">
View More
</div>
<?php //endif; ?>
<!--<div class="actions">
<button class="btn btn-primary prev-item" disabled="disabled"><i class="fa fa-chevron-circle-left"></i> Prev</button>
<button class="btn btn-primary next-item">Next <i class="fa fa-chevron-circle-right"></i></button>
</div>-->
</div>
</div>
</div>
</div>
</div>
<?php
echo $args['after_widget'];
}
public function form( $instance ) {
$instance['tab_1_title'] = isset($instance['tab_1_title'])?$instance['tab_1_title']:'Tab 1';
$instance['tab_2_title'] = isset($instance['tab_2_title'])?$instance['tab_2_title']:'Tab 2';
$instance['n'] = isset($instance['n'])?$instance['n']:4;
$sl_posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'storylist'
));
?>
<h5>Tab 1</h5>
<p>
<label for="<?php echo $this->get_field_id( 'tab_1_title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'tab_1_title' ); ?>" name="<?php echo $this->get_field_name( 'tab_1_title' ); ?>" type="text" value="<?php echo esc_attr( $instance['tab_1_title'] ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'storylist_id_1' ); ?>"><?php _e( 'Story List:' ); ?></label>
<select id="<?php echo $this->get_field_id( 'storylist_id_1' ); ?>" name="<?php echo $this->get_field_name( 'storylist_id_1' ); ?>">
<?php
if (count($sl_posts)) {
foreach($sl_posts as $storyList) {
$selected = ($instance['storylist_id_1'] == $storyList->ID ? "selected='selected'" : "");
echo "<option value='{$storyList->ID}' {$selected}>{$storyList->post_title}</option>";
}
} else {
echo "<option value='0'>"._('No Story Lists Found')."</option>";
}
?>
</select>
</p>
<!-- Number of items to show -->
<p>
<label for="<?php echo $this->get_field_id( 'n' ); ?>"><?php _e( 'Number of items to show at once:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'n' ); ?>" name="<?php echo $this->get_field_name( 'n' ); ?>" type="number" min="0" step="1" value="<?php echo intval($instance['n']) ?>">
</p>
<h5>Tab 2</h5>
<p>
<label for="<?php echo $this->get_field_id( 'tab_2_title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'tab_2_title' ); ?>" name="<?php echo $this->get_field_name( 'tab_2_title' ); ?>" type="text" value="<?php echo esc_attr( $instance['tab_2_title'] ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'storylist_id_2' ); ?>"><?php _e( 'Story List:' ); ?></label>
<select id="<?php echo $this->get_field_id( 'storylist_id_2' ); ?>" name="<?php echo $this->get_field_name( 'storylist_id_2' ); ?>">
<?php
if (count($sl_posts)) {
foreach($sl_posts as $storyList) {
$selected = ($instance['storylist_id_2'] == $storyList->ID ? "selected='selected'" : "");
echo "<option value='{$storyList->ID}' {$selected}>{$storyList->post_title}</option>";
}
} else {
echo "<option value='0'>"._('No Story Lists Found')."</option>";
}
?>
</select>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['tab_1_title'] = isset($new_instance['tab_1_title'])?$new_instance['tab_1_title']:'Tab 1';
$instance['tab_2_title'] = isset($new_instance['tab_2_title'])?$new_instance['tab_2_title']:'Tab 2';
$instance['storylist_id_1'] = isset($new_instance['storylist_id_1']) ? (int)$new_instance['storylist_id_1'] : 0;
$instance['storylist_id_2'] = isset($new_instance['storylist_id_2']) ? (int)$new_instance['storylist_id_2'] : 0;
$instance['n'] = isset($new_instance['n']) ? (int)$new_instance['n'] : 4;
return $instance;
}
}
add_action( 'widgets_init', function(){ register_widget( 'ChemTabs' ); });
I created a page template specifically for this page, but it just comes up blank. Here is the code for that page:
<?php
/*
Template Name: Custom StoryList Archives
*/
?>
<?php get_header(); ?>
<?php
if ( is_single(210639) ) {
$args = array( 'post_type' => 'storylist', 'posts_per_page' => 30 );
}
endif();
?>
<?php get_footer(); ?>
What am I missing????
I solved this by adding this code: $list_args2 = array( "post_type" => "any", "post__in" => get_post_meta( 210639, 'storylist', true), "orderby" => "post__in", 'post_status' => 'publish' );
I have a custom post type called storylist, which is done as a plugin for a sidebar widget.The custom post type story list compiles lists of other custom post types and displays them in the sidebar. The same way the posts are displaying in the sidebar, I need to have that same list, which in this case is "hot topics" display on a page when the "view more" button is clicked. The best I got was an archive of all the storylist posts but when you click on them, such as hot topics, it take you to the url http://site/storylist/hot-topics , which is the correct url, but it's blank and only shows the title of the page. Any suggestions on how to make those posts appear on hot topics?
Here's the code to make it display in the widget:
<?php
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;
class ChemTabs extends WP_Widget {
function __construct() {
parent::__construct(
'tabs', _('Tabs Widget'),
array(
'description' => _('A tabs widget.'),
));
}
private function ce_getPostClass($style, $index) {
switch ($style) {
case 'first-up':
if ( $index == 0 ) return 'first-up';
else return 'list-type';
break;
default:
return $style;
}
}
private function ce_has($listStyle, $index, $post) {
$class = $this->ce_getPostClass($listStyle, $index);
$has = array(
'thumb' => false,
'terms' => true,
'date' => true,
'content' => false,
'excerpt' => false,
'title' => true,
'video' => false,
'webinarDate' => false
);
switch ($class) {
case 'first-up':
$has['thumb'] = true;
$has['excerpt'] = true;
break;
case 'list-type':
$has['terms'] = false;
break;
case 'thumblist':
$has['thumb'] = true;
break;
case 'single': // no need for this anymore probably
$has['content'] = true;
break;
}
switch ($post->post_type) {
case 'video':
$has['video'] = true;
break;
case 'webinar':
$has['webinarDate'] = true;
break;
}
return $has;
}
public function widget( $args, $instance ) {
global $post;
$instance['tab_1_title'] = isset($instance['tab_1_title'])?$instance['tab_1_title']:'Tab 1';
$instance['tab_2_title'] = isset($instance['tab_2_title'])?$instance['tab_2_title']:'Tab 2';
$instance['storylist_id_1'] = isset($instance['storylist_id_1']) ? $instance['storylist_id_1'] : '';
$instance['storylist_id_2'] = isset($instance['storylist_id_2']) ? $instance['storylist_id_2'] : '';
$instance['n'] = isset($instance['n']) ? $instance['n'] : 4;
$title = isset($instance['title'])?$instance['title']:'';
$type = isset($instance['type'])?$instance['type']:'color';
$class = isset($instance['class'])?$instance['class']:'brand-primary';
// Post list configuration
// Note: posts temporarily rendered with ajax
$list_style = isset($instance['listStyle'])?$instance['listStyle']:'thumblist';
$list_post_type = isset($instance['post_type'])?$instance['post_type']:'post';
$list_filter = isset($instance['filter'])?$instance['filter']:'{}';
$list_display = isset($instance['display'])?$instance['display']:5;
$list_channel = isset($instance['channel'])?$instance['channel']:0;
$list_division = isset($instance['division'])?$instance['division']:0;
$readmore = isset($instance['readmore'])?$instance['readmore'] : '';
$list_args1 = array( "post_type" => "any", "post__in" => get_post_meta($instance['storylist_id_1'], 'storylist', true), "orderby" => "post__in" );
$posts1 = new WP_Query();
$posts1 = $posts1->query($list_args1);
$list_args2 = array( "post_type" => "any", "post__in" => get_post_meta($instance['storylist_id_2'], 'storylist', true), "orderby" => "post__in" );
$posts2 = new WP_Query();
$posts2 = $posts2->query($list_args2);
// Note: posts temporarily rendered with ajax
echo $args['before_widget'];
$instance['tab_2_title'] = get_the_title($instance['storylist_id_2']);
//#TODO: Use get_template_part('content/post', $modifier); on tabs too.
?>
<div class="module tabs">
<div class="striped striped-gray">
<!-- Nav tabs -->
<ul class="nav nav-tabs nav-justified" role="tablist">
<li class="active">
<a href="#tab-pane-1" role="tab" data-toggle="tab">
<h2 class="h4"><?= $instance['tab_1_title'] ?></h2>
</a>
</li>
<li>
<a href="#tab-pane-2" role="tab" data-toggle="tab">
<h2 class="h4"><?= $instance['tab_2_title'] ?></h2>
</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane active" id="tab-pane-1">
<div class="post-list no-c-paginator" max="<?= $instance['n'] ?>">
<?php
$n = 0;
foreach((array)$posts1 as $post): setup_postdata($post);
$n++;
if ( $n % 3 == 1) {
?> <div class="no-c-container<?php echo $n == 1 ? ' active' : '';?>"> <?php
}
?>
<article class="article thumblist no-c-paginator-item">
<!-- thumbnail -->
<?php echo the_post_thumbnail('cm_thumb', array('class' => 'img-thumbnail pull-left', 'style' => 'margin-right: 0.5em;')) ?>
<header>
<!-- terms -->
<?php if (!empty($terms) ): ?>
<p class="terms">
<?= $terms[0]['name'] ?>
</p>
<?php endif; ?>
<!-- title -->
<h3 class="title"><?php echo apply_filters('the_title', the_title()); ?></h3>
<!-- Time -->
<div class="date-author">
<?php include(locate_template('UI/byline.php')); ?>
<span class="topic"><?php echo cm_get_single_topic() ?></span>
</div>
</header>
<div class="clearfix"></div><!-- to clear the thumbnail's left floating -->
</article>
<?php
if ( $n % 3 == 0) {
?> </div> <?php
}
endforeach; wp_reset_postdata();
if ( $n % 3 != 0) {
?> </div> <?php
}
?>
<?php if($readmore): ?>
<div class="actions">
<button href="<?php echo $readmore; ?>" class="btn btn-primary btn-xs">View More</button>
</div>
<?php endif; ?>
</div>
<div class="actions">
<button class="btn btn-primary prev-item" disabled="disabled"><i class="fa fa-chevron-circle-left"></i> Prev</button>
<button class="btn btn-primary next-item">Next <i class="fa fa-chevron-circle-right"></i></button>
</div>
</div>
<div class="tab-pane" id="tab-pane-2">
<div class="post-list no-c-paginator" max="<?= $instance['n'] ?>">
<?php
$n = 0;
foreach((array)$posts2 as $post):
$n++;
if ( $n % 3 == 1) {
?> <div class="no-c-container<?php echo $n == 1 ? ' active' : '';?> "> <?php
}
?>
<article class="article thumblist no-c-paginator-item">
<!-- thumbnail -->
<?php echo get_the_post_thumbnail($post->ID, 'cm_thumb', array('class' => 'img-thumbnail pull-left', 'style' => 'margin-right: 0.5em;')) ?>
<header>
<!-- terms -->
<?php if (!empty($terms) ): ?>
<p class="terms">
<?= $terms[0]['name'] ?>
</p>
<?php endif; ?>
<!-- title -->
<h3 class="title"><?php echo apply_filters('the_title', $post->post_title); ?></h3>
<!-- Time -->
<div class="date-author">
<time datetime="<?php the_time('c'); ?>" pubdate="pubdate" ><?php the_time('F j, Y'); ?></time>
<?php include(locate_template('UI/byline.php')); ?>
<span class="topic"><?php echo cm_get_single_topic() ?></span>
</div>
</header>
<div class="clearfix"></div><!-- to clear the thumbnail's left floating -->
</article>
<?php
if ( $n % 3 == 0) {
?> </div> <?php
}
endforeach;
if ( $n % 3 != 0) {
?> </div> <?php
}
?>
<?php //if($readmore): ?>
<div class="actions">
View More
</div>
<?php //endif; ?>
<!--<div class="actions">
<button class="btn btn-primary prev-item" disabled="disabled"><i class="fa fa-chevron-circle-left"></i> Prev</button>
<button class="btn btn-primary next-item">Next <i class="fa fa-chevron-circle-right"></i></button>
</div>-->
</div>
</div>
</div>
</div>
</div>
<?php
echo $args['after_widget'];
}
public function form( $instance ) {
$instance['tab_1_title'] = isset($instance['tab_1_title'])?$instance['tab_1_title']:'Tab 1';
$instance['tab_2_title'] = isset($instance['tab_2_title'])?$instance['tab_2_title']:'Tab 2';
$instance['n'] = isset($instance['n'])?$instance['n']:4;
$sl_posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'storylist'
));
?>
<h5>Tab 1</h5>
<p>
<label for="<?php echo $this->get_field_id( 'tab_1_title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'tab_1_title' ); ?>" name="<?php echo $this->get_field_name( 'tab_1_title' ); ?>" type="text" value="<?php echo esc_attr( $instance['tab_1_title'] ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'storylist_id_1' ); ?>"><?php _e( 'Story List:' ); ?></label>
<select id="<?php echo $this->get_field_id( 'storylist_id_1' ); ?>" name="<?php echo $this->get_field_name( 'storylist_id_1' ); ?>">
<?php
if (count($sl_posts)) {
foreach($sl_posts as $storyList) {
$selected = ($instance['storylist_id_1'] == $storyList->ID ? "selected='selected'" : "");
echo "<option value='{$storyList->ID}' {$selected}>{$storyList->post_title}</option>";
}
} else {
echo "<option value='0'>"._('No Story Lists Found')."</option>";
}
?>
</select>
</p>
<!-- Number of items to show -->
<p>
<label for="<?php echo $this->get_field_id( 'n' ); ?>"><?php _e( 'Number of items to show at once:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'n' ); ?>" name="<?php echo $this->get_field_name( 'n' ); ?>" type="number" min="0" step="1" value="<?php echo intval($instance['n']) ?>">
</p>
<h5>Tab 2</h5>
<p>
<label for="<?php echo $this->get_field_id( 'tab_2_title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'tab_2_title' ); ?>" name="<?php echo $this->get_field_name( 'tab_2_title' ); ?>" type="text" value="<?php echo esc_attr( $instance['tab_2_title'] ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'storylist_id_2' ); ?>"><?php _e( 'Story List:' ); ?></label>
<select id="<?php echo $this->get_field_id( 'storylist_id_2' ); ?>" name="<?php echo $this->get_field_name( 'storylist_id_2' ); ?>">
<?php
if (count($sl_posts)) {
foreach($sl_posts as $storyList) {
$selected = ($instance['storylist_id_2'] == $storyList->ID ? "selected='selected'" : "");
echo "<option value='{$storyList->ID}' {$selected}>{$storyList->post_title}</option>";
}
} else {
echo "<option value='0'>"._('No Story Lists Found')."</option>";
}
?>
</select>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['tab_1_title'] = isset($new_instance['tab_1_title'])?$new_instance['tab_1_title']:'Tab 1';
$instance['tab_2_title'] = isset($new_instance['tab_2_title'])?$new_instance['tab_2_title']:'Tab 2';
$instance['storylist_id_1'] = isset($new_instance['storylist_id_1']) ? (int)$new_instance['storylist_id_1'] : 0;
$instance['storylist_id_2'] = isset($new_instance['storylist_id_2']) ? (int)$new_instance['storylist_id_2'] : 0;
$instance['n'] = isset($new_instance['n']) ? (int)$new_instance['n'] : 4;
return $instance;
}
}
add_action( 'widgets_init', function(){ register_widget( 'ChemTabs' ); });
Is there any way I can make a page template displaying the page id? I tried to do that but it didn't display anything.
I solved this by adding this code $list_args2 = array( "post_type" => "any", "post__in" => get_post_meta( 210639, 'storylist', true), "orderby" => "post__in", 'post_status' => 'publish' );
I have a wordpress theme with a widget named 'My - Post Cycle', which allows me to post entries from my blog. I want to be able to select which post 'categories' are displayed with the widget. Having the 'category ID', how can I edit the widget PHP to only show the category IDs I choose. Here is the code:
<?php
// =============================== My Post Cycle widget ======================================
class MY_CycleWidget extends WP_Widget {
/** constructor */
function MY_CycleWidget() {
parent::WP_Widget(false, $name = 'My - Post Cycle');
}
/** #see WP_Widget::widget */
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$limit = apply_filters('widget_limit', $instance['limit']);
$category = apply_filters('widget_category', $instance['category']);
$count = apply_filters('widget_count', $instance['count']);
?>
<?php echo $before_widget; ?>
<?php if ( $title )
echo $before_title . $title . $after_title; ?>
<?php if($category=="testi"){?>
<script type="text/javascript">
jQuery(function(){
jQuery('#slides').slides({
effect: 'fade',
crossfade: true,
preload: true,
generateNextPrev: true,
autoHeight: true
});
});
</script>
<div id="slides">
<div class="slides_container">
<?php $limittext = $limit;?>
<?php global $more; $more = 0;?>
<?php query_posts("posts_per_page=". $count ."&post_type=" . $category);?>
<?php while (have_posts()) : the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$testiname = $custom["my_testi_caption"][0];
$testiurl = $custom["my_testi_url"][0];
$testiinfo = $custom["my_testi_info"][0];
?>
<div class="testi_item">
<?php if($limittext=="" || $limittext==0){ ?>
<?php the_excerpt(); ?>
<?php }else{ ?>
<?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$limittext);?>
<?php } ?>
<div class="name-testi">
<span class="user"><?php echo $testiname; ?></span>,
<?php echo $testiurl; ?><br/>
<span class="info"><?php echo $testiinfo; ?></span>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
</div>
<!-- end of testimonials -->
<?php } elseif($category=="portfolio"){ ?>
<script type="text/javascript">
jQuery(function(){
jQuery('#slides').slides({
effect: 'fade',
crossfade: true,
preload: true,
generateNextPrev: true,
autoHeight: true
});
});
</script>
<div id="slides">
<div class="slides_container">
<?php $limittext = $limit;?>
<?php global $more; $more = 0;?>
<?php query_posts("posts_per_page=". $count ."&post_type=" . $category);?>
<?php while (have_posts()) : the_post(); ?>
<?php
$thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url( $thumb,'full'); //get img URL
$image = aq_resize( $img_url, 250, 150, true ); //resize & crop img
?>
<div class="item">
<?php if($limittext=="" || $limittext==0){ ?>
<figure class="featured-thumbnail">
<img src="<?php echo $image ?>" alt="<?php the_title(); ?>" />
</figure>
<?php }else{ ?>
<figure class="featured-thumbnail">
<img src="<?php echo $image ?>" alt="<?php the_title(); ?>" />
</figure>
<?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$limittext); } ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
</div>
<!-- end of portfolio_cycle -->
<?php } else { ?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#slider-code').tinycarousel({ axis: 'y'});
});
</script>
<div id="slider-code">
<a class="buttons prev" href="#">left</a>
<div class="viewport">
<ul class="overview">
<?php $limittext = $limit;?>
<?php global $more; $more = 0;?>
<?php query_posts("posts_per_page=" . $count . "&post_type=" . $category);?>
<?php while (have_posts()) : the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$period = $custom["period"][0];
?>
<li>
<?php if($limittext=="" || $limittext==0){ ?>
<h5><?php the_title(); ?></h5>
<b><?php echo $period; ?></b>
<em><?php _e('view details', 'theme1831'); ?></em>
<?php }else{ ?>
<h5><?php the_title(); ?></h5>
<b><?php echo $period; ?></b>
<div class="excerpt"><?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$limittext); ?></div>
<em><?php _e('view details', 'theme1831'); ?></em>
<?php } ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
</div>
<a class="buttons next" href="#">right</a>
</div>
<!-- end of post_cycle -->
<?php }?>
<?php echo $after_widget; ?>
<?php
}
/** #see WP_Widget::update */
function update($new_instance, $old_instance) {
return $new_instance;
}
/** #see WP_Widget::form */
function form($instance) {
$title = esc_attr($instance['title']);
$limit = esc_attr($instance['limit']);
$category = esc_attr($instance['category']);
$count = esc_attr($instance['count']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('Limit Text:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Posts per page:', 'theme1831'); ?><input class="widefat" style="width:30px; display:block; text-align:center" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Show profile link:', 'theme1831'); ?><br />
<select id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" style="width:150px;" >
<option value="testi" <?php echo ($category === 'testi' ? ' selected="selected"' : ''); ?>>Testimonials</option>
<option value="portfolio" <?php echo ($category === 'portfolio' ? ' selected="selected"' : ''); ?> >Portfolio</option>
<option value="" <?php echo ($category === '' ? ' selected="selected"' : ''); ?>>Blog</option>
</select>
</label></p>
<?php
}
} // class Cycle Widget
?>
Thank you for your help.
UPDATE:
I was unable to get the "My - Post Cycle" widget to work as I wanted it to, so I decided to use another one of the widgets that come with the theme ("My - Recent Post"). It doesn't look as nice as the other one, but it allows me to post posts off of one category. Maybe seeing the code, will allows someone to figure out how to make the first widget work like this one, but retain the style. Here is its code:
<?php
// =============================== My Recent Posts (News widget) ======================================
class MY_PostWidget extends WP_Widget {
/** constructor */
function MY_PostWidget() {
parent::WP_Widget(false, $name = 'My - Recent Posts');
}
/** #see WP_Widget::widget */
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$category = apply_filters('widget_category', $instance['category']);
$post_format = apply_filters('widget_post_format', $instance['post_format']);
$linktext = apply_filters('widget_linktext', $instance['linktext']);
$linkurl = apply_filters('widget_linkurl', $instance['linkurl']);
$count = apply_filters('widget_count', $instance['count']);
$sort_by = apply_filters('widget_sort_by', $instance['sort_by']);
$excerpt_count = apply_filters('widget_excerpt_count', $instance['excerpt_count']);
?>
<?php echo $before_widget; ?>
<?php if ( $title )
echo $before_title . $title . $after_title; ?>
<?php if($post_format == 'post-format-standard') {
$args = array(
'showposts' => $count,
'category_name' => $category,
'orderby' => $sort_by,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-aside', 'post-format-gallery', 'post-format-link', 'post-format-image', 'post-format-quote', 'post-format-audio', 'post-format-video'),
'operator' => 'NOT IN'
)
)
);
} else {
$args = array(
'showposts' => $count,
'category_name' => $category,
'orderby' => $sort_by,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array($post_format)
)
)
);
} ?>
<?php $wp_query = new WP_Query( $args ); ?>
<ul class="latestpost">
<?php if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();?>
<li class="clearfix">
<?php if(has_post_thumbnail()) { ?>
<?php
$thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url( $thumb,'full'); //get img URL
$image = aq_resize( $img_url, 100, 100, true ); //resize & crop img
?>
<figure class="featured-thumbnail">
<img src="<?php echo $image ?>" alt="<?php the_title(); ?>" />
</figure>
<?php } ?>
<time datetime="<?php the_time('Y-m-d\TH:i'); ?>"><?php the_time('F j, Y'); ?></time>
<h4><?php the_title(); ?></h4>
<?php if($excerpt_count!="") { ?>
<div class="excerpt">
<?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$excerpt_count);?>
</div>
<?php } ?>
<?php _e('Read more', 'theme1831'); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php $wp_query = null; $wp_query = $temp;?>
<!-- Link under post cycle -->
<?php if($linkurl !=""){?>
<?php echo $linktext; ?>
<?php } ?>
<?php echo $after_widget; ?>
<?php
}
/** #see WP_Widget::update */
function update($new_instance, $old_instance) {
return $new_instance;
}
/** #see WP_Widget::form */
function form($instance) {
$title = esc_attr($instance['title']);
$category = esc_attr($instance['category']);
$post_format = esc_attr($instance['post_format']);
$linktext = esc_attr($instance['linktext']);
$linkurl = esc_attr($instance['linkurl']);
$count = esc_attr($instance['count']);
$sort_by = esc_attr($instance['sort_by']);
$excerpt_count = esc_attr($instance['excerpt_count']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Category Slug:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" type="text" value="<?php echo $category; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('post_format'); ?>"><?php _e('Post format:', 'theme1831'); ?><br />
<select id="<?php echo $this->get_field_id('post_format'); ?>" name="<?php echo $this->get_field_name('post_format'); ?>" style="width:150px;" >
<option value="post-format-standard" <?php echo ($post_format === 'post-format-standard' ? ' selected="selected"' : ''); ?>>Standard</option>
<option value="post-format-aside" <?php echo ($post_format === 'post-format-aside' ? ' selected="selected"' : ''); ?>>Aside</option>
<option value="post-format-quote" <?php echo ($post_format === 'post-format-quote' ? ' selected="selected"' : ''); ?> >Quote</option>
<option value="post-format-link" <?php echo ($post_format === 'post-format-link' ? ' selected="selected"' : ''); ?> >Link</option>
<option value="post-format-image" <?php echo ($post_format === 'post-format-image' ? ' selected="selected"' : ''); ?> >Image</option>
<option value="post-format-gallery" <?php echo ($post_format === 'post-format-gallery' ? ' selected="selected"' : ''); ?> >Gallery</option>
<option value="post-format-audio" <?php echo ($post_format === 'post-format-audio' ? ' selected="selected"' : ''); ?> >Audio</option>
<option value="post-format-video" <?php echo ($post_format === 'post-format-video' ? ' selected="selected"' : ''); ?> >Video</option>
</select>
</label></p>
<p><label for="<?php echo $this->get_field_id('sort_by'); ?>"><?php _e('Post order:', 'theme1831'); ?><br />
<select id="<?php echo $this->get_field_id('sort_by'); ?>" name="<?php echo $this->get_field_name('sort_by'); ?>" style="width:150px;" >
<option value="date" <?php echo ($sort_by === 'date' ? ' selected="selected"' : ''); ?>>Date</option>
<option value="title" <?php echo ($sort_by === 'title' ? ' selected="selected"' : ''); ?>>Title</option>
<option value="comment_count" <?php echo ($sort_by === 'comment_count' ? ' selected="selected"' : ''); ?>>Comment Count</option>
<option value="rand" <?php echo ($sort_by === 'rand' ? ' selected="selected"' : ''); ?>>Random</option>
</select>
</label></p>
<p><label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Posts per page:'); ?><input class="widefat" style="width:30px; display:block; text-align:center" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('excerpt_count'); ?>"><?php _e('Excerpt length (words):'); ?><input class="widefat" style="width:30px; display:block; text-align:center" id="<?php echo $this->get_field_id('excerpt_count'); ?>" name="<?php echo $this->get_field_name('excerpt_count'); ?>" type="text" value="<?php echo $excerpt_count; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('linktext'); ?>"><?php _e('Link Text:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('linktext'); ?>" name="<?php echo $this->get_field_name('linktext'); ?>" type="text" value="<?php echo $linktext; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('linkurl'); ?>"><?php _e('Link Url:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('linkurl'); ?>" name="<?php echo $this->get_field_name('linkurl'); ?>" type="text" value="<?php echo $linkurl; ?>" /></label></p>
<?php
}
} // class Widget
?>
You should change the parameter post_type with category_name to the query_posts() function called inside the widget method.
<?php query_posts("posts_per_page=". $count . "&category_name=" . $category); ?>
Reference: WP_Query Category Parameters
My widget is showingmin my admin area but when I click it so that it can drop down so that I may enter information... it only shows the DELETE link, CLOSE link and SAVE button. the widget will not expand. I believe I am missing something in the script for the textarea causing the widget not to function properly.
<?php
if( !class_exists('smWidget') ):
class smWidget extends WP_Widget {
// constructor
function smWidget() {
// Give widget name here
$widget_ops = array('description' => __('Displays a selected post from a selected post type.', 'ci_theme'));
parent::WP_Widget('smWidget', $name='-= SM Item =-', $widget_ops);
}
// display widget
function widget($args, $instance) {
extract( $args );
$ci_post_id = $instance['postid'];
$post_type_name = $instance['post_type_name'];
if( empty($ci_post_id) or empty($post_type_name) )
return;
$q = new WP_Query( array(
'post_type' => $post_type_name,
'p' => $ci_post_id
) );
echo $before_widget;
$title = apply_filters( 'widget_title', empty( $instance['title']) ? '' : $instance['title'], $instance, $this->id_base );
if ($title)
echo $before_title . $title . $after_title;
while ( $q->have_posts() ): $q->the_post();
global $post;
if ( get_post_type() == 'testimonial' ) :
?>
<div class="testimonial-wrap">
<div class="testimonial-inner">
<blockquote> <i class="fa fa-quote-left"></i>
<?php the_content(); ?>
<cite>
<?php the_title(); ?>
</cite> </blockquote>
</div>
</div>
<?php
elseif ( get_post_type() == 'video' ) :
?>
<div <?php post_class('item'); ?>>
<?php
$video = get_post_meta($post->ID, 'ci_cpt_video_url', true);
echo wp_oembed_get($video);
?>
</div>
<?php
else :
get_template_part('loop', 'item');
endif;
endwhile;
wp_reset_postdata();
echo $after_widget;
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = sanitize_text_field($new_instance['title']);
$instance['post_type_name'] = sanitize_key($new_instance['post_type_name']);
$instance['postid'] = absint($new_instance['postid']);
$instance['teatarea'] = absint($new_instance['textarea']);
return $instance;
}
function form($instance){
$defaults = array(
'title' => '',
'post_type_name' => 'post',
'postid' => '',
'textarea' => '',
);
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
$post_type_name = $instance['post_type_name'];
$post_id = $instance['postid'];
$textarea = $instance['textarea'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">
<?php _e('Title (optional):', 'ci_theme'); ?>
</label>
<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" class="widefat" />
</p>
<?php
$post_types = get_post_types( array('public' => true), 'objects' );
unset($post_types['attachment'], $post_types['slider']);
?>
<p>
<label for="<?php echo $this->get_field_id('post_type_name'); ?>">
<?php _e('Show posts from this post type:', 'ci_theme'); ?>
<label>
</p>
<?php ?>
<select name="<?php echo $this->get_field_name('post_type_name'); ?>" id="<?php echo $this->get_field_id('post_type_name'); ?>" >
<?php
foreach( $post_types as $key => $pt )
{
?>
<option value="<?php echo esc_attr($key); ?>" <?php selected($key, $post_type_name); ?>><?php echo $pt->labels->name; ?></option>
<?php
}
?>
</select>
<img src="<?php echo get_child_or_parent_file_uri('/panel/img/ajax-loader-16x16.gif'); ?>" class="loading_posts" style="display: none;">
<?php
?>
<p></p>
<?php
?>
<p>
<label for="<?php echo $this->get_field_id('postid'); ?>">
<?php _e('Select a post to show:', 'ci_theme'); ?>
</label>
</p>
<?php
?>
<p class="ci_widget_post_type_posts_dropdown">
<?php
wp_dropdown_posts(
array(
'post_type' => $post_type_name,
'show_option_none' => ' ',
'selected' => $post_id,
'class' => 'widefat'
),
$this->get_field_name('postid')
);
?>
</p>
<?php
?>
<p>
<label for="<?php echo $this->get_field_id('textarea'); ?>">
<?php _e('Description:', 'ci_theme'); ?>
</label>
<textarea class="widefat" id="<?php echo $this->get_field_id('textarea'); ?>" name="<?php echo $this->get_field_name('textarea'); ?>" rows="7" cols="20" ><?php echo $textarea; ?></textarea>
</p>
<?php
}
static function _ajax_get_posts()
{
$post_type_name = sanitize_key($_POST['post_type_name']);
$name_field = esc_attr($_POST['name_field']);
$str = wp_dropdown_posts(
array(
'echo' => false,
'post_type' => $post_type_name,
'show_option_none' => ' ',
'class' => 'widefat'
),
$name_field
);
echo $str;
die;
}
} // class
register_widget('smWidget');
add_action('wp_ajax_ci_widget_post_type_ajax_get_posts', 'smWidget::_ajax_get_posts');
endif; // !class_exists
?>