WordPress.org

Support

Support » Themes and Templates » [Resolved] [Theme: Underscores (_S) – custom build] PHP Error in my functions.php

[Resolved] [Theme: Underscores (_S) – custom build] PHP Error in my functions.php

  • Hey im currently developing my own theme.
    However I got a problem with my code in functions PHP.

    This is my code:

    function feature_post(){
    
    	if ( is_home() ){
    
    		 if ( have_posts() ) {
    			$featured = new WP_Query( 'posts_per_page=1' );
    			 while ( $featured->have_posts() ) : $featured->the_post(){ 
    
    			 	get_template_part( 'content', get_post_format() );
    
    			}endwhile;
    
    		}
    
    	add_action('feature_post','before_header');
    	}
    }

    And this is the error i am getting

    Parse error: syntax error, unexpected '{' in /Users/lucasdechow/Sites/the-damn-margin-blog/wp-content/themes/tdm/functions.php on line 184

    What have I done wrong?
    Im using the underscores

Viewing 15 replies - 1 through 15 (of 17 total)
  • Moderator Andrew Nevins

    @anevins

    Forum moderator

    It looks like there’s a missing curly brace elsewhere in the code, not in the excerpt you’ve posted.

    The excerpt closes all the curly brace’s.
    This even easy to be checked in Sublime Text 2.

    I think I might have gone wrong somewhere in the syntax of the loop?

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    May you paste the entirety of the functions.php code using PasteBin and then link us that PasteBin’d page?

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    You’re right, it is in the excerpt of code provided.
    The while loop;

    while ( $featured->have_posts() ) : $featured->the_post(){

    It should be this I think;

    if ( $featured->have_posts() ) : while ( $featured->have_posts() ) : $featured->have_posts()

    http://codex.wordpress.org/The_Loop

    @andrew
    Thanks again for looking into this.

    Should I leave the curly braces on?

    If I dont it gives me this error:
    Parse error: syntax error, unexpected T_STRING in /Users/lucasdechow/Sites/the-damn-margin-blog/wp-content/themes/tdm/functions.php on line 186

    Else it gives me the exact same error as before…

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    You should have curly braces on.
    You’ve separated the if condition and therefore you don’t need the inline if conditions on the while statement.
    Your code could be this;

    function feature_post(){
    
    	if ( is_home() ){
    
    		 if ( have_posts() ) {
    			$featured = new WP_Query( 'posts_per_page=1' );
    			 while ( $featured->have_posts() ) { 
    
    			 	get_template_part( 'content', get_post_format() );
    
    			}endwhile;
    
    		} $featured->the_post()
    
    	add_action('feature_post','before_header');
    	}
    }

    @andrew

    I got no longer the bug from before YAY!
    But however it still produces this error:

    Parse error: syntax error, unexpected T_ENDWHILE in /Users/lucasdechow/Sites/the-damn-margin-blog/wp-content/themes/tdm/functions.php on line 227

    I tried removing and moving the endwhile statement but it just doesn’t budge.

    Any ideas on how to proceed?

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    What happens when you remove the endwhile?

    This random error appears:

    Parse error: syntax error, unexpected T_STRING in /Users/lucasdechow/Sites/the-damn-margin-blog/wp-content/themes/tdm/functions.php on line 229

    Basically it says add_action doesn’t work… Hmmm

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    Can you notice any missing end curly braces?

    Nope not really.
    However i tried just a standard statement and the hook doesn’t work really.
    I don’t know might be a core problem?

    Oh jeez this gets complicated…

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    Run your functions.php code through a syntax checker like this one http://www.piliapp.com/php-syntax-check/

    It says no errors.

    But however, there must be something wrong with the hook.

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    I’ve never used hooks before, but I’ve always seen them outside of the functions. Is this where they should be? Outside of your feature_post() function?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘[Resolved] [Theme: Underscores (_S) – custom build] PHP Error in my functions.php’ is closed to new replies.
Skip to toolbar