hiding post date in alx-post widget?
-
i like the hueman theme very much, but i still have to struggle a little with it:
i’ve spent hours on searching for a way to hide the post date when displaying it in the alx-post widget. i checked all backend settings tabs, i found out that i can uncheck to show the category of a post, but not its post date.
then i went into the theme files, but when i delete line 21 in content.php, nothing happens.
also i would like to show not only the post title, but also the whole text. therefore in theme options/blog i set the excerpt length to 100 but nothing happened. again, i changed the entry in the content.php on line 28 from 0 to 100, but somehow those changes are not executed.
-
okay..checking
Can you again paste the code of alx-posts.php here or in paste bin<?php /* AlxPosts Widget License: GNU General Public License v2.0 License URI: https://www.gnu.org/licenses/gpl-2.0.html Copyright: (c) 2013 Alexander "Alx" Agnarson - https://alxmedia.se @package AlxPosts @version 1.0 */ class AlxPosts extends WP_Widget { /* Constructor /* ------------------------------------ */ function AlxPosts() { parent::__construct( false, 'AlxPosts', array('description' => 'Display posts from a category', 'classname' => 'widget_alx_posts') );; } /* Widget /* ------------------------------------ */ public function widget($args, $instance) { extract( $args ); $instance['title']?NULL:$instance['title']=''; $title = apply_filters('widget_title',$instance['title']); $output = $before_widget."\n"; if($title) $output .= $before_title.$title.$after_title; ob_start(); ?> <?php $posts = new WP_Query( array( 'post_type' => array( 'post' ), 'showposts' => $instance['posts_num'], 'cat' => $instance['posts_cat_id'], 'ignore_sticky_posts' => true, 'orderby' => $instance['posts_orderby'], 'order' => 'dsc', 'date_query' => array( array( 'after' => $instance['posts_time'], ), ), ) ); ?> <ul class="alx-posts group <?php if($instance['posts_thumb']) { echo 'thumbs-enabled'; } ?>"> <?php while ($posts->have_posts()): $posts->the_post(); ?> <li> <?php if($instance['posts_thumb']) { // Thumbnails enabled? ?> <div class="post-item-thumbnail"> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php if ( has_post_thumbnail() ): ?> <?php the_post_thumbnail('thumb-medium'); ?> <?php else: ?> <img src="<?php echo get_template_directory_uri(); ?>/img/thumb-medium.png" alt="<?php the_title(); ?>" /> <?php endif; ?> <?php if ( has_post_format('video') && !is_sticky() ) echo'<span class="thumb-icon small"><i class="fa fa-play"></i></span>'; ?> <?php if ( has_post_format('audio') && !is_sticky() ) echo'<span class="thumb-icon small"><i class="fa fa-volume-up"></i></span>'; ?> <?php if ( is_sticky() ) echo'<span class="thumb-icon small"><i class="fa fa-star"></i></span>'; ?> </a> </div> <?php } ?> <div class="post-item-inner group"> <?php if($instance['posts_category']) { ?><p class="post-item-category"><?php the_category(' / '); ?></p><?php } ?> <p class="post-item-title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></p> <!-- <p class="post-item-date"><?php the_time('j M, Y'); ?></p> --> <p><?php the_excerpt();?></p> </div> </div> </li> <?php endwhile; ?> </ul><!--/.alx-posts--> <?php $output .= ob_get_clean(); $output .= $after_widget."\n"; echo $output; } /* Widget update /* ------------------------------------ */ public function update($new,$old) { $instance = $old; $instance['title'] = strip_tags($new['title']); // Posts $instance['posts_thumb'] = $new['posts_thumb']?1:0; $instance['posts_category'] = $new['posts_category']?1:0; $instance['posts_num'] = strip_tags($new['posts_num']); $instance['posts_cat_id'] = strip_tags($new['posts_cat_id']); $instance['posts_orderby'] = strip_tags($new['posts_orderby']); $instance['posts_time'] = strip_tags($new['posts_time']); return $instance; } /* Widget form /* ------------------------------------ */ public function form($instance) { // Default widget settings $defaults = array( 'title' => '', // Posts 'posts_thumb' => 1, 'posts_category' => 1, 'posts_num' => '4', 'posts_cat_id' => '0', 'posts_orderby' => 'date', 'posts_time' => '0', ); $instance = wp_parse_args( (array) $instance, $defaults ); ?> <style> .widget .widget-inside .alx-options-posts .postform { width: 100%; } .widget .widget-inside .alx-options-posts p { margin-bottom: 0.3em; } .widget .widget-inside .alx-options-posts hr { border: none; border-bottom: 2px solid #ddd; margin: 1em 0; } .widget .widget-inside .alx-options-posts h4 { margin-bottom:0.665em; } </style> <div class="alx-options-posts"> <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 echo esc_attr($instance["title"]); ?>" /> </p> <hr> <h4>List Posts</h4> <p> <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('posts_thumb'); ?>" name="<?php echo $this->get_field_name('posts_thumb'); ?>" <?php checked( (bool) $instance["posts_thumb"], true ); ?>> <label for="<?php echo $this->get_field_id('posts_thumb'); ?>">Show thumbnails</label> </p> <p> <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('posts_category'); ?>" name="<?php echo $this->get_field_name('posts_category'); ?>" <?php checked( (bool) $instance["posts_category"], true ); ?>> <label for="<?php echo $this->get_field_id('posts_category'); ?>">Show categories</label> </p> <p> <label style="width: 55%; display: inline-block;" for="<?php echo $this->get_field_id("posts_num"); ?>">Items to show</label> <input style="width:20%;" id="<?php echo $this->get_field_id("posts_num"); ?>" name="<?php echo $this->get_field_name("posts_num"); ?>" type="text" value="<?php echo absint($instance["posts_num"]); ?>" size='3' /> </p> <p> <label style="width: 100%; display: inline-block;" for="<?php echo $this->get_field_id("posts_cat_id"); ?>">Category:</label> <?php wp_dropdown_categories( array( 'name' => $this->get_field_name("posts_cat_id"), 'selected' => $instance["posts_cat_id"], 'show_option_all' => 'All', 'show_count' => true ) ); ?> </p> <p style="padding-top: 0.3em;"> <label style="width: 100%; display: inline-block;" for="<?php echo $this->get_field_id("posts_orderby"); ?>">Order by:</label> <select style="width: 100%;" id="<?php echo $this->get_field_id("posts_orderby"); ?>" name="<?php echo $this->get_field_name("posts_orderby"); ?>"> <option value="date"<?php selected( $instance["posts_orderby"], "date" ); ?>>Most recent</option> <option value="comment_count"<?php selected( $instance["posts_orderby"], "comment_count" ); ?>>Most commented</option> <option value="rand"<?php selected( $instance["posts_orderby"], "rand" ); ?>>Random</option> </select> </p> <p style="padding-top: 0.3em;"> <label style="width: 100%; display: inline-block;" for="<?php echo $this->get_field_id("posts_time"); ?>">Posts from:</label> <select style="width: 100%;" id="<?php echo $this->get_field_id("posts_time"); ?>" name="<?php echo $this->get_field_name("posts_time"); ?>"> <option value="0"<?php selected( $instance["posts_time"], "0" ); ?>>All time</option> <option value="1 year ago"<?php selected( $instance["posts_time"], "1 year ago" ); ?>>This year</option> <option value="1 month ago"<?php selected( $instance["posts_time"], "1 month ago" ); ?>>This month</option> <option value="1 week ago"<?php selected( $instance["posts_time"], "1 week ago" ); ?>>This week</option> <option value="1 day ago"<?php selected( $instance["posts_time"], "1 day ago" ); ?>>Past 24 hours</option> </select> </p> </div> <?php } } /* Register widget /* ------------------------------------ */ function alx_register_widget_posts() { register_widget( 'AlxPosts' ); } add_action( 'widgets_init', 'alx_register_widget_posts' );
Okay, so after following this thread a bit I’ve made some changes to the theme code, which I sneaky-added to 2.0.0 just now, here. The widgets are now loaded like this in functions.php:
/* Load custom widgets /* ------------------------------------ */ if ( ! function_exists( 'alx_widgets' ) ) { function alx_widgets() { load_template( get_template_directory() . '/functions/widgets/alx-tabs.php' ); load_template( get_template_directory() . '/functions/widgets/alx-video.php' ); load_template( get_template_directory() . '/functions/widgets/alx-posts.php' ); } add_action( 'after_setup_theme', 'alx_widgets' ); }
This means that you can create an identical function in your child theme’s functions.php (the default one will be disabled) and link to whatever directories you want, or unhook them completely.
thanks alexander,
i uploaded the 2.0.0 anew and added your above shown lines to the functions.php of my child theme – now it looks like that. but my widget still shows no excerpt and does not hide the date. did i edit the functions.php correctly?<?php add_action('wp_head','load_parent_style',0); function load_parent_style() { wp_register_style('parent-theme',get_bloginfo('template_directory').'/style.css'); wp_enqueue_style('parent-theme'); } /* Load custom widgets /* ------------------------------------ */ if ( ! function_exists( 'alx_widgets' ) ) { function alx_widgets() { load_template( get_template_directory() . '/functions/widgets/alx-tabs.php' ); load_template( get_template_directory() . '/functions/widgets/alx-video.php' ); load_template( get_template_directory() . '/functions/widgets/alx-posts.php' ); } add_action( 'after_setup_theme', 'alx_widgets' ); } ?>
So after some discussion with a www.remarpro.com theme reviewer, I’ve removed all the if ( ! function_exists() ) from callbacks that were added yesterday, as they can just be removed/replaced with remove_action or remove_filter instead. So it’s unnecessary.
pigsound, can you try one thing. First, re-download the latest again (sorry), and then simply edit the widget directly in the parent theme, and see if it works at all. I just want to see if your edits works without child theme replacement first.
done. the widget seems to work now – although there seems to go something slightly wrong: the first post looks ok, the following posts are styled differently because they are in a differerent list than the first post.
If it does work, then to do a widget-replacement via child theme, if I understand it correctly (I’m pretty new to child-themes), add something like this to your child-theme functions.php:
// Remove parent theme widget remove_action( 'widgets_init', 'alx_register_widget_posts' ); // Load child theme widget load_template( get_template_directory() . '/functions/widgets/alx-posts.php' );
Perhaps I’m misunderstanding the other guy, and you may need to change the widgets_init name for the child-theme widget at the bottom of the widget file. Not sure.
Edit: Didn’t see your reply. Nice that it works a bit at least. For the issue you are having, try removing the <p></p> tags around the excerpt, as they are auto-added by the excerpt I think. So right now it may result in double p tags, which breaks the markup.
i added the code you posted above into the functions.php with no effect.
i tried a variant (replaced “alx_register_widget_post” with “alx-posts.php”, following your suggestion):
// Remove parent theme widget remove_action( 'widgets_init', 'alx-posts.php' ); // Load child theme widget load_template( get_template_directory() . '/functions/widgets/alx-posts.php' );
both efforts to no effect. the widget ignores the template in the child theme directory and calls the template from the original theme.
should i try some other variant?
Try:
get_stylesheet_directory()
instead of:
get_template_directory()
ooh, that returns a blank page.
I see one thing now, you should not have touched this code:
// Remove parent theme widget remove_action( 'widgets_init', 'alx_register_widget_posts' );
That needs to be as it is, and not be renamed.
—
You can also test:
// Load child theme widget load_template( '/functions/widgets/alx-posts.php' );
–
And if that does not work, replace this in the child theme widget:
/* Register widget /* ------------------------------------ */ function alx_register_widget_posts() { register_widget( 'AlxPosts' ); } add_action( 'widgets_init', 'alx_register_widget_posts' );
With:
/* Register widget /* ------------------------------------ */ function alx_register_widget_posts_child() { register_widget( 'AlxPosts' ); } add_action( 'widgets_init', 'alx_register_widget_posts_child' );
another helper stated:
“if there’s a child theme, both functions.php are being executed!
that’s why one should use the following construction:
if ( ! function_exists( 'alx_widgets' ) ) {
only if the function does not exist yet, it will defined by that codebit.
so if you define the function in the child theme functions.php before, you can edit it as you wish.
this way the parent theme stays unchanged and can be updated.”(sorry for my baaad english…!)
I fully understand that – and that is why I did the changes I did last night. However, as the www.remarpro.com theme reviewer pointed out, the widgets are already “pluggable”, via remove_action, so adding that is duplicate functionality and unnecessary.
// Remove parent theme widget remove_action( 'widgets_init', 'alx_register_widget_posts' ); // Load child theme widget load_template( get_stylesheet_directory() . '/functions/widgets/alx-posts.php' );
Does the same thing kind of, as copying and overriding the function via a child theme. Or should do.
I think the reason you got a white page may have been because you had:
remove_action( 'widgets_init', 'alx-posts.php' );
no, the white page came when i entered a code you in the meantime have removed again. i have changed back the functions.php entry with no renaming before that already.
i tried your tips, and none of them caused any change. the widget is still looking like before.
what did you mean by that ?
Perhaps I’m misunderstanding the other guy, and you may need to change the widgets_init name for the child-theme widget at the bottom of the widget file. Not sure.
In /hueman-child/functions/widgets/alx-posts.php, on line 176 to 181, you will find:
/* Register widget /* ------------------------------------ */ function alx_register_widget_posts() { register_widget( 'AlxPosts' ); } add_action( 'widgets_init', 'alx_register_widget_posts' );
What I wanted you to try was simply replacing that code there above, with:
/* Register widget /* ------------------------------------ */ function alx_register_widget_posts_child() { register_widget( 'AlxPosts' ); } add_action( 'widgets_init', 'alx_register_widget_posts_child' );
- The topic ‘hiding post date in alx-post widget?’ is closed to new replies.