• I have problems inserting codes in index.php and content.php!

    I want to insert php and other codes (e.g Google Adsense) in index.php?

    I would like to insert this before endwhile but it doesn’t work:

    ` <?php if( $wp_query->current_post == 0 ) {
    ADSENSE CODE
    }?>`

    <?php if ( have_posts() ) : ?>
    
    			<?php if ( is_home() && ! is_front_page() ) : ?>
    				<header>
    					<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
    				</header>
    			<?php endif; ?>
    
    			<?php
    			// Start the loop.
    			while ( have_posts() ) : the_post();
    
    				/*
    				 * Include the Post-Format-specific template for the content.
    				 * If you want to override this in a child theme, then include a file
    				 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
    				 */
    				get_template_part( 'content', get_post_format() );
    
    			// End the loop.
    
    			endwhile;
    
    			// Previous/next page navigation.
    			the_posts_pagination( array(
    				'prev_text'          => __( 'F?reg?ende sida', 'twentyfifteen' ),
    				'next_text'          => __( 'N?sta sida', 'twentyfifteen' ),
    				'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Sida', 'twentyfifteen' ) . ' </span>',
    			) );
    
    		// If no content, include the "No posts found" template.
    		else :
    			get_template_part( 'content', 'none' );
    
    		endif;
    		?>

    Content.php
    How can I insert code after ‘the_content( sprintf(‘ but before the “keep reading” here:

    <div class="entry-content">
    		<?php
    
    			/* translators: %s: Name of current post */
    			the_content( sprintf(
                                   __('Keep reading %s', 'twentyfifteen' ),
    				the_title( '<span class="screen-reader-text">', '</span>', false )
    			) );
    
    			wp_link_pages( array(
    				'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Sidor:', 'twentyfifteen' ) . '</span>',
    				'after'       => '</div>',
    				'link_before' => '<span>',
    				'link_after'  => '</span>',
    				'pagelink'    => '<span class="screen-reader-text">' . __( 'Sida', 'twentyfifteen' ) . ' </span>%',
    				'separator'   => '<span class="screen-reader-text">, </span>',
    			) );
    		?>
    
    	</div><!-- .entry-content -->

    Thanks for any help!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m guessing you’re not using the right structure for PHP.

    You use the <?php and ?> tags if you’re not already in a PHP block. If you’re in a PHP block, just write the PHP you want.

    AdSense code is HTML and JavaScript so you need to insert it outside of a PHP block.

    So the code you probably need is something like:

    … more php code…
    if( $wp_query->current_post == 0 ) { ?>
    ADSENSE CODE
    <?php }
    … more php code…

    Thread Starter caroline

    (@makeupedia)

    Thank you Marios!

    I didn’t understand that the opening and closing tag was mandatory in the end. It works fine now, glad for your help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Insert code in index.php & content.php’ is closed to new replies.