• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Need help: Fatal Error – 'Cannot Redeclare function' error.

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Need help: Fatal Error – 'Cannot Redeclare function' error.

This topic is: resolved

Tagged: functions.php, php, redeclare function

  • This topic has 8 replies, 2 voices, and was last updated 8 years ago by Ren Ventura.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • January 16, 2015 at 12:46 pm #137689
    FireSamurai
    Member

    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( '&lt;article %s&gt;', genesis_attr( 'entry' ) );
    
    				do_action( 'genesis_entry_header' );
    
    				do_action( 'genesis_before_entry_content' );
    				// printf( '&lt;div %s&gt;', genesis_attr( 'entry-content' ) );
    					do_action( 'genesis_entry_content' );
    				// echo '&lt;/div&gt;'; //* end .entry-content
    				do_action( 'genesis_after_entry_content' );
    
    				do_action( 'genesis_entry_footer' );
    
    			echo '&lt;/article&gt;';
    
    			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.com
    January 16, 2015 at 1:27 pm #137692
    Ren Ventura
    Member

    Hey, 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 #137693
    FireSamurai
    Member

    I've created a gist, thank you for taking a look:
    TCD functions.php

    January 16, 2015 at 1:55 pm #137699
    Ren Ventura
    Member

    Nothing 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 #137700
    FireSamurai
    Member

    I 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 #137702
    FireSamurai
    Member

    Fixed 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 #137704
    Ren Ventura
    Member

    Very 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 #137705
    FireSamurai
    Member

    Thanks, and thank you for taking the time. Have a great weekend.

    January 16, 2015 at 2:39 pm #137706
    Ren Ventura
    Member

    You do the same.


    Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren

  • Author
    Posts
Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble