WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [Theme: Underscores (_S) - custom build] PHP Error in my functions.php (18 posts)

  1. Lucas
    Member
    Posted 1 year ago #

    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

  2. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

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

  3. Lucas
    Member
    Posted 1 year ago #

    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?

  4. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

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

  5. Lucas
    Member
    Posted 1 year ago #

  6. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

    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

  7. Lucas
    Member
    Posted 1 year ago #

    @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...

  8. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

    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');
    	}
    }
  9. Lucas
    Member
    Posted 1 year ago #

    @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?

  10. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

    What happens when you remove the endwhile?

  11. Lucas
    Member
    Posted 1 year ago #

    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

  12. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

    Can you notice any missing end curly braces?

  13. Lucas
    Member
    Posted 1 year ago #

    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...

  14. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

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

  15. Lucas
    Member
    Posted 1 year ago #

    It says no errors.

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

  16. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

    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?

  17. Lucas
    Member
    Posted 1 year ago #

    I tried putting the hook code outside and correctly it worked.
    I don't know what to do about the loop though, it still gives me 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 188

    Hmm perhaps I use the endwhile wrongly...

  18. Lucas
    Member
    Posted 1 year ago #

    I fixed it using this code instead.

    Just if anyone finds this thread and wonders how it is done this is how to do it:

    First we make a function (in functions.php) called featured which will create a custom WP_Query.

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

    Now I have created a hook using this function:

    function tdm_before_header() {
        do_action('tdm_before_header');
    }

    ...and calls it from the theme using the function itself (note: excerpt) :

    ...
    <div id="page" class="hfeed site cf">
    	<?php tdm_before_header(); ?>
    	<header id="masthead" class="site-header" role="banner">
    ...

Topic Closed

This topic has been closed to new replies.

About this Topic