Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Theme options page WordPress

  • SOLVED

I have a music festival page that display all festivaldays on index page.
Url to page, http://planeta.se/

Each day have separate colour,
see screenshot, http://awesomescreenshot.com/0d82qs83b0

Each day is a category (here is the first loop),

<?php query_posts('showposts=78&cat=4,'); if (have_posts()) : ?>
<ul id="customCategory" class="group">
<?php while ( have_posts() ) : the_post(); ?>
<li>

<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
the_post_thumbnail('featuredImg');
echo '</a>';
} else { ?>
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/ingenbildikon1.jpg" alt="<?php the_title(); ?>" /></a>
<?php } ?>
<div class="infoH">
<div class="info"><h2><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2></div>
<!--<div class="exerpt"></div>-->
<?php if((get_post_meta($post->ID, "exerpt", true))) { ?>
<div class="exerpt">
<?php echo get_post_meta($post->ID, "exerpt", true); ?>
</div>
<?php } ?>

<div class="date2"><p><?php $my_textbox_value = mtbxr_val("scen_och_datum");
echo $my_textbox_value;?></p></div>
</div>
</li>
<?php endwhile ;?>
<?php endif; wp_reset_query();?>
</div>


My question is if anyone can build a "theme options page" (backend) where I can choose what category to show in each loop on the index page. And also control what category to show in sidebars on each single page.

Answers (5)

2014-05-03

Jihan Ahmed answers:

Hello, so you have several post loops on your index.php or the default wp loop I am not sure... but if u have several post loops and u wanted to control the post category of each loop.. then its very simple. I have developed many theme options see http://wptheming.com/options-framework-theme/

If its the case let me know... I'll need access to wp-admin and ftp for creating a backend option for u.


Jihan Ahmed comments:

sorry I missed it... i see u have several queries like <?php query_posts('showposts=78&cat=4,'); if (have_posts()) : ?>

its very easy to control the category... the theme options that I will integrate will list all available category for each loop and u only have to pick the right one !!

2014-05-02

Arnav Joy answers:

here is the line which holds the cat id

query_posts('showposts=78&cat=4,');

so you can replace it something like

query_posts('showposts=78&cat='.$cat_id);

where $cat_id is the id of the category taken from theme options_page

like

$cat_id = get_option('home_page_cat');

2014-05-02

Firoja_Imtosupport answers:

Hi,

To acheive functionality you want will have to create a custom plugin and then modify your function. Please can you provide me your skype id

Thanks.


Manny comments:

Hello!
Here is the code I´m using for test option page,

But how do I code that page to get to chose category?

/**
* Theme Option Page Example
*/
function pu_theme_menu()
{
add_theme_page( 'Theme Option', 'Theme Options', 'manage_options', 'pu_theme_options.php', 'pu_theme_page');
}
add_action('admin_menu', 'pu_theme_menu');
/**
* Callback function to the add_theme_page
* Will display the theme options page
*/
function pu_theme_page()
{
?>
<div class="section panel">
<h1>Custom Theme Options</h1>
<form method="post" enctype="multipart/form-data" action="options.php">
<?php
settings_fields('pu_theme_options');

do_settings_sections('pu_theme_options.php');
?>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>

</form>

</div>
<?php
}
/**
* Register the settings to use on the theme options page
*/
add_action( 'admin_init', 'pu_register_settings' );

/**
* Function to register the settings
*/
function pu_register_settings()
{
// Register the settings with Validation callback
register_setting( 'pu_theme_options', 'pu_theme_options', 'pu_validate_settings' );

// Add settings section
add_settings_section( 'pu_text_section', 'Text box Title', 'pu_display_section', 'pu_theme_options.php' );



// Create textbox field
$field_args = array(
'type' => 'text',
'id' => 'pu_textbox',
'name' => 'pu_textbox',
'desc' => 'Example of textbox description',
'std' => '',
'label_for' => 'pu_textbox',
'class' => 'css_class'
);

add_settings_field( 'example_textbox', 'Example Textbox', 'pu_display_setting', 'pu_theme_options.php', 'pu_text_section', $field_args );
}
/**
* Function to add extra text to display on each section
*/
function pu_display_section($section){

}
/**
* Function to display the settings on the page
* This is setup to be expandable by using a switch on the type variable.
* In future you can add multiple types to be display from this function,
* Such as checkboxes, select boxes, file upload boxes etc.
*/
function pu_display_setting($args)
{
extract( $args );

$option_name = 'pu_theme_options';

$options = get_option( $option_name );

switch ( $type ) {
case 'text':
$options[$id] = stripslashes($options[$id]);
$options[$id] = esc_attr( $options[$id]);
echo "<input class='regular-text$class' type='text' id='$id' name='" . $option_name . "[$id]' value='$options[$id]' />";
echo ($desc != '') ? "<br /><span class='description'>$desc</span>" : "";
break;
}
}
/**
* Callback function to the register_settings function will pass through an input variable
* You can then validate the values and the return variable will be the values stored in the database.
*/
function pu_validate_settings($input)
{
foreach($input as $k => $v)
{
$newinput[$k] = trim($v);

// Check the input is a letter or a number
if(!preg_match('/^[A-Z0-9 _]*$/i', $v)) {
$newinput[$k] = '';
}
}

return $newinput;
}


