• Resolved semperaye

    (@semperaye)


    Hi. I keep getting either: “The snippet has been deactivated due to an error on line 4: syntax error, unexpected ‘$html’ (T_VARIABLE)”, or an error 500 when I add the following snippet:

    add_shortcode('post_cats', 'sa_show_cats');
    function sa_show_cats() {
      $cats = wp_get_post_categories( get_the_ID(), ['fields'=>'all',]);
      $html = array();
      foreach ( $cats as $cat ) {
        $url = site_url("/category/{$cat->slug}/");
        $html[] = "<a class=\"sa-cat-links\" href=\"$url\">{$cat->name}</a>";
      }
      return implode( $html, ', ');
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hi,

    I can’t see any syntax issues in the snippet you posted. The only thing I can think of is perhaps there’s an issue with the use of the short array syntax on line 3?

    Perhaps you could try switching out that line with this:

    $cats = wp_get_post_categories( get_the_ID(), array( 'fields' => 'all' ) );

    Thread Starter semperaye

    (@semperaye)

    Thank your taking a look into this for me! Unfortunately your suggested change in code did not fix the error 500.

    Plugin Author Shea Bunge

    (@bungeshea)

    Unfortunately, I can’t really say for sure what might be causing the issue without more information, but I’ve rewritten the snippet slightly – perhaps you could try this out and see whether it works?

    add_shortcode( 'post_cats', function () {
    	$cats = wp_get_post_categories( get_the_ID(), array( 'fields' => 'all' ) );
    	$html = array();
    
    	foreach ( $cats as $cat ) {
    		$html[] = sprintf(
    			'<a class="sa-cat-links" href="%s">%s</a>',
    			get_term_link( $cat, 'category' ),
    			$cat->name
    		);
    	 }
    
    	return implode( $html, ', ' );
    } );
    Thread Starter semperaye

    (@semperaye)

    Shea,

    THANK YOU!! This works perfectly! I wonder what the issue was??!

    Plugin Author Shea Bunge

    (@bungeshea)

    it’s difficult to say, but I suspect it has to do with escaping double quotation marks on line 7. sprintf() is a great alternative for these situations.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Error 500’ is closed to new replies.