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

Sub categories inherits main cat template, functions.php issue. WordPress

  • SOLVED

Hello,

I am having a strange issue, I require a sub-category to have the same template as its parent, I found this resource; [[LINK href="http://wcdco.info/aJ"]]http://wcdco.info/aJ[[/LINK]]

It works! Except its causing issues with my existing functions.php scripts.

<strong>functions.php contains;</strong>

Category inherits
Custom Widgets
Custom Meta, with post screen interface
Remove unnecessary from header

<strong>The problem;</strong>

The category inherits script seems to be conflicting with my custom meta script, I can no longer query meta data; meta script works perfectly fine without category inherits.

<blockquote>Wondering if I am missing a simple closing statement, or that my inherit script was based on an older version of Wordpress.</blockquote>

<em>Any information you can provide will be much appreciated.</em>

<?php

// Category inherits

function inherit_template() {
if (is_category()) {
$catid = get_query_var('cat');
if ( file_exists(TEMPLATEPATH . '/category-' . $catid . '.php') ) {
include( TEMPLATEPATH . '/category-' . $catid . '.php');
exit;
}

$cat = &get_category($catid);
$parent = $cat->category_parent;
while ($parent) {
$cat = &get_category($parent);
if ( file_exists(TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php') ) {
include (TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php');
exit;
}

$parent = $cat->category_parent;
}
}
}

add_action('template_redirect', 'inherit_template', 1);

// Custom Widgets

register_sidebar( array(
'name' => __( 'Posts Sidebar'),
'id' => 'post_sidebar',
'description' => __( 'These widgets are shown on all posts.' ),
'before_title' => '<h2>',
'after_title' => '</h2>',
) );

register_sidebar( array(
'name' => __( 'Category Sidebar'),
'id' => 'cat_sidebar',
'description' => __( 'These widgets are shown on all category and archives.' ),
'before_title' => '<h2>',
'after_title' => '</h2>',
) );

register_sidebar( array(
'name' => __( 'footer'),
'id' => 'footer',
'description' => __( 'This widget is shown in the footer.' ),
'before_title' => '<h2>',
'after_title' => '</h2>',
) );

// Custom Meta, with post screen interface

if ( !class_exists('rt_custom_fields') ) {

class rt_custom_fields {

var $prefix = 'krp_';

var $customFields = array(

array(
"name" => "post_img",
"title" => "Article Thumbnail",
"description" => "Please attached the photo you wish to display.",
"type" => "rttheme_upload",
"scope" => array( "post" ),
"capability" => "edit_post"
),

array(
"name" => "post_webinars",
"title" => "Webinars URL",
"description" => "Please enter the full URL of the intended Webinar.",
"type" => "rttheme_upload",
"scope" => array( "post" ),
"capability" => "edit_post"
),

array(
"name" => "sidebar_id",
"title" => "Sidebar ID",
"description" => "Please enter the page ID of sidebar you wish to display.",
"type" => "rttheme_text",
"scope" => array( "page" ),
"capability" => "edit_post"
),

);

function rt_custom_fields() { $this->__construct(); }

function __construct() {
add_action( 'admin_menu', array( &$this, 'createCustomFields' ) );
add_action( 'save_post', array( &$this, 'saveCustomFields' ) );
add_action( 'do_meta_boxes', array( &$this, 'removeDefaultCustomFields' ), 10, 3 );
}

function removeDefaultCustomFields( $type, $context, $post ) {
foreach ( array( 'normal', 'advanced', 'side' ) as $context ) {
remove_meta_box( 'postcustom', 'post', $context );
remove_meta_box( 'pagecustomdiv', 'page', $context );
}
}

function createCustomFields() {
if ( function_exists( 'add_meta_box' ) ) {
add_meta_box( 'rt_custom_fields', 'Custom functions powered by Twelve Seas Web Design.', array( &$this, 'displayCustomFields' ), 'post', 'normal', 'high' );

add_meta_box( 'rt_custom_fields', 'Custom functions powered by Twelve Seas Web Design.', array( &$this, 'displayCustomFields' ), 'page', 'normal', 'high' );

}
}

function displayCustomFields() {
global $post;
?>
<div class="form-wrap">
<?php
wp_nonce_field( 'rt_custom_fields', 'rt_custom_fields_wpnonce', false, true );
foreach ( $this->customFields as $customField ) {
$scope = $customField[ 'scope' ];
$output = false;
foreach ( $scope as $scopeItem ) {
switch ( $scopeItem ) {
case "post": {
if ( basename( $_SERVER['SCRIPT_FILENAME'] )=="post-new.php" || $post->post_type=="post" )
$output = true;
break;
}
case "page": {
if ( basename( $_SERVER['SCRIPT_FILENAME'] )=="page-new.php" || $post->post_type=="page" )
$output = true;
break;
}
}
if ( $output ) break;
}
if ( !current_user_can( $customField['capability'], $post->ID ) )
$output = false;
if ( $output ) { ?>
<div class="form-field form-required">
<?php
switch ( $customField[ 'type' ] ) {
case "checkbox": {
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;">' . $customField[ 'title' ] . '</label>&nbsp;&nbsp;';
if ( $customField[ 'description' ] ) echo '<p>' . $customField[ 'description' ] . '</p>';
echo '<input type="checkbox" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="yes"';
if ( get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) == "yes" )
echo ' checked="checked"';
echo '" style="width: auto;" />';
break;
}
case "textarea": {
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'">' . $customField[ 'title' ] . '</label>';
if ( $customField[ 'description' ] ) echo '<p>' . $customField[ 'description' ] . '</p>';
echo '<textarea name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" columns="30" rows="3">' . ( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '</textarea>';
break;
}
case "rttheme_upload": {
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'">' . $customField[ 'title' ] . '</label>';
if ( $customField[ 'description' ] ) echo '<p>' . $customField[ 'description' ] . '</p>';
echo '<input type="text" style="width:185px;" size="6" class="newtag form-input-tip" name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" value="' . ( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '" /><input type="button" style="width:45px;outline:none;padding:2px 0;" class="rttheme_upload_button '. $this->prefix . $customField[ 'name' ] .' button tagadd" value="save" tabindex="3" />';
break;
}
default: {
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'">' . $customField[ 'title' ] . '</label>';
if ( $customField[ 'description' ] ) echo '<p>' . $customField[ 'description' ] . '</p>';
echo '<input type="text" name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" value="' . ( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '" />';
break;
}
}

?>

</div>
<?php
}
} ?>
</div>
<?php
}

function saveCustomFields( $post_id ) {
global $post;
if ( !wp_verify_nonce( $_POST[ 'rt_custom_fields_wpnonce' ], 'rt_custom_fields' ) )
return $post_id;
foreach ( $this->customFields as $customField ) {
if ( current_user_can( $customField['capability'], $post_id ) ) {
if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) && trim( $_POST[ $this->prefix . $customField['name'] ] ) ) {
update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $_POST[ $this->prefix . $customField['name'] ] );
} else {
delete_post_meta( $post_id, $this->prefix . $customField[ 'name' ] );
}
}
}
}

}

}

$rt_custom_fields_var = new rt_custom_fields();

// remove unnecessary from header

remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);

