Avatar elsue asked

Adding Custom Post Type & Taxonomy to Widget Options

The way that I got Widget Options to work for my custom post type "treatments" and to show all the terms under by custom taxonomy "conditions" was to modify some system files (hopefully at some point these files can be moved into the theme or my custom style).

First I added my code to the display.php file found in warp --> systems --> wordpress --> config --> layouts --> fields

Image

Here is the code in my display.php file:

<?php  
/**  
* @package   Warp Theme Framework  
* @author    YOOtheme http://www.yootheme.com  
* @copyright Copyright (C) YOOtheme GmbH  
* @license   http://www.gnu.org/licenses/gpl.html GNU/GPL  
*/  

    $options  = array();  
    $defaults = array(  
        '*'          => 'All',  
        'front_page' => 'Frontpage',  
        'home'       => 'Home (Posts page)',  
        'archive-treatments' => 'Treatments Archives',  
        'taxonomy-conditions' => 'Conditions Archives',  
        'archive'    => 'Archive',  
        'search'     => 'Search',  
        'single'     => 'Single',  
        'single-treatments'     => 'Treatments Single',  
        'page'      => 'Pages',  
    );  

    $selected = is_array($value) ? $value : array('*');  

    if (count($selected) > 1 && in_array('*', $selected)) {  
        $selected = array('*');  
    }  

    // set default options  
    foreach ($defaults as $val => $label) {  
        $attributes = in_array($val, $selected) ? array('value' => $val, 'selected' => 'selected') : array('value' => $val);  
        $options[]  = sprintf('<option %s />%s</option>', $control->attributes($attributes), $label);  
    }  

    // set pages  
    if ($pages = get_pages()) {  
        $options[] = '<optgroup label="Pages">';  

        foreach ($pages as $page) {  
            $val        = 'page-'.$page->ID;  
            $attributes = in_array($val, $selected) ? array('value' => $val, 'selected' => 'selected') : array('value' => $val);  
            $options[]  = sprintf('<option %s />%s</option>', $control->attributes($attributes), $page->post_title);  
        }  

        $options[] = '</optgroup>';                    
    }  

    // set categories  
    if ($categories = get_categories()) {  
        $options[] = '<optgroup label="Categories">';  

        foreach ($categories as $category) {  
            $val        = 'cat-'.$category->cat_ID;  
            $attributes = in_array($val, $selected) ? array('value' => $val, 'selected' => 'selected') : array('value' => $val);  
            $options[]  = sprintf('<option %s />%s</option>', $control->attributes($attributes), $category->cat_name);  
        }  

        $options[] = '</optgroup>';                    
    }  

    // set conditions  
    if ($terms = get_terms('conditions')) {  
        $options[] = '<optgroup label="Conditions">';  

        foreach ( $terms as $term ) {  
            $val        =  'tax-'.$term->slug;  
            $attributes = in_array($val, $selected) ? array('value' => $val, 'selected' => 'selected') : array('value' => $val);  
            $options[]  = sprintf('<option %s />%s</option>', $control->attributes($attributes), $term->name);  
        }  

        $options[] = '</optgroup>';                    
    }  


?>  
<select name="<?php echo $name;?>[]" style="width:220px;height:120px;" multiple="multiple">  
    <?php echo implode("", $options); ?>  
</select>

Next I modified system.php in warp --> systems --> wordpress --> helpers. This is my code.

/*  
        Function: getQuery  
            Get current query information  

        Returns:  
            Object  
    */  
    public function getQuery() {  
        global $wp_query;  

        // create, if not set  
        if (empty($this->query)) {  

            // init vars  
            $obj   = $wp_query->get_queried_object();  
            $query = array();  

            // find current page type  
            foreach (array('home', 'front_page', 'archive', 'taxonomy-conditions', 'archive-treatments', 'single-treatments', 'search', 'single', 'page', 'category', 'conditions') as $type) {  
                if (call_user_func('is_'.$type)) {  
                    $query[] = $type;  

                    if ((is_single()) && (get_post_type() == 'treatments')) {  
                         $query[] = 'single-treatments';  
                     }  

                    if ((is_archive()) && get_taxonomy( 'conditions')) {  
                         $query[] =  'taxonomy-conditions';  
                    }       

                    if ((is_archive()) && is_post_type_archive( 'treatments')) {  
                         $query[] =  'archive-treatments';     
                    }  
                    if ($type == 'page') {  
                        $query[] = 'page-'.$obj->ID;  
                    }  

                    if ($type == 'category') {  
                        $query[] = 'cat-'.$obj->cat_ID;  
                    }  

                    if (is_tax( 'conditions')){  
                        $query[] = 'tax-'.$obj->slug;  
                    }  


    }  
            }  

            $this->query = $query;  
        }  

        return $this->query;  
    }  

The end result is that I have widget options for my templates as well as all the terms in the custom taxonomy "conditions."

Again, I hope this helps.

  • WordPress
  • Warp Theme

10 Answers

1

Avatar david.carroll answered

It's official... You are my new favorite person in the community!!!

Thank you for contributing such great details.

I can't help but think this could be easily extended to automatically add support for all registered custom post types and avoid hard coding this each type.

Also, have you tested this with WooCommerce by any chance?

Great job!!!

2

Avatar david.carroll answered

@alex - Regarding the error message you reported:

If the page did not require the taxonomy to be loaded, calling the function is_taxonomy-character() would generate the function not found error.

I recommend modifying the line:

if (call_user_func('is_'.$type)) {  

To become:

if (function_exists( 'is_'.$type ) && call_user_func('is_'.$type)) {  

This will, at least, test the existence of the custom taxonomy method before call it.

1

Avatar elsue answered

Thanks David,

I'm learning a lot...more comfortable with css than php, that might change soon :)

I've used Woocommerce with another theme but not tried to integrate it with Warp. Here is the Template Structure and WooCommerce Conditionals which may help with any code that has to be added.

I hope that Warp adds custom post type handling. That would be awesome!

Ellen

1

Avatar pxforti answered

Hi,

This is a great tutorial. I got errors unless I added this code from david.carroll to system.php

I recommend modifying the line:

if (call_user_func('is_'.$type)) {    

To become:

if (function_exists( 'is_'.$type ) && call_user_func('is_'.$type)) {  

Thanks

Edited

0

Avatar alex.kahl.40 answered

wow, that`s really cool, for all hardcoding...after all, only a few lines of code, but the way to it can be hard, i know:)...thank you ellen

0

Avatar alex.kahl.40 answered

Hi Ellen,

it works for me, i can call special widgets on a ctp taxonomy page

but i get also an error message above on all my pages(home, contact,.....) .....the error messages are always the same and for all my new created types

i.e. taxonomy-character

Warning: call_user_func() expects parameter 1 to be a valid callback, function 'is_taxonomy-character' not found or invalid function name in E:\xampp\htdocs\bigeasy\wp-content\themes\yoo_bigeasy_wp\warp\systems\wordpress\helpers\system.php on line 167

can you help me?

best regards alex

0

Avatar alex.kahl.40 answered

ok i`ve got it:)
it was an error reporting issue..errors have to be hidden

Edited

0

Avatar alex.kahl.40 answered

a little bit of an ignorant solution but it works...lol^^

0

Avatar alex.kahl.40 answered

Thank you David,

this works very well^^
I understood the issue, but didn`t know how to fix it, as i am not a real programmer.

Best regards
Alex

Edited

0

Avatar javier.jimenez.90 answered

Hello everybody!

I think this is the way to resolve the problem that some users set in the support forum:

How can we assign style to the custom content type of "The events calendar pro" plugin. http://www.yootheme.com/support/question/8658

The ECP Support team submit me to this forum.

I have tried this but I can´t resolve the problem... please... How can I assign style to events calendar pro?!?! We need HELP!

Thanks.

Know someone who can answer? Share a link to this question via email or twitter.