• mak1wp

    (@mak1wp)


    Hi,

    I’ve been working with a page builder plugin from site origin which is capable of listing a post loop (via widget) and arranging the posts in order of date, post ID, comment count and others but it does not list Post by Category.

    I’ve been trying to add this functionality but my PHP and wordpress skills aren’t strong enough. Are there any WP hero’s about who would find this easy to do and help me achieve this category specific listing of the post loop?

    I’ve included the code from the plugin which enables the post loop widget – which is missing the category select. Its a fairly long script as its showing all of the other options included in the post loop function.

    Many many thanks to anybody that replies!

    class SiteOrigin_Panels_Widgets_PostLoop extends WP_Widget{
    	function __construct() {
    		parent::__construct(
    			'siteorigin-panels-postloop',
    			__( 'Blog Posts', 'so-panels' ),
    			array(
    				'description' => __( 'Displays a post loop.', 'so-panels' ),
    			)
    		);
    	}
    
    	function widget( $args, $instance ) {
    		if(empty($instance['template'])) return;
    
    		$template = $instance['template'];
    		$query_args = $instance;
    		unset($query_args['template']);
    		unset($query_args['additional']);
    		unset($query_args['sticky']);
    
    		$query_args = wp_parse_args($instance['additional'], $query_args);
    
    		global $wp_query;
    		$query_args['paged'] = $wp_query->get('paged');
    
    		switch($instance['sticky']){
    			case 'ignore' :
    				$query_args['ignore_sticky_posts'] = 1;
    				break;
    			case 'only' :
    				$query_args['post__in'] = get_option( 'sticky_posts' );
    				break;
    			case 'exclude' :
    				$query_args['post__not_in'] = get_option( 'sticky_posts' );
    				break;
    		}
    
    		if ( !empty( $instance['title'] ) ) {
    			echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'];
    		}
    
    		// Create the query
    		query_posts($query_args);
    
    		echo $args['before_widget'];
    		locate_template($instance['template'], true, false);
    		echo $args['after_widget'];
    
    		// Reset everything
    		wp_reset_query();
    		wp_reset_postdata();
    	}
    
    	function update($new, $old){
    		return $new;
    	}
    
    	function get_loop_templates(){
    		$templates = array();
    
    		$files = glob(get_template_directory().'/columns*.php');
    		foreach($files as $file){
    			$templates[] = basename($file);
    		}
    		$files = glob(get_stylesheet_directory().'/columns*.php');
    		foreach($files as $file){
    			$templates[] = basename($file);
    		}
    		$templates = array_unique($templates);
    		sort($templates);
    
    		return $templates;
    	}
    
    	function form( $instance ) {
    		$instance = wp_parse_args($instance, array(
    			'title' => '',
    			'template' => 'loop.php',
    
    			// Query args
    			'post_type' => 'post',
    			'posts_per_page' => '',
    			'order' => 'DESC',
    			'orderby' => 'date',
    
    			'taxonomy'                 => 'category', // I've added this?!? thats my entire contribution here. 
    
    			'sticky' => '',
    			'additional' => '',
    		));
    
    		$templates = $this->get_loop_templates();
    		if(empty($templates)) {
    			?><p><?php _e("----", 'so-panels') ?></p><?php
    			return;
    		}
    
    		// Get all the loop template files
    		$post_types = get_post_types(array('public' => true));
    		$post_types = array_values($post_types);
    		$post_types = array_diff($post_types, array('attachment', 'revision', 'nav_menu_item'));
    
    		?>
    		<p>
    			<label for="<?php echo $this->get_field_id( 'title' ) ?>"><?php _e( 'Title', 'so-panels' ) ?></label>
    			<input class="widefat" name="<?php echo $this->get_field_name( 'title' ) ?>" id="<?php echo $this->get_field_id( 'title' ) ?>" value="<?php echo esc_attr( $instance['title'] ) ?>">
    		</p>
    		<p>
    			<label for="<?php echo $this->get_field_id('template') ?>"><?php _e('Template', 'so-panels') ?></label>
    			<select id="<?php echo $this->get_field_id( 'template' ) ?>" name="<?php echo $this->get_field_name( 'template' ) ?>">
    				<?php foreach($templates as $template) : ?>
    					<option value="<?php echo esc_attr($template) ?>" <?php selected($instance['template'], $template) ?>><?php echo esc_html($template) ?></option>
    				<?php endforeach; ?>
    			</select>
    		</p>
    		<p>
    			<label for="<?php echo $this->get_field_id('post_type') ?>"><?php _e('Post Type', 'so-panels') ?></label>
    			<select id="<?php echo $this->get_field_id( 'post_type' ) ?>" name="<?php echo $this->get_field_name( 'post_type' ) ?>" value="<?php echo esc_attr($instance['post_type']) ?>">
    				<?php foreach($post_types as $type) : ?>
    					<option value="<?php echo esc_attr($type) ?>" <?php selected($instance['post_type'], $type) ?>><?php echo esc_html($type) ?></option>
    				<?php endforeach; ?>
    			</select>
    		</p>
    
    		<p>
    			<label for="<?php echo $this->get_field_id('posts_per_page') ?>"><?php _e('Posts Per Page', 'so-panels') ?></label>
    			<input type="text" class="small-text" id="<?php echo $this->get_field_id( 'posts_per_page' ) ?>" name="<?php echo $this->get_field_name( 'posts_per_page' ) ?>" value="<?php echo esc_attr($instance['posts_per_page']) ?>" />
    		</p>
    
    		<p>
    			<label <?php echo $this->get_field_id('orderby') ?>><?php _e('Order By', 'so-panels') ?></label>
    			<select id="<?php echo $this->get_field_id( 'orderby' ) ?>" name="<?php echo $this->get_field_name( 'orderby' ) ?>" value="<?php echo esc_attr($instance['orderby']) ?>">
    				<option value="none" <?php selected($instance['orderby'], 'none') ?>><?php esc_html_e('None', 'so-panels') ?></option>
    				<option value="ID" <?php selected($instance['orderby'], 'ID') ?>><?php esc_html_e('Post ID', 'so-panels') ?></option>
    				<option value="author" <?php selected($instance['orderby'], 'author') ?>><?php esc_html_e('Author', 'so-panels') ?></option>
    				<option value="name" <?php selected($instance['orderby'], 'name') ?>><?php esc_html_e('Name', 'so-panels') ?></option>
    				<option value="name" <?php selected($instance['orderby'], 'name') ?>><?php esc_html_e('Name', 'so-panels') ?></option>
    				<option value="date" <?php selected($instance['orderby'], 'date') ?>><?php esc_html_e('Date', 'so-panels') ?></option>
    				<option value="modified" <?php selected($instance['orderby'], 'modified') ?>><?php esc_html_e('Modified', 'so-panels') ?></option>
    				<option value="parent" <?php selected($instance['orderby'], 'parent') ?>><?php esc_html_e('Parent', 'so-panels') ?></option>
    				<option value="rand" <?php selected($instance['orderby'], 'rand') ?>><?php esc_html_e('Random', 'so-panels') ?></option>
    				<option value="comment_count" <?php selected($instance['orderby'], 'comment_count') ?>><?php esc_html_e('Comment Count', 'so-panels') ?></option>
    				<option value="menu_order" <?php selected($instance['orderby'], 'menu_order') ?>><?php esc_html_e('Menu Order', 'so-panels') ?></option>
    				<option value="menu_order" <?php selected($instance['orderby'], 'menu_order') ?>><?php esc_html_e('Menu Order', 'so-panels') ?></option>
    			</select>
    		</p>
    
    		<p>
    			<label for="<?php echo $this->get_field_id('order') ?>"><?php _e('Order', 'so-panels') ?></label>
    			<select id="<?php echo $this->get_field_id( 'order' ) ?>" name="<?php echo $this->get_field_name( 'order' ) ?>" value="<?php echo esc_attr($instance['order']) ?>">
    				<option value="DESC" <?php selected($instance['order'], 'DESC') ?>><?php esc_html_e('Descending', 'so-panels') ?></option>
    				<option value="ASC" <?php selected($instance['order'], 'ASC') ?>><?php esc_html_e('Ascending', 'so-panels') ?></option>
    			</select>
    		</p>
    
    		<p>
    			<label for="<?php echo $this->get_field_id('sticky') ?>"><?php _e('Sticky Posts', 'so-panels') ?></label>
    			<select id="<?php echo $this->get_field_id( 'sticky' ) ?>" name="<?php echo $this->get_field_name( 'sticky' ) ?>" value="<?php echo esc_attr($instance['sticky']) ?>">
    				<option value="" <?php selected($instance['sticky'], '') ?>><?php esc_html_e('Default', 'so-panels') ?></option>
    				<option value="ignore" <?php selected($instance['sticky'], 'ignore') ?>><?php esc_html_e('Ignore Sticky', 'so-panels') ?></option>
    				<option value="exclude" <?php selected($instance['sticky'], 'exclude') ?>><?php esc_html_e('Exclude Sticky', 'so-panels') ?></option>
    				<option value="only" <?php selected($instance['sticky'], 'only') ?>><?php esc_html_e('Only Sticky', 'so-panels') ?></option>
    			</select>
    		</p>
    
    		<p>
    			<label for="<?php echo $this->get_field_id('additional') ?>"><?php _e('Additional ', 'so-panels') ?></label>
    			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'additional' ) ?>" name="<?php echo $this->get_field_name( 'additional' ) ?>" value="<?php echo esc_attr($instance['additional']) ?>" />
    			<small><?php printf(__('Additional query arguments. See <a href="%s" target="_blank">query_posts</a>.', 'so-panels'), 'http://codex.wordpress.org/Function_Reference/query_posts') ?></small>
    		</p>
    	<?php
    	}
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • The loop widget also gives you a field called “Additional”, which takes additional arguments for the WordPress query. If you add the following to the Additional field, it *should* only display posts from a given category

    category_name=my-category-slug

    You can get a category slug by navigating to Posts > Categories and copying the slug value from the category you want to display.

    I hope that helps, I still have quite a lot of work to do on the plugin’s documentation πŸ™‚

    Thread Starter mak1wp

    (@mak1wp)

    Greg Priday you sir, are a legend. That worked perfectly.

    Your plugin’s amazing man, really big well done and thanks for sharing!

    Best of luck with future theme and plugin developments and sales!

    Hi Greg,

    Just wonder if “page builder” only works under page template type= default?

    I used a child theme of “twenty-twelve” and a page with my self-defined type (“front-page”)…
    Then the effect in page builder won’t appear…

    But when I switch the page type to “default”, page builder effects appear…

    Also, I couldn’t delete the “twitter-timeline” that was inserted to one of the box inside my columns …

    Dunno if there’s anything wrong…

    Many thanks in advance …

    Page Builder’s content is outputted using the the_content() function, which is the standard way page templates display a page’s content. If a page template just echo’s $post->post_content (which is considered bad practice), then Page Builder wont work with that template.

    Simple solution if this is the case is to replace

    <?php echo $post->post_content ?>

    With

    <?php the_content() ?>

    And just as a friendly reminder, if you need to ask a question, rather start a new thread than join someone else’s. It’s neater that way πŸ™‚

    Many thanks, Greg…

    I am not familiar with the manner on tech forum as I am not a tech guy. Thanks for letting me know.

    I will try replacing the echo tag… (try figuring out which file to look for) with the WordPress function (“the_content”, not sure if I am right)…

    Anyway, really learn a lot.

    Your work is really great.

    Have a great day.

    Hi Greg!

    I am using your pitch premium theme and site builder plugin. Awesome work!

    I’m trying to use loop widget to display projects on a page that fit a certain skill.

    So here our my settings and it isn’t working:

    Template: loop-projects.php
    Post Type: project

    Additional: category_name=my-skill-slug

    Shows nothing but if I remove the additional it works fine.

    Hi Greg,

    I need to modify “Post Loop edit”:

    I need to add a select menu to add a “class” to the Title.

    Where I can find the file to edit to add a new menu in the “Edit Post loop (PB) Widget”?

    Thanks

    Raffaele

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Category select function within page builder post loop’ is closed to new replies.