How to add a custom page template selector to a custom taxonomy?

I am trying to add the custom page template selector that can be found in the “edit page” screen for pages to a custom taxonomy. The custom taxonomy is product_cat created by WooCommerce.

I am using the Tax-meta-class created by Ohad Raz of en.bainternet.info. This allows me to add a meta box to the the “edit product categories” screens. Ohad Raz has also authored a plugin that adds a custom page template selector menu to the “edit category” screens. However it does not show for the custom taxonomy.

I can get the selector menu to out put the templates by adding:

<?php page_template_dropdown($template); ?>

to the Tax-meta-class.php. It out puts the proper values that i can use later in a template redirect but it won’t save.

The built in way to add a drop down selector has you choose the options values and names by adding them to an array like so:

array('selectkey1'=>'Select Value1','selectkey2'=>'Select Value2')

Which outputs this:

<option value='selectkey1'>Select Value1</option>
<option value='selectkey2'>Select Value2</option>

if I use the code:

$templates = get_page_templates();
foreach ( $templates as $template_name => $template_filename ) {
   echo $template_name . ' ' . $template_filename . ' ';
}

I can get it to output:

Contact Page contact-page.php EVENT Pageevent-page.php Product
Icon product-icon.php Product SubFeature product-subfeature.php
Lockable Dowel template-lockable-dowel.php

I was hoping there would be a way to store these values in an array that can be used in place of:

array('selectkey1'=>'Select Value1','selectkey2'=>'Select Value2')

This way it will be using the Tax-meta-class how it should be and everything should work fine.

I am fairly new and all self taught so please excuse me if I have not asked in the proper way. Thank you for taking the time to rean my question.

Sincerely,
Leon

Answer

get_page_templates already returns an array:

Array
(
    [Sidebar] => sidebar.php
    [Category] => category.php
    [Page] => page.php
)

If you need to swap keys and values you can use php’s array_flip.

Attribution
Source : Link , Question Author : Leon Francis Shelhamer , Answer Author : Milo

Leave a Comment