?>





Answers (2)

2010-08-17

Baki Goxhaj answers:

You are missing this bit of code - the action hook:

add_action('template_redirect', 'inherit_template');


Here is all the code:

add_action('template_redirect', 'inherit_template');

function inherit_template() {

if (is_category()) {

$catid = get_query_var('cat');

if ( file_exists(TEMPLATEPATH . '/category-' . $catid . '.php') ) {
include( TEMPLATEPATH . '/category-' . $catid . '.php');
exit;
}

$cat = &get_category($catid);

$parent = $cat->category_parent;

while ($parent){
$cat = &get_category($parent);
if ( file_exists(TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php') ) {
include (TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php');
exit;
}
$parent = $cat->category_parent;
}
}
}


West Coast Design Co. comments:

Hi Baki,

Action hook was there … it was just at the bottom of that section of code; both work by them self’s … but still conflicts.

Hmm.


Baki Goxhaj comments:

It worked on my server, so changing place would be a option. Also, removing the their argument on the hook "1" would be another try.


West Coast Design Co. comments:

Interesting, my meta script works on single.php or category.php but not category-ID.php.


Baki Goxhaj comments:

I suppose you know that "category-ID.php" as to look like "category-6.php" and that it works where 6 is the ID of the parent category.


West Coast Design Co. comments:

Yes :) ... did you have time to look at this first hand, could use this funds plus more through paypal ...


Baki Goxhaj comments:

OK, I can do that for you on-site - info[@]wplancer[.]com :)


West Coast Design Co. comments:

Sorry, WP Questions went straight to spam … didn’t see your reply!

2010-08-17

Ehthisham tk answers:

try changing the position of meta ,i mean place that function above the category inherit function .
Some times this works .


West Coast Design Co. comments:

Hi Ehthisham,

Good answer, however that was the first thing I tried.

:)