Wordpress在首页列表插入广告位

本文介绍了WordPress主题模板文件的基本结构及如何在其中插入广告代码。通过使用PHP条件语句,实现了在特定位置显示广告,并考虑了不同数量的帖子情况。此外,还展示了如何根据不同设备类型加载相应的模板。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先选择首页模板

在这里插入图片描述

将代码插入到while循环中

<?php
/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package WordPress
 * @subpackage Twenty_Nineteen
 * @since 1.0.0
 */

get_header();
?>

	<section id="primary" class="content-area">
		<main id="main" class="site-main">

	
		<?php if ( have_posts() ) : ?>

			<?php // Load posts loop.?>
			 <?php while ( have_posts() ) : the_post();?>
				
				<?php if ( wp_is_mobile() ) : ?>
					 <?php get_template_part( 'template-parts/content/content', 'mobile' ); ?>
				
				 <?php else: ?>
					 <?php get_template_part( 'template-parts/content/content'); ?>
				<?php endif; ?>
			
				<?php if ($wp_query->current_post == 3) : ?>  
    			<div> 广告代码 </div>  
				<?php endif;  ?>  
				<?php if ($wp_query->found_posts < 4 and $wp_query->current_post == ($wp_query->found_posts - 1)): ?>  
    			<div> 广告代码 </div>  
				<?php endif; ?>
				<?php if ($wp_query->current_post == 7) : ?>  
    			<div> 广告代码 </div>  
				<?php endif;  ?>  
				<?php if ($wp_query->found_posts < 8 and $wp_query->current_post == ($wp_query->found_posts - 1)): ?>  
    			<div> 广告代码 </div>  
				<?php endif; ?>
				

	

			
			<?php endwhile; ?>

			<?php // Previous/next page navigation.?>
			<?php twentynineteen_the_posts_navigation(); ?>
			<?php if ($wp_query->current_post == 11) : ?>  
    			<div> 广告代码 </div>  
				<?php endif;  ?>  
				<?php if ($wp_query->found_posts < 12 and $wp_query->current_post == ($wp_query->found_posts - 1)): ?>  
    			<div> 广告代码 </div>  
				<?php endif; ?>
	
			 <?php else : ?> 

			<?php // If no content, include the "No posts found" template. ?> 
			<?php get_template_part( 'template-parts/content/content', 'none' );?> 

		<?php endif; ?> 
	

		</main><!-- .site-main -->
	</section><!-- .content-area -->

<?php
get_footer();

但是有些问题,下边不会显示

还有写循环的代码

<?php
/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package WordPress
 * @subpackage Twenty_Nineteen
 * @since 1.0.0
 */

get_header();
?>
	<section id="primary" class="content-area">
		<main id="main" class="site-main">

	
		<?php if ( have_posts() ) : ?>

			<?php // Load posts loop.?>
			 <?php while ( have_posts() ) : the_post();?>
				
				<?php if ( wp_is_mobile() ) : ?>
			
					 <?php get_template_part( 'template-parts/content/content', 'mobile' ); ?>
				
				 <?php else: ?>
			
					 <?php get_template_part( 'template-parts/content/content'); ?>
				<?php endif; ?>
			
			<?php for ($i = 4; $i<=$wp_query->found_posts; $i=$i+4) :?>
				<?php if ($wp_query->current_post == $i-1) : ?>  
    			<div>
					广告代码
				</div>  
				<?php endif; ?>  
			<?php if ($wp_query->found_posts <1000 and $wp_query->current_post == ($wp_query->found_posts - 1)): ?>  
 				<div>广告代码</div>
			<?php endif; ?>
			<?php endfor; ?>
			
			
			<?php endwhile; ?>

			<?php // Previous/next page navigation.?>
			<?php twentynineteen_the_posts_navigation(); ?>

	
			 <?php else : ?> 

			<?php // If no content, include the "No posts found" template. ?> 
			<?php get_template_part( 'template-parts/content/content', 'none' );?> 

		<?php endif; ?> 
	

		</main><!-- .site-main -->
	</section><!-- .content-area -->

<?php
get_footer();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值