Archived

This topic is now archived and is closed to further replies.

  • 0

Using a page as a category header!

Question

Posted · Report post

Oh dear, please forgive my ramblings on this post. I'm a designer, not much of a programmer, so if you can help out I'd be grateful! I'm trying to either embed a category into a page, or a page into a category. Either will serve the purpose of having a page be a contextual, editable introduction to the content listed in that category. :) Naturally, I'm running into some problems.

I've embedded post-clips from my Audiobook category into my Audiobook page, which acts as a header/introduction to the list of posts. This isn't the only category/page combo that I'm embedding post-clips into; I also have a Podcast page with Podcast category post-clips embedded into it. There are two plugins which would do what I'd like, but they each have their own drawbacks. To show the difference, I have each page using a different plugin. The plugin List Category Posts does exactly as its name implies; a list, and applied very easily with bracket code in the page content. It requires no editing of template files. For what it does, it works well, however, there's no pagination nor a simple template system to format the output into magazine style post-clips, nor does it support the display of the audio player (see next plugin). The plugin combo Page2Cat (sadly discontinued) and its child plugin Category Page Extender work together to link categories to pages, and it requires inserting code into template.postloop.php. Unfortunately, using this I've run into a bug where the output is duplicated. Template code:

<?php if( function_exists(page2cat_pages)){
	     page2cat_pages(get_the_ID(), 4, 0);
	} ?>
As you can see from the Podcast page, the plugins work, kindof. The category posts are doubling for some reason. Normally I'd create a new page template, but I wasn't absolutely sure how to do this with PlatformPro, so I inserted the code into the template.postloop.php file, here:
							<div class="entry_content">
									<?php
										the_content(__('<p>Continue reading ยป</p>','pagelines'));?>
										<div class="clear"></div>
										<?php if(is_single()) wp_link_pages(__('<p>Pages:', 'pagelines'), '</p>', __('number', 'pagelines'));
										$edit_type = (is_page()) ? __('Edit Page','pagelines') : __('Edit Post','pagelines');
										edit_post_link( '['.$edit_type.']', '', '');
										pagelines_register_hook( 'pagelines_loop_after_post_content', 'theloop' ); //hook
									?>
	
								<?php if( function_exists(page2cat_pages)){
	     page2cat_pages(get_the_ID());
	} ?>
	
								</div>[/code]

What I'd love to have happen is to see Pageline's magazine style post-clips be embeddable to pages with a category (or tag) filter. I can't do this perfectly with either plugin. I [b]think[/b] it might be possible to format it this way with the List Category Post plugin with its template files, so I'll be plucking at it for a while until my little knowledge of php wears thin. ;) A new feature idea? :) A text-content or page template section that can be defined to show specific categories or tags with the ability to attach features/boxes/highlights/or even pages in its content, as the current templates allow to do. I'd love to be able to get this to work, please chip in if you have a clue how this can be made possible. :)

Share this post


Link to post
Share on other sites

5 answers to this question

Posted · Report post

Okay, with some effort I patched together a solution which I'll share here for everyone who ever wanted to put a category into a page. My coding's pretty messy, and I won't claim to know PHP, but it worked for me. :) Plugin used: List Category Posts (modified) Example: Podcasts page (using Pagelines Pro) From here on I'll refer to List Category Posts as LCP since it's kind of a handful to type all the time. LCP has 4 php files that direct the plugin: lcp_widget_form.php list_cat_posts.php list_cat_posts_widget.php templates/default.php I only modified two files, list_cat_posts.php and default.php. default.php is an example of LCP's template system, see the plugin site for more information about this. LCP had a couple of bugs that I had to sort through before I could make it work the way I wanted it to, and most of those fixes I found while browsing the support forum on wordpress. Feel free to improve upon the changes I've made. :) the modified list_cat_posts.php file:

<?php
	/*
	Plugin Name: List category posts
	Plugin URI: http://picandocodigo.net/programacion/wordpress/list-category-posts-wordpress-plugin-english/
	Description: List Category Posts allows you to list posts from a category into a post/page using the [catlist] shortcode. This shortcode accepts a category name or id, the order in which you want the posts to display, and the number of posts to display. You can use [catlist] as many times as needed with different arguments. Usage: [catlist argument1=value1 argument2=value2].
	Version: 0.13.2
	Author: Fernando Briano
	Author URI: http://picandocodigo.net/
	*/
	
	/* Copyright 2008-2010  Fernando Briano  (email : fernando@picandocodigo.net)
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
	*/
	
	include('list_cat_posts_widget.php');
	
	/**
	 *
	 * Main plugin function: Gets the shortcode parameters, set defaults, and call the plugin's function.
	 * @param $atts
	 * @param $content
	 */
	function catlist_func($atts, $content = null) {
		$atts = shortcode_atts(array(
				'id' => '1',
				'class' => 'lcp_catlist',
				'name' => 'default',
				'orderby' => 'date',
				'order' => 'asc',
				'numberposts' => '15',
				'date' => 'yes',
				'author' => 'yes',
				'dateformat' => get_option('date_format'),
				'template' => 'categories',
				'excerpt' => 'yes',
				'exclude' => '0',
				'excludeposts' => '0',
				'offset' => '0',
				'tags' => '',
				'content' => 'no',
				'catlink' => 'no',
				'comments' => 'yes',
				'thumbnail' => 'yes'
			), $atts);
		return list_category_posts($atts);
	}
	
	/* Add the shortcode to WordPress */
	add_shortcode('catlist', 'catlist_func');
	
	/**
	 * Main function, this is where the flow goes and calls auxiliary functions
	 * @param array $atts
	 */
	function list_category_posts($atts){
		$lcp_category_id = $atts['id'];
		$lcp_category_name = $atts['name'];
	
		//Get the category posts:
		$catposts = lcp_category($lcp_category_id, $lcp_category_name, $atts);
	
		//Template code:
		$tplFileName = null;
		$possibleTemplates = array(
			// File locations lower in list override others
			WP_PLUGIN_DIR.'/list-category-posts/templates/'.$atts['template'].'.php',
		);
		foreach ($possibleTemplates as $key => $file) {
			if (is_readable($file)) {
				$tplFileName = $file;
			}
		}
		if ((!empty($tplFileName)) && (is_readable($tplFileName))) {
			require($tplFileName);
		}else{
			if ($cat_link_string != 'yes'){
				$lcp_output = '<p>' . $cat_link_string . '</p>';
			} else {
				$lcp_output = '';
			}
	//		$lcp_output .= '<ul class="lcp_catlist">';//For default ul
	      $lcp_output .= '<ul class="'.$atts['class'].'">';//For default ul
			foreach ($catposts as $single):
				$lcp_output .= lcp_display_post($single, $atts);
			endforeach;
			$lcp_output .= "";
		}
		return $lcp_output;
	}
	
	/**
	 * Get the categories
	 * @param string $lcp_category_id
	 * @param string $lcp_category_name
	 */
	function lcp_category($lcp_category_id, $lcp_category_name, $atts){
		if($lcp_category_name != 'default' && $lcp_category_id == '0'){
			$lcp_category = 'category_name=' . $atts['name'];
			$category_id = get_cat_ID($atts['name']);
		}else{
			$lcp_category = 'cat=' . $atts['id'];
			$category_id = $atts['id'];
		}
	
		//Link to the category:
		$cat_link_string = '';
		if ($atts['catlink'] == 'yes'){
			$cat_link = get_category_link($category_id);
			$cat_data = get_category($category_id);
			$cat_title = $cat_data->name;
			$cat_link_string = '' . $cat_title . '';
		}
		//Build the query for get_posts()
		$catposts = get_posts($lcp_category.'&numberposts=' . $atts['numberposts'] .
					'&orderby=' . $atts['orderby'] .
					'&order=' . $atts['order'] .
					'&exclude=' . $atts['excludeposts'] .
					'&tag=' . $atts['tags'] .
					'&offset=' . $atts['offset'] );
		return $catposts;
	}
	
	function lcp_display_post($single, $atts){
		$lcp_output .= '
	ID).'">' . $single->post_title . '';
		if ($atts['comments'] == yes){
			$lcp_output .= ' (';
			$lcp_output .=  lcp_comments($single);
			$lcp_output .=  ')';
		}
		if ($atts['date']=='yes'){
			$lcp_output .= lcp_showdate($single, $atts['dateformat']);
		}
		if ($atts['author']=='yes'){
			$lcp_output .= " - ".lcp_showauthor($single) . '';
		}
		if ($atts['content']=='yes' && $single->post_content){
			$lcp_output.= lcp_content($single); // line tweaked to output filtered content
		}
		if ($atts['excerpt']!='no' && !($atts['content']=='yes' && $single->post_content) ){
			$lcp_output .= lcp_excerpt($single);
		}
		if ($atts['thumbnail']=='yes'){
			$lcp_output .= lcp_thumbnail($single);
		}
		$lcp_output.="
	";
		return $lcp_output;
	}
	
	function lcp_comments($single){
		return $single->comment_count;
	}
	
	function lcp_showauthor($single){
		$lcp_userdata = get_userdata($single->post_author);
		return $lcp_userdata->display_name;
	}
	
	function lcp_showdate($single, $dateformat){
		return  ' - ' . get_the_time($dateformat, $single);//by Verex, great idea!
	}
	
	function lcp_content($single){
		$lcp_content = apply_filters('the_content', $single->post_content); // added to parse shortcodes
		$lcp_content = str_replace(']]>', ']]&gt', $lcp_content); // added to parse shortcodes
		return '<p>' . $lcp_content . '</p>';
	}
	
	function lcp_excerpt($single){
		if($single->post_excerpt){
			return '<p>' . $single->post_excerpt . '</p>';
		}
		$lcp_excerpt = strip_tags($single->post_content);
		if ( post_password_required($post) ) {
			$lcp_excerpt = __('There is no excerpt because this is a protected post.');
			return $lcp_excerpt;
		}
		if (strlen($lcp_excerpt) > 255) {
			$lcp_excerpt = substr($lcp_excerpt, 0, 252) . '...';
		}
		return '<p>' . $lcp_excerpt . '</p>';
	}
	
	function lcp_thumbnail($single){
		$lcp_thumbnail = '';
		if ( has_post_thumbnail($single->ID) ) {
			$lcp_thumbnail = get_the_post_thumbnail($single->ID);
		}
		return $lcp_thumbnail;
	}
	
	/** TODO - These are the todo's for a 1.0 release:
	 *  -Pagination
	 *  -Simplify template system
	 *  -i18n
	 */
	?>
	[/code]

	
	the modified default.php file:
	
	
[code] <?php /* Plugin Name: List Category Posts - Template Plugin URI: http://picandocodigo.net/programacion/wordpress/list-category-posts-wordpress-plugin-english/ Description: Template file for List Category Post Plugin for Wordpress which is used by plugin by argument template=value.php Version: 0.8 Author: Radek Uldrych & Fernando Briano Author URI: http://picandocodigo.net http://radoviny.net */ /* Copyright 2009 Radek Uldrych (email : verex@centrum.cz), Fernando Briano (http://picandocodigo.net) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ //Show category? if ($cat_link_string != ''){ $lcp_output = '<p>' . $cat_link_string . '</p>'; }else{ $lcp_output = ''; } $lcp_output .= '<ul class="lcp_catlist">';//For default ul //Posts loop: foreach($catposts as $single): $lcp_output .= ' ID) . '">' . $single->post_title . ''; //Show comments? if($atts['comments'] == yes){ $lcp_output .= ' (' . $single->comment_count . ')'; } //Style for date: if($atts['date']=='yes'){ $lcp_output .= ' - ' . get_the_time($atts['dateformat'], $single); } //Show author? if($atts['author']=='yes'){ $lcp_userdata = get_userdata($single->post_author); $lcp_output .=" - ".$lcp_userdata->display_name; } //Show thumbnail? if($atts['thumbnail']=='yes'){ $lcp_output .= '<div class="lcp_thumbnail">'. lcp_thumbnails($single) . '</div>'; } //Show content? if($atts['content']=='yes' && $single->post_content){ $lcpcontent = apply_filters('the_content', $single->post_content); // added to parse shortcodes $lcpcontent = str_replace(']]>', ']]&gt', $lcpcontent); // added to parse shortcodes $lcp_output .= '<p>' . $lcpcontent . '</p>'; // line tweaked to output filtered content } //Show excerpt? if($atts['excerpt']=='yes' && !($atts['content']=='yes' && $single->post_content) ){ $lcp_output .= lcp_excerpt($single); } $lcp_output .=' '; endforeach; $lcp_output .= ''; ?>

Just a reminder, I'm not a programmer, so I won't be able to give any support in regards to things that don't work. If it does work for you, please let me know! :)

Share this post


Link to post
Share on other sites

Posted · Report post

I think I might have found another possible solution to my answer. Reading around I saw this thread, about using Query-posts and Widget-logic plugings with the Content sidebar. Excellent stuff, I wouldn't have thought to look in that direction. I have a test up after following Sourena Parham's directions. I decided to go with embedding a page into the category since the inverse only produces a list. From here I will try to find a way to format it so that the page fits more naturally. The only downside to this method is that I am not sure how to turn off the other sections (feature slider and full width sidebar) while I'm browsing the categories. Is there a way to create a new platformpro template that affects only category and tag browsing?

Share this post


Link to post
Share on other sites

Posted · Report post

Hm, there is no option for customizing the templates for categories and tags, but maybe there is a way to create new template files. Is this something that you're up for?

Share this post


Link to post
Share on other sites

Posted · Report post

Certainly, a new template section just for categories and tags would benefit everybody! :) Alternatively, if we follow Widget logic's train of thought), sections like the Feature Slider (et al.) could benefit from an excluding page/post/category/tag. Though that's far and away from a template solution, into the realms of core programming. :b

Share this post


Link to post
Share on other sites