Community Forums › Forums › Archived Forums › General Discussion › Need help: Fatal Error – 'Cannot Redeclare function' error.
Tagged: functions.php, php, redeclare function
- This topic has 8 replies, 2 voices, and was last updated 9 years, 10 months ago by Ren Ventura.
-
AuthorPosts
-
January 16, 2015 at 12:46 pm #137689FireSamuraiMember
Hi,
I'm using a custom post type to display recipes on my cooking site, http://thecookingdish.com. I have a function that displays the recipe posts in a grid instead of being linear on the recipe archive pages.
The problem is that when I click 'Add New' on the Recipe custom post type, I get the following error:
Fatal error: Cannot redeclare recipes_post_image() (previously declared in /Users/ctmower/Documents/MAMP Sites/ctm/thecookingdish/wp-content/themes/seasoning2014/functions.php:233) in /Users/ctmower/Documents/MAMP Sites/ctm/thecookingdish/wp-content/themes/seasoning2014/functions.php on line 231
I'm not sure what's causing it and could really use some insight or even a solution. Thanks so much!
Line 231:
function recipes_post_image() {
Line 233:
$size = 'recipes-featured';
The offending function:
/** Determine how recipe archive pages look */ add_filter( 'pre_get_posts', 'seasoning2014_archive_query' ); function seasoning2014_archive_query( $query ) { if( $query->is_main_query() && $query->is_category( array( 'the-cooking-dish-recipes', 'appetizer-recipes', 'beverage-recipes', 'bread-recipes', 'candy-recipes', 'main-dish-recipes', 'side-dish-recipes', 'snack-recipes', 'soup-and-salad-recipes', 'dessert-recipes' )) || $query->is_tax( array( 'course', 'cuisine', 'ingredient', )) || $query->is_post_type_archive( 'recipe' ) ){ // $query->set( 'orderby', 'title' ); // $query->set( 'order', 'ASC' ); $query->set( 'posts_per_page', '15' ); // Divisible by 3 //* Remove the post info remove_action( 'genesis_entry_header', 'genesis_post_info', 12 ); //* Swap out the featured image remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 ); add_action( 'genesis_entry_content', 'recipes_post_image', 0 ); function recipes_post_image() { $size = 'recipes-featured'; if ( !is_singular() && genesis_get_option( 'content_archive_thumbnail' ) ) { $img = genesis_get_image( array( 'format' => 'html', 'size' => 'recipes-featured', 'context' => 'archive', 'attr' => array( 'class' => 'alignnone post-image' ) ) ); if ( ! empty( $img ) ) printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img ); } } //* Remove the post content remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); remove_action( 'genesis_entry_content', 'genesis_do_post_content_nav', 12 ); remove_action( 'genesis_entry_content', 'genesis_do_post_permalink', 14 ); //* Remove the post meta remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_open', 5 ); remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_close', 15 ); remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); //* Swap out the loops remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'seasoning2014_recipes_loop' ); } }
The custom loop called at end of function:
function seasoning2014_recipes_loop() { //* Use old loop hook structure if not supporting HTML5 if ( ! genesis_html5() ) { genesis_legacy_loop(); return; } //* The loop if ( have_posts() ) : while ( have_posts() ) : the_post(); do_action( 'genesis_before_entry' ); printf( '<article %s>', genesis_attr( 'entry' ) ); do_action( 'genesis_entry_header' ); do_action( 'genesis_before_entry_content' ); // printf( '<div %s>', genesis_attr( 'entry-content' ) ); do_action( 'genesis_entry_content' ); // echo '</div>'; //* end .entry-content do_action( 'genesis_after_entry_content' ); do_action( 'genesis_entry_footer' ); echo '</article>'; do_action( 'genesis_after_entry' ); endwhile; //* end of one post do_action( 'genesis_after_endwhile' ); else : //* if no posts exist do_action( 'genesis_loop_else' ); endif; //* end loop }
Thanks for taking the time to look at this.
http://thecookingdish.comJanuary 16, 2015 at 1:27 pm #137692Ren VenturaMemberHey, Chris. Could you share a link to your full functions.php file (i.e. GitHub, Pastebin, etc.)? When I pasted your code into a text editor, it's all funky and difficult to debug without correcting all of the formatting issues.
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
January 16, 2015 at 1:32 pm #137693FireSamuraiMemberI've created a gist, thank you for taking a look:
TCD functions.phpJanuary 16, 2015 at 1:55 pm #137699Ren VenturaMemberNothing is standing out to me as of now. That error doesn't even make sense because you aren't redeclaring the function at all, let alone on those specific lines. Can you remove lines 228-248, repeat the process and let me know if you get the same error?
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
January 16, 2015 at 2:01 pm #137700FireSamuraiMemberI know, that's what's crazy about it. If I remove those lines of code I don't get that error, but then I dont' get the image I want either. Those lines are only meant to swap out the featured image size. Maybe if there's another way of doing that, I can circumvent the problem.
January 16, 2015 at 2:11 pm #137702FireSamuraiMemberFixed it. The answer makes total sense now that I step back and look at it again.
I was defining the recipes_post_image() function inside the seasoning2014_archive_query() function. The problem is that the seasoning2014_archive_query() gets called each pre_get_posts action.
The second time that happens I was defining the recipes_post_image() function for a second time, causing the error.
January 16, 2015 at 2:29 pm #137704Ren VenturaMemberVery nice. That's exactly what I was just looking at and thinking through! Good job.
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
January 16, 2015 at 2:32 pm #137705FireSamuraiMemberThanks, and thank you for taking the time. Have a great weekend.
January 16, 2015 at 2:39 pm #137706Ren VenturaMemberYou do the same.
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.