• Resolved Peyton

    (@peytongregory)


    So here’s the widget code I’m using to display posts. Even though I changed the $sort_by to title from date it’s still loading posts in order by date. Anyone know what I need to do to load them alphabetically?

    <?php class CategoryPosts extends WP_Widget {
    
    	function __construct() {
    		$widget_ops = array('classname' => 'cat-post-widget', 'description' => __('List single category posts'));
    		parent::__construct('category-posts', __('Category Posts'), $widget_ops);
    	}
    
    	// Displays category posts widget on blog.
    	function widget($args, $instance) {
    		global $post;
    		$post_old = $post; // Save the post object.
    
    		extract( $args );
    
    		$sizes = get_option('mkrdip_cat_post_thumb_sizes');
    
    		// If not title, use the name of the category.
    		if( !$instance["title"] ) {
    			$category_info = get_category($instance["cat"]);
    			$instance["title"] = $category_info->name;
    		}
    
    		$valid_sort_orders = array('date', 'title', 'comment_count', 'rand');
    		if ( in_array($instance['sort_by'], $valid_sort_orders) ) {
    			$sort_by = $instance['sort_by'];
    			$sort_order = (bool) isset( $instance['asc_sort_order'] ) ? 'ASC' : 'DESC';
    		} else {
    			// by default, display latest first
    			$sort_by = 'title';
    			$sort_order = 'DESC';
    		}
    
    		// Get array of post info.
    		$cat_posts = new WP_Query(
    			"showposts=" . $instance["num"] .
    			"&cat=" . $instance["cat"] .
    			"&orderby=" . $sort_by .
    			"&order=" . $sort_order
    		);
    
    		// Excerpt length filter
    		$new_excerpt_length = create_function('$length', "return " . $instance["excerpt_length"] . ";");
    		if ( $instance["excerpt_length"] > 0 )
    			add_filter('excerpt_length', $new_excerpt_length);
    
    		echo $before_widget;
    
    		// Widget title
    		if( !isset ( $instance["hide_title"] ) ) {
    			echo $before_title;
    			if( isset ( $instance["title_link"] ) ) {
    				echo '<a href="' . get_category_link($instance["cat"]) . '">' . $instance["title"] . '</a>';
    			} else {
    				echo $instance["title"];
    			}
    			echo $after_title;
    		}
    
    		// Post list
    		echo "<ul>\n";
    
    		while ( $cat_posts->have_posts() )
    		{
    			$cat_posts->the_post(); ?>
Viewing 1 replies (of 1 total)
  • Thread Starter Peyton

    (@peytongregory)

    Never mind, I figured it out.

    class CategoryPosts extends WP_Widget {
    
    	function __construct() {
    		$widget_ops = array('classname' => 'cat-post-widget', 'description' => __('List single category posts'));
    		parent::__construct('category-posts', __('Category Posts'), $widget_ops);
    	}
    
    	// Displays category posts widget on blog.
    	function widget($args, $instance) {
    		global $post;
    		$post_old = $post; // Save the post object.
    
    		extract( $args );
    
    		$sizes = get_option('mkrdip_cat_post_thumb_sizes');
    
    		// If not title, use the name of the category.
    		if( !$instance["title"] ) {
    			$category_info = get_category($instance["cat"]);
    			$instance["title"] = $category_info->name;
    		}
    
    		$valid_sort_orders = array('title');
    		if ( in_array($instance['sort_by'], $valid_sort_orders) ) {
    			$sort_by = "title";
    			$sort_order = (bool) isset( $instance['asc_sort_order'] ) ? 'ASC' : 'DESC';
    		} else {
    			// by default, display latest first
    			$sort_by = 'title';
    			$sort_order = 'DESC';
    		}
    
    		// Get array of post info.
    		$cat_posts = new WP_Query(
    			"showposts=" . $instance["num"] .
    			"&cat=" . $instance["cat"] .
    			"&orderby=" . $sort_by .
    			"&order=" . $sort_order
    		);
    
    		// Excerpt length filter
    		$new_excerpt_length = create_function('$length', "return " . $instance["excerpt_length"] . ";");
    		if ( $instance["excerpt_length"] > 0 )
    			add_filter('excerpt_length', $new_excerpt_length);
    
    		echo $before_widget;
    
    		// Widget title
    		if( !isset ( $instance["hide_title"] ) ) {
    			echo $before_title;
    			if( isset ( $instance["title_link"] ) ) {
    				echo '<a href="' . get_category_link($instance["cat"]) . '">' . $instance["title"] . '</a>';
    			} else {
    				echo $instance["title"];
    			}
    			echo $after_title;
    		}
    
    		// Post list
    		echo "<ul>\n";
    
    		while ( $cat_posts->have_posts() )
    		{
    			$cat_posts->the_post(); ?>
Viewing 1 replies (of 1 total)
  • The topic ‘How to sort posts in Alphabetical order’ is closed to new replies.