Firoja_Imtosupport comments:

Hi,

Ok i understand you want to modify your function (from http://www.paulund.co.uk/theme-options-page) to add categories dropdown and save.
Please you provide your skype id?

Thanks


Firoja_Imtosupport comments:

Hi,

Ok i understand you want to modify your function (from http://www.paulund.co.uk/theme-options-page) to add categories dropdown and save.
Please you provide your skype id?

Thanks


Firoja_Imtosupport comments:

Hi,

http://stackoverflow.com/questions/9116709/wordpress-theme-options-page-with-category-list link will be useful for you to code as it consist of categories listing

Thanks


Firoja_Imtosupport comments:

Hi,

Does above link useful, else please let me know i can provide some code snippets

Thanks

2014-05-02

isp_charlie answers:

you can use advanced custom fields plugins and acf option page extend plugin.

2014-05-02

Bob answers:

You can download this plugin [[LINK href="http://wp-lovers.com/wpquestions/wlseo.zip"]]http://wp-lovers.com/wpquestions/wlseo.zip[[/LINK]]

This is sample plugin how you can create options page for your site. if you don't want to create plugin then you can integrate it in your theme also. Let me know if you don't know how to integrate it.

This plugin contains different elements that you can create using simple array.

download plugin install it. go to class.my-options.php file scroll little and find function <strong>public function get_settings()</strong>

here you will create simple array of form elements that you want.

In your case if you want to get dropdown of categories available then you have to create an array of categories and url code like following.


$categoreis_arr // a variable that will hold category array.
$this->settings['example_select'] = array(
'section' => 'general',
'title' => __( 'Example Select' ),
'desc' => __( 'This is a description for the drop-down.' ),
'type' => 'select',
'std' => '',
'choices' => $categoreis_arr



I am writing little bit of code right now in new post I will send you complete code.
meanwhile you can play with above plugin.

Let me know if I am on wrong path or I am misunderstood your task.


Bob comments:

<strong>Step 1:</strong> Download and install plugin [[LINK href="http://wp-lovers.com/wpquestions/wlseo.zip"]]http://wp-lovers.com/wpquestions/wlseo.zip[[/LINK]]
<em>Note: We can rename your options page name and everything once your comfortable with it.</em>

<strong>Step 2:</strong> Go to <strong>class.my-options.php</strong>, go to line no 27. There should be something like this
$this->sections['general'] = __( 'General' );
Here you can create different section for your theme.

<strong>Step 3:</strong> Go to <strong>class.my-options.php</strong>, go to line no 293. There should be something like this
public function get_settings()
Here you can create different form element for your theme.
few elements are created for your referance

<strong>Step 3:</strong> go to line no around 342 there should be example of dropdown.
replace old code with new one to show categories.
old code:

$this->settings['example_select'] = array(
'section' => 'general',
'title' => __( 'Example Select' ),
'desc' => __( 'This is a description for the drop-down.' ),
'type' => 'select',
'std' => '',
'choices' => array(
'choice1' => 'Other Choice 1',
'choice2' => 'Other Choice 2',
'choice3' => 'Other Choice 3'
)
);

new code:
$categories = get_categories( $args );
$categoreis_arr[0]= "Please select Category";
if($categories){
foreach($categories as $category){
$categoreis_arr[$category->cat_ID]= $category->name;
}
}
$this->settings['example_select'] = array(
'section' => 'general',
'title' => __( 'Example Select' ),
'desc' => __( 'This is a description for the drop-down.' ),
'type' => 'select',
'std' => '',
'choices' => $categoreis_arr
);

<strong>Note</strong> it will not display categories which do not have any posts.

Now go to backend side of your wordpress you whould see "Dashboard 2" menu left side.
go there you should be able to see different form element one is dropdown of categories. change category and save


If everything is good so far then now you need that saved option in your theme file code.
Here is function which do that
$selected_cat = wlseo_option('example_select');


Bob comments:

I also have another very simple solution let me know if you are interested in it or above thing do not work.

With above code once you have backend saved option you can modify the query which use save options value like Arnav suggested.

so code look like.


$selected_cat = wlseo_option('example_select');
query_posts('showposts=78&cat='.$selected_cat);


Bob comments:

Recently in my localhost I have added extra code for user to select multiple categories.

it will return array of selected category ids.

That code is not on server yet. if you wish I can upload it there.


Manny comments:

yes please! :)


Bob comments:

replce old plugin with new one.
[[LINK href="http://wp-lovers.com/wpquestions/wlseo-new.zip"]]http://wp-lovers.com/wpquestions/wlseo-new.zip[[/LINK]]

to create multi select you need to create multyselect type form element.


Manny comments:

I just send you a pm :)


Bob comments:

sent u pm.