Ask your WordPress questions! Pay money and get answers fast! (more info)

Child theme functions.php: change and additions to functions WordPress

  • SOLVED

I have to do some changes and additions to child theme functions.php file (parent theme is twentyeleven, wordpress version 3.4.2):

1) activate only general feed (www.mysite.com/feed/) with its title visible in the browser menu (see screenshot, second feed does not have to display)

2) change “Continue reading →” to "Continua a leggere"

3) change name to widget area "Main sidebar" in back end area (it should be "Right sidebar").

Thanks for your help.



Answers (3)

2013-03-13

John Cotton answers:

Can you SHOW us your style.css file - that's what makes child themes work.

We need to see the contents to check that you've set the child theme up properly.


maryhellen comments:

Style sheet works and also this functions.php but only if i add this 2 funtions :

// aggiunta codice per aver un secondo menu


register_nav_menus( array(

'primary' => __( 'Primary Menu', 'progettobenesserecompleto'),

'left-sidebar' => __( 'Secondary Menu', 'progettobenesserecompleto'),

) );

/* Remove theme support for Background */



function remove_custom_theme_support(){



remove_theme_support('custom-background');



}

add_action( 'after_setup_theme', 'remove_custom_theme_support', 11 );


When i add rest of code i get blank page.


maryhellen comments:

here is style sheet header:

/*
Theme Name: Progetto Benessere Completo
Theme URI: http://wordpress.org/extend/themes/twentyeleven
Author: Maryhellen Segatta
Author URI: http://www.maryhellen.it
Description: Child theme for the Twenty Eleven theme
Template: twentyeleven
Version: 0.1.0
*/


maryhellen comments:

Any good news?


John Cotton comments:

Your stylesheet declaration looks fine.

I think Arnav is on the right track with getting some debug info. It looks like your script is failing before output, hence the white screen.

<blockquote>
But i get the php error in the server log file: error is generated by the fact that all the functions but 2, are the same
of the functions.php parent theme file:
</blockquote>

Unless parent theme functions are declared with function_exists checks wrapped around them (not all are) then you can't reuse those function names. If you want to "override" those functions, generally you've got to use remove_action to get rid of the call to the parent function and then use a corresponding add_action to call your replacement.

But you're clearly not showing all your code since <em>twentyeleven_widgets_init</em> isn't mentioned anywhere in what you've displayed.


maryhellen comments:

In the child theme i need to change "Continue reading" to "Continua a leggere" of this
function:
/**

* Returns a "Continue Reading" link for excerpts

*/

function twentyeleven_continue_reading_link() {

return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) . '</a>';

}


How can i do?

Thanks


John Cotton comments:

<blockquote>twentyeleven_continue_reading_link</blockquote>
Well that function <em>is</em> declared inside a function_exists wrapper, so you just add it to your functions.php.

So I don't think that function is your problem.


maryhellen comments:

yes, it is a problem: if i add that code i get blank page and PHP fatal error for function already declared in the parent theme. If i remove it page displays correctly.

What i need is the correct block of code to achieve the text change mentioned above in the twentyeleven_continue_reading_link function.

Can you help me please?

Thanks


John Cotton comments:

That can't be your erorr (or something is wrong with your setup). The child theme functions.php <strong><em>is loaded before</em></strong> the parent theme one.

So either you have declared it twice in that file or something is badly wrong.


maryhellen comments:

Thanks, I understood error reason.


maryhellen comments:

i have changed my question as long i understood what really i needed. Can you help me?

2013-03-13

Arnav Joy answers:

are you getting white screen when you see your site?


maryhellen comments:

yes, but without php error.


Arnav Joy comments:

try setting

define('WP_DEBUG', true) ;

in wp-config.php


maryhellen comments:

Does not work, also with blank page i do not get error.


maryhellen comments:

But i get the php error in the server log file: error is generated by the fact that all the functions but 2, are the same
of the functions.php parent theme file:

PHP Fatal error: Cannot redeclare twentyeleven_widgets_init() (previously declared in /home4/progett4/public_html/nonabusaredegliunderscore/wp-content/themes/progettobenesserecompleto/functions.php:17) in /home4/progett4/public_html/nonabusaredegliunderscore/wp-content/themes/twentyeleven/functions.php on line 445


Arnav Joy comments:

do one thing , delete all the code from functions.php and the check if theme works and if it works then try adding code one by one to functions.php and that way you will get which function/code is causing problem.


maryhellen comments:

I did. I found problem is that all functions but 2 (the only that worked) are already declared in the parent theme functions.php file.

2013-03-15

webGP answers:

Hello!

For 2) and 3) please paste the code below into your child theme functions.php file:


add_action( 'after_setup_theme', 'my_child_theme_setup' );
add_action( 'widgets_init', 'custom_twentyeleven_widgets_init', 9999 );

function my_child_theme_setup() {
remove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );
remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
add_filter( 'excerpt_more', 'custom_twentyeleven_auto_excerpt_more' );
add_filter( 'get_the_excerpt', 'custom_twentyeleven_custom_excerpt_more' );

}

function custom_twentyeleven_custom_excerpt_more( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
$output .= custom_twentyeleven_continue_reading_link();
}
return $output;
}

function custom_twentyeleven_continue_reading_link() {
return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Continua a leggere', 'Progetto Benessere Completo' ) . '</a>';
}

function custom_twentyeleven_auto_excerpt_more( $more ) {
return ' &hellip;' . custom_twentyeleven_continue_reading_link();
}

function custom_twentyeleven_widgets_init() {


register_sidebar( array(
'name' => __( 'Right Sidebar', 'twentyeleven' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );

}


EDIT:

For 1) put this line:

remove_action( 'wp_head', 'feed_links_extra', 3);

into your child theme functions.php file.


maryhellen comments:

I have tried your solutions and here are results:

1) not completey working because also feed comments is displayed and i'd like only general feed

2) seems not to work

3) works fine (sidebar name is changed)


webGP comments:

1) There is one php function responsible for displaying general feed and comments feed, so so it can be very difficult or even impossible to remove one of them.
2) I think it works, but twentyeleven theme use not only this function to display "Read more" text, there is a piece of code in content.php file, which display this text manually:

<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>

So if you don't overwrite this code to override the function will not change anything.


maryhellen comments:

Sorry, but it does not work. I have changed as you suggest:

<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>

Maybe i ahve to substitute also 'twentyeleven' with child theme directory?

Thanks


maryhellen comments:

Please can you help me to change "continue reading" text?


webGP comments:

Copy content.php file from twentyeleven theme directory and pase it into your child theme directory, after that replace line:

<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>

with:

<?php the_content( __( 'Continua a leggere', 'Progetto Benessere Completo' ) ); ?>


I've tested this solution on my localhost and it works.


maryhellen comments:

I don't want bother you but does not work in my case, here it is what exactly did:

i put this code in the child theme functions.php:

// cmabiare continue reading nei feed e negli estratti dei poat

add_action( 'after_setup_theme', 'my_child_theme_setup' );

add_action( 'widgets_init', 'custom_twentyeleven_widgets_init', 9999 );



function my_child_theme_setup() {

remove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );

remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );

add_filter( 'excerpt_more', 'custom_twentyeleven_auto_excerpt_more' );

add_filter( 'get_the_excerpt', 'custom_twentyeleven_custom_excerpt_more' );



}



function custom_twentyeleven_custom_excerpt_more( $output ) {

if ( has_excerpt() && ! is_attachment() ) {

$output .= custom_twentyeleven_continue_reading_link();

}

return $output;

}


function custom_twentyeleven_continue_reading_link() {

return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Continua a leggere', 'Progetto Benessere Completo' ) . '</a>';

}


And i placed this in the content.php (child theme directory):

<?php the_content( __( 'Continua a leggere', 'Progetto Benessere Completo' ) ); ?>

I followed exactly your instructions but in the feed i continue to read: "Continue reading".

How can fix it?

Thanks for your patience.


webGP comments:

You forgot about this piece of code:

function custom_twentyeleven_auto_excerpt_more( $more ) {

return ' &hellip;' . custom_twentyeleven_continue_reading_link();

}


maryhellen comments:

I add these lines of code but text is not changed. Any idea?

Thanks


maryhellen comments:

I did a new wordpress installation (this time wp 5.1) and i applied your code but it does not work.
I can i do?

Thanks


maryhellen comments:

I cleared the cache and in a new installation, where feed is not redirect to Feedburner, works.

In the other installation where i redirect feed to Feedburner, with FDburner plugin, does not work.

I waited for minutes to make update feedburner but text change did not happen.

How can i do?

Why it is so difficult to change a so simple text change in the child theme?

Please help me.

Thanks



webGP comments:

Please, send me all files of your child theme and I will look into them.


maryhellen comments:

Thanks, here are child theme files content:

content.php:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if ( is_sticky() ) : ?>
<hgroup>
<h3 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<!--<h3 class="entry-format"><?php _e( 'Featured', 'twentyeleven' ); ?></h3> -->
</hgroup>
<?php else : ?>
<h4 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h4>
<?php endif; ?>
<?php if ( 'post' == get_post_type() ) : ?>
<div class="entry-meta">
<?php twentyeleven_posted_on(); ?>
</div><!-- .entry-meta -->
<?php endif; ?>
<?php if ( comments_open() && ! post_password_required() ) : ?>
<!-- <div class="comments-link">
<?php comments_popup_link( '<span class="leave-reply">' . __( 'Reply', 'twentyeleven' ) . '</span>', _x( '1', 'comments number', 'twentyeleven' ), _x( '%', 'comments number', 'twentyeleven' ) ); ?>
</div>-->
<?php endif; ?>
</header><!-- .entry-header -->
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continua a leggere', 'Progetto Benessere Completo' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
<!-- <footer class="entry-meta">
<?php $show_sep = false; ?>
<?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'twentyeleven' ) );
if ( $categories_list ):
?>
<span class="cat-links">
<?php printf( __( '<span class="%1$s">Pubblicato a</span> %2$s', 'twentyeleven' ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list );
$show_sep = true; ?></span>
<?php endif; // End if categories ?>
<?php
/* translators: used between list items, there is a space after the comma */
$tags_list = get_the_tag_list( '', __( ', ', 'twentyeleven' ) );
if ( $tags_list ):
if ( $show_sep ) : ?>
<span class="sep"> | </span>
<?php endif; // End if $show_sep ?>
<span class="tag-links">
<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyeleven' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list );
$show_sep = true; ?></span>
<?php endif; // End if $tags_list ?>
<?php endif; // End if 'post' == get_post_type() ?>
<?php if ( comments_open() ) : ?>
<?php if ( $show_sep ) : ?>
<span class="sep"> | </span>
<?php endif; // End if $show_sep ?>
<span class="comments-link"><?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a reply', 'twentyeleven' ) . '</span>', __( '<b>1</b> Reply', 'twentyeleven' ), __( '<b>%</b> Replies', 'twentyeleven' ) ); ?></span>
<?php endif; // End if comments_open() ?>
<?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- #entry-meta -->
</article><!-- #post-<?php the_ID(); ?> -->


functiobs.php:

function custom_twentyeleven_widgets_init() {
register_sidebar( array(

'name' => __( 'Right Sidebar', 'twentyeleven' ),

'id' => 'sidebar-1',

'before_widget' => '<aside id="%1$s" class="widget %2$s">',

'after_widget' => "</aside>",

'before_title' => '<h3 class="widget-title">',

'after_title' => '</h3>',

) );

}

// cmabiare continue reading nei feed e negli estratti dei poat

add_action( 'after_setup_theme', 'my_child_theme_setup' );

add_action( 'widgets_init', 'custom_twentyeleven_widgets_init', 9999 );



function my_child_theme_setup() {

remove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );

remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );

add_filter( 'excerpt_more', 'custom_twentyeleven_auto_excerpt_more' );

add_filter( 'get_the_excerpt', 'custom_twentyeleven_custom_excerpt_more' );



}



function custom_twentyeleven_custom_excerpt_more( $output ) {

if ( has_excerpt() && ! is_attachment() ) {

$output .= custom_twentyeleven_continue_reading_link();

}

return $output;

}


function custom_twentyeleven_continue_reading_link() {

return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Continua a leggere', 'Progetto Benessere Completo' ) . '</a>';

}

function custom_twentyeleven_auto_excerpt_more( $more ) {


return ' &hellip;' . custom_twentyeleven_continue_reading_link();


}


/* remove all feeds but post feed and post comment feed */

remove_action( 'wp_head', 'feed_links_extra', 3);


/* Remove theme support for Background */

function remove_custom_theme_support(){

remove_theme_support('custom-background');

}
add_action( 'after_setup_theme', 'remove_custom_theme_support', 11 );



Thanks


maryhellen comments:

hello, any news?

Thanks


webGP comments:

I have no idea, for me everything is working properly.


maryhellen comments:

So, there is not a solution. Thanks