Title: Php error after adding php code
Last modified: April 14, 2023

---

# Php error after adding php code

 *  [Lutfi Hamid](https://wordpress.org/support/users/lutfiku/)
 * (@lutfiku)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/php-error-after-adding-php-code/)
 * Hallo, nice to meet you
 * When I was adding php code in code snippet plugin, my website have error and 
   crash. Here is the error log
 * Parse error: syntax error, unexpected token “endif” in /home/xxxx/public_html/
   wp-content/plugins/code-snippets.ol/php/snippet-ops.php(504) : eval()’d code 
   on line 47
 * And here is the php code that I was adding
 *     ```wp-block-code
       // Related Post Berdasarkan Kategori
       add_action( 'generate_before_comments_container','langit_related_post' );  
       function langit_related_post() { 
       	if ( is_single() ) : ?>
       <div class="related-post__container">
       	<h2 class="related-post__header">Related Posts</h2>
       	<div class="related-post__content">
       		<?php
                   global $post;
                   $orig_post  = $post;
                   $categories = get_the_category($post->ID);
   
                       if ($categories) {
                       $categories_ids = array();
                       foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
                       $args=array(
                       'category__in'        => $category_ids,
                       'post__not_in'        => array($post->ID),
                       'posts_per_page'      => 3, // Number of related posts to display.
                       'ignore_sticky_posts' => 1
                       );
   
                       $related_query = new wp_query( $args );
   
                       while( $related_query->have_posts() ) {
                       $related_query->the_post();
                       ?>
   
       		<div class="related-post__item">
       			<div class="related-post__item--thumb">
       				<a rel="nofollow" href="<? the_permalink()?>"><?php the_post_thumbnail(array(300,150)); ?>
       				</a>
       			</div>
       			<h3 class="related-post__item--title">
       				<a href="<? the_permalink()?>"><?php the_title(); ?></a>
       			</h3>
       		</div>
   
       		<? }
         }
         $post = $orig_post;
         wp_reset_query();
         ?>
       	</div>
       </div>
       <?php 
       	endif;
       }
       ```
   

Viewing 1 replies (of 1 total)

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/php-error-after-adding-php-code/#post-16665397)
 * Hi [@lutfiku](https://wordpress.org/support/users/lutfiku/),
 * I’m not 100% certain what the issue here is, but it can presumably be eliminated
   by not using `endif;` and instead using a standard `if` statement with curly 
   braces `{}`.
 * Here’s an updated version of your snippet:
 *     ```wp-block-code
       add_action( 'generate_before_comments_container', function () {
       	if ( ! is_single() ) {
       		return;
       	}
   
       	?>
       	<div class="related-post__container">
       		<h2 class="related-post__header">Related Posts</h2>
       		<div class="related-post__content">
       			<?php
       			global $post;
       			$orig_post = $post;
       			$categories = get_the_category( $post->ID );
   
       			if ( $categories ) {
       				$category_ids = array();
   
       				foreach ( $categories as $individual_category ) {
       					$category_ids[] = $individual_category->term_id;
       				}
   
       				$args = array(
       					'category__in'        => $category_ids,
       					'post__not_in'        => array( $post->ID ),
       					'posts_per_page'      => 3, // Number of related posts to display.
       					'ignore_sticky_posts' => 1,
       				);
   
       				$related_query = new WP_Query( $args );
   
       				while ( $related_query->have_posts() ) {
       					$related_query->the_post();
       					?>
   
       					<div class="related-post__item">
       						<div class="related-post__item--thumb">
       							<a rel="nofollow"
       							   href="<? the_permalink() ?>"><?php the_post_thumbnail( array( 300, 150 ) ); ?>
       							</a>
       						</div>
       						<h3 class="related-post__item--title">
       							<a href="<? the_permalink() ?>"><?php the_title(); ?></a>
       						</h3>
       					</div>
   
       				<? }
       			}
       			$post = $orig_post;
       			wp_reset_query();
       			?>
       		</div>
       	</div>
       	<?php
       } );
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Php error after adding php code’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [3 years, 1 month ago](https://wordpress.org/support/topic/php-error-after-adding-php-code/#post-16665397)
 * Status: not a support question