[theme-reviewers] Page Template Files in Sub-Folders

Sayontan Sinha sayontan at gmail.com
Wed Jul 27 23:17:25 UTC 2011


Well, this is how BP does it for activity streams:

<?php
    add_filter('wp_dropdown_pages', 'custom_wp_pages_filter'); //adds
to the drop-down
    add_filter('page_template', 'custom_page_template');// executes
when the page template is being loaded

	add_action('pre_get_posts', 'bpp_fix_get_posts_on_activity_front');
	add_filter('the_posts', 'bpp_fix_the_posts_on_activity_front');

	function custom_wp_pages_filter($page_html) {
		if ('page_on_front' != substr($page_html, 14, 13))
			return $page_html;

		$selected = false;
		$page_html = str_replace('</select>', '', $page_html);

		if ($this->bpp_page_on_front() == 'activity')
			$selected = ' selected="selected"';

		$page_html .= '<option class="level-0"
value="activity"'.$selected.'>'.__('Activity Stream',
'buddypress').'</option></select>';
		return $page_html;
	}

    function custom_page_template($template) {
        global $wp_query;

        if (empty($wp_query->post->ID))
            return locate_template(array('activity/index.php'), false);
        else


            return $template;
    }
	function bpp_fix_get_posts_on_activity_front() {
		global $wp_query;

		if (!empty($wp_query->query_vars['page_id']) && 'activity' ==
$wp_query->query_vars['page_id'])

			$wp_query->query_vars['page_id'] = '"activity"';
	}

	function bpp_fix_the_posts_on_activity_front($posts) {
		global $wp_query;
		if (empty($posts) && !empty($wp_query->query_vars['page_id']) &&
'"activity"' == $wp_query->query_vars['page_id'])
			$posts = array((object) array('ID' => 'activity'));
		return $posts;
	}
?>

This is an adaptation of the BP default theme. It does quite a few things:

   1. Adds the new page template to the "Page Templates" drop-down
   2. Makes sure that the loop doesn't act up
   3. Loads the file activity/index.php when something is using the
   "activity" template.

I was curious if there is an easier way to avoid clutter in the root folder.
Other uses, including locate_template() or get_template_part() in isolation
will not do the job - I believe all the hooks have to be applied.


On Wed, Jul 27, 2011 at 3:59 PM, Emil Uzelac <emil at themeid.com> wrote:

> The only option I could think of from top of my head is:
>
> <?php
> /*
> Template Name: Blog
> */
> ?>
>
> <?php get_template_part( 'blog-template'); ?>
>
> But with this you're not gaining whole lot.
>
> Basically i.e. blog.php is a placeholder and blog-template.php is the
> actual template.
>
> Emil
> ----
> *Emil Uzelac* | ThemeID | T: 224-444-0006 | Twitter: @EmilUzelac | E:
> emil at themeid.com | http://themeid.com
> Make everything as simple as possible, but not simpler. - Albert Einstein
>
>
>
> On Wed, Jul 27, 2011 at 5:26 PM, Sayontan Sinha <sayontan at gmail.com>wrote:
>
>> Hi,
>> Is there a way to have page templates in a sub-folder of a theme or do
>> they have to be in the root folder? I know that using filter hooks an a
>> template from a sub-folder can be added to the drop-down list of templates
>> in the "Edit Page" screen (the way BP does it). Is there an easier way to do
>> this?
>>
>> Regards,
>> Sayontan.
>>
>> --
>> Sayontan Sinha
>> http://mynethome.net | http://mynethome.net/blog
>>  --
>> Beating Australia in Cricket is like killing a celebrity. The death gets
>> more coverage than the crime.
>>
>>
>> _______________________________________________
>> theme-reviewers mailing list
>> theme-reviewers at lists.wordpress.org
>> http://lists.wordpress.org/mailman/listinfo/theme-reviewers
>>
>>
>
> _______________________________________________
> theme-reviewers mailing list
> theme-reviewers at lists.wordpress.org
> http://lists.wordpress.org/mailman/listinfo/theme-reviewers
>
>


-- 
Sayontan Sinha
http://mynethome.net | http://mynethome.net/blog
--
Beating Australia in Cricket is like killing a celebrity. The death gets
more coverage than the crime.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.wordpress.org/pipermail/theme-reviewers/attachments/20110727/6349f6d7/attachment.htm>


More information about the theme-reviewers mailing list