Tagged: Theme Options
Enabling post types based on custom added theme options
-
CreatorTopic
-
February 17, 2015 at 10:57 pm #20578
Brett
ParticipantI am starting to mess around with adding theme options to child themes. I am trying to add support to a plugin called Church Theme Content that creates four different custom post types. What I would like to do is add a tab in the theme options to enable or disable the different post types as needed.
I did the following to create the new tab and then add three options. I was messing around with radios and checkboxes to see which would work better. I did
themeblvd_add_option_tab( 'church_content', 'Church Content' );
$churchcontent = 'Church Content Support'; $churchcontent_description = 'Here you can configure your options for displaying Church Content.'; $churchcontent_options = array( array( 'name' => 'Enable Sermons', 'desc' => 'Select whether you\'d like to enable support for Church Themes Sermons.', 'id' => 'churchcontent_sermons', 'std' => 'show', 'type' => 'radio', 'options' => array( 'show' => 'Yes, enable Sermons.', 'hide' => 'No, disable Sermons.' ) ), array( 'name' => 'Enable People', 'desc' => 'Select whether you\'d like to enable support for Church Themes People.', 'id' => 'churchcontent_people', 'std' => '1', 'type' => 'checkbox', ), array( 'name' => 'Enable Locations', 'desc' => 'Select whether you\'d like to enable support for Church Themes Locations.', 'id' => 'churchcontent_locations', 'std' => 'show', 'type' => 'radio', 'options' => array( 'show' => 'Yes, enable Locations.', 'hide' => 'No, disable Locations.' ) ) ); themeblvd_add_option_section( 'church_content', 'churchcontent', $churchcontent, $churchcontent_description, $churchcontent_options );
This adds the tab just fine but now I am trying to add the condition to enable if the box is checked. I searched info on the Options Framework and for the checkbox I found something that looked like it should work but it isn’t.
It said to use
if ( 1 == themeblvd_get_option('churchcontent_people') ) { add_theme_support('ctc-people'); } else { // no support }
What should that be in order to or add theme support for that post type if the option is checked? How would that work with a radio button?
-
CreatorTopic
-
AuthorReplies
-
February 17, 2015 at 11:47 pm #20579
Jason Bobich
KeymasterHi,
Your question more seems to be about registering a post type based on some conditional over actually the theme options part of it.
I’m not quite sure where you’re getting the idea to do this? Or that “theme support” has anything to do with registering custom post types?
add_theme_support('ctc-people');
http://codex.wordpress.org/Function_Reference/add_theme_support
When you register your post types you’d just see if your options are set. That’s all you’re trying to do, right?
function my_post_types() { if ( themeblvd_get_option('churchcontent_people') ) { // ... register_post_type( $post_type, $args ); } // ... } add_action( 'init', 'my_post_types' );
http://codex.wordpress.org/Function_Reference/register_post_type
And it’s easier to paste code into the forum if you use pre tags.
February 17, 2015 at 11:57 pm #20580Jason Bobich
KeymasterWhen you’re playing around with these different option types and trying to see what values you’re getting and how to use them, simply get the value, and print it out on the screen.
$val = themeblvd_get_option('whatever'); var_dump($val);
February 18, 2015 at 12:08 am #20581Brett
ParticipantI think you misunderstood my questions which wasn’t very clear. The Church Theme Content plugin registers 4 different post types for different things. One is for sermons, one is for people/staff, one is for events and one is for locations.
To add support for the post types to a theme, you have to add the following to your functions.php file.
// Church Theme Content Framework
function mytheme_add_ctc_support() {
require(get_stylesheet_directory() . '/_ctc_framework/framework.php');function forgiven_add_ctc_support() {
add_theme_support('church-theme-content');
add_theme_support('ctc-sermons');
add_theme_support('ctc-people');
add_theme_support( 'ctc-locations' );
add_theme_support( 'ctc-events' );
if (!is_multisite()) { add_theme_support('ctfw-force-downloads'); }
}add_action( 'after_setup_theme', 'mytheme_add_ctc_support' );
All that is fine but what I am trying to do now with the theme options is set up a new tab that lets you control which of the post types are enabled. I want to add a check box that says “enable sermons” or “enable events” or “enable people” etc. If the box is not checked, I don’t want to enable that specific post type because not everyone will need all four of them. Some may just need sermons, or sermons and events, etc.
What I want to do is say, if this box is checked, then add_theme_support(‘ctc-sermons’);
More than anything here, I am messing around theme options to learn how to add more control to themes.
February 18, 2015 at 12:45 am #20582Jason Bobich
Keymasteradd_action( 'after_setup_theme', 'mytheme_add_ctc_support' );
… I think this may be where the confusion is coming in? At the point in the loading process,
themeblvd_get_option()
is not available to be used. So, you’re never going to get a value back calling that function here.The settings are pulled and ready to be used after_setup_theme, priority 1000. So, you can try this:
add_action( 'after_setup_theme', 'mytheme_add_ctc_support', 1001 );
Then in your
mytheme_add_ctc_support()
function you’re free to use:if ( themeblvd_get_option('churchcontent_people') ) { add_theme_support(‘ctc-people’); }
Since
themeblvd_get_option('churchcontent_people')
is returning a value from a check box, it will always be a string'1'
or a string'0'
.if ( themeblvd_get_option('churchcontent_people') ) { // ... Yes, option was checked }
if ( ! themeblvd_get_option('churchcontent_people') ) { // ... No, option was not checked }
February 18, 2015 at 12:54 am #20583Brett
ParticipantThat was it, that worked. Thanks. This is my first attempt at messing with theme settings. How would that work with the radio option instead of a checkbox?
February 18, 2015 at 12:59 am #20584Jason Bobich
KeymasterIt depends on what you made your values. Let’s look at what you did above:
array( 'name' => 'Enable Locations', 'desc' => 'Select whether you\'d like to enable support for Church Themes Locations.', 'id' => 'churchcontent_locations', 'std' => 'show', 'type' => 'radio', 'options' => array( 'show' => 'Yes, enable Locations.', 'hide' => 'No, disable Locations.' ) )
You’ve set your values as “show” and “hide” … which probably doesn’t make total sense in this case, but technically doesn’t matter. Maybe use “yes” and “no”?
But with the way you have it, you’ve got:
if ( themeblvd_get_option('churchcontent_locations') == 'show' ) { add_theme_support(‘ctc-locations’); }
-
AuthorReplies
- You must be logged in to reply to this topic.