• 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

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

    (@bungeshea)

    Hi @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:

    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.