Wordpress在首页列表插入广告位

首先选择首页模板

在这里插入图片描述

将代码插入到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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WordPress多功能主题Cosy破解版,优化模板速度十分适合seo。   功能说明:   独创-海报式分享,解放设计师,轻松生成文章的海报。   分享到微信、微博、qq要快要好,更要有范!   3种文章列表设计,自定义选择   多一种选择,多一种可能,让你的网站百变不单调   强大的专题文章功能   内置专题汇总页、专题文章列表页。专题可插入首页文章中,展示网站干货,增加网站阅读量   十多个小工具,用不用,反正够用   重新设计优化十余种默认小工具。又新增广告、文章(按评论、浏览、点赞等排序)小工具。   简约好看的菜单   分别自定义Web及移动端菜单,自由自主。移动端还暗藏更好看逼格的菜单。   自定义标志   网站LO GO、标题、favicon网站小标、默认头像、默认缩略图等都可自定义上传   自定义网站社交平台链接展示   文章投稿以及网站页脚上,可自定义显示社交平台的链接。   专属主题Q群交流   广交友,多交流,多分享,多乐趣   幻灯片支持滑动浏览   在任何的设备上浏览,确保您和用户更佳的用户体验   更大的幻灯片,更逼格的文章展示风格   首页顶部提供2种展示文章方式,文章幻灯片展示和杂志式展示,任意切换   丰富的广告位   文章列表、文章页内置多个广告位,让广告更好看。   更好看的置顶文章   支持WP置顶文章,分类页专有的置顶文章专栏,让置顶文章更醒目好看。   智能的裁剪文章缩略图   采用timthumb裁剪文章缩略图,裁剪最合适的图片大小,智能居中,支持WP特色缩略图。   本地化优化   决绝龟速、专治WordPress任何水土不服   好看丰富的网站图标   IconFonts和Simple Line Icons(多达1000+ Font Icon),选你所选   提供主题使用教程   主题新手配置教程一一俱全,快速配置主题   更新说明:   优化了当移动端不显示登录入口的时候,抽屉菜单无法收起的问题   优化了移动端抽屉菜单的显示效果   优化了微信、海报弹出层和tooltip的显示层级问题。   优化了导航返回顶部的时候,不怎么丝滑的问题   优化了侧栏固定下拉的效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值