Php 在主页和分类wordpress页面的每第7个帖子之间插入广告
我正试图在我的wordpress网站上每隔7篇帖子之间插入谷歌广告,并带有无限滚动条。我搜索并找到了结果,但这对我来说不起作用,因为我使用的WordPress主题有点复杂。我发现的结果是添加了这个代码 你的广告代码在这里 在index.php之后,但是在我的例子中,循环不同于一般的wordpress主题。我的循环是这样的,我想知道如何在我的例子中实现上面的代码 循环码Php 在主页和分类wordpress页面的每第7个帖子之间插入广告,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我正试图在我的wordpress网站上每隔7篇帖子之间插入谷歌广告,并带有无限滚动条。我搜索并找到了结果,但这对我来说不起作用,因为我使用的WordPress主题有点复杂。我发现的结果是添加了这个代码 你的广告代码在这里 在index.php之后,但是在我的例子中,循环不同于一般的wordpress主题。我的循环是这样的,我想知道如何在我的例子中实现上面的代码 循环码 static function loop( $template ) { global $wp_query;
static function loop( $template ) {
global $wp_query;
echo '<input type="hidden" id="query-' . $template . '" value="' . urlencode( json_encode( $wp_query -> query ) ) . '" />';
if ( count( $wp_query->posts) > 0 ) {
if( self::is_grid( $template ) ){
?>
<div class="loop-container-view grid">
<?php self::loop_switch( $template , 1 ); ?>
</div>
<?php
}else{
?>
<div class="loop-container-view list" id="contloop">
<?php self::loop_switch( $template , 0 ); ?>
</div>
<?php
}
get_template_part('pagination');
} else {
get_template_part('loop', '404');
}
}
这就叫这个代码,我把它缩短了一点
static function get( $post , $template = 'blog_page' ){
$meta = meta::get_meta( $post -> ID , 'settings' );
if( isset( $meta['safe'] ) ){
if( meta::logic( $post , 'settings' , 'safe' ) ){
$classes = ' nsfw';
}else{
$classes = ' ';
}
}else{
$classes = ' ';
}
?>
<!-- post -->
<article id="post-<?php echo $post -> ID; ?>" <?php post_class( 'post ' . $classes , $post -> ID ); ?>>
</article>
<?php
}
环路开关功能
static function loop_switch( $template = '' , $grid = 1 ) {
global $wp_query;
if ( !empty( $template ) ) {
$ajax = false;
} else {
$query = array();
$template = isset( $_POST['template'] ) && strlen( $_POST['template'] ) ? $_POST['template'] : exit();
$query = isset( $_POST['query'] ) && !empty( $_POST['query'] ) ? (array)json_decode( urldecode( $_POST['query'] ) ) : exit();
$query['post_status'] = 'publish';
$wp_query = new WP_Query( $query );
$grid = isset($_POST['grid']) ? (int)$_POST['grid'] : 1;
$ajax = true;
}
$template = str_replace( array( '_hot' , '_new' , '_like' ) , '' , $template );
if( $grid == 1 ){
$k = 1;
$i = 1;
$nr = $wp_query->post_count;
if (layout::get_length(0, $template) == 940 ) {
$div = 3;
} else {
$div = 2;
}
foreach ($wp_query->posts as $post) {
$wp_query->the_post();
if ($i == 1) {
if (( $nr - $k ) < $div) {
$classes = 'class="last"';
} else {
$classes = '';
}
echo '<div ' . $classes . '>';
}
self::grid( $post, $template );
if ($i % $div == 0) {
echo '</div>';
$i = 0;
}
$i++;
$k++;
}
if ($i > 1) {
echo '</div>';
}
}else{
foreach( $wp_query->posts as $index => $post ) {
$wp_query->the_post();
if ($index > 0) {
?><!--<p class="delimiter"> </p>--><?php
}
self::get( $post, $template );
}
}
if( $ajax ){
exit();
}
}
post在front-page.php文件中调用
<?php
$wp_query = new WP_Query(array( 'page_id' => options::get_value( 'front_page' , 'page' ) ) );
if( $wp_query -> post_count > 0 ){
foreach( $wp_query -> posts as $post ){
$wp_query -> the_post();
$post_id = $post -> ID;
?>
<article id="post-<?php the_ID(); ?>" <?php post_class() ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
<!-- post meta top -->
<?php
if( meta::logic( $post , 'settings' , 'meta' ) ){
get_template_part( 'post-meta-top' );
}
?>
</header>
<div class="entry-content">
<?php
/* if show featured image */
if( options::logic( 'blog_post' , 'show_featured' ) ){
if( has_post_thumbnail ( $post -> ID ) ){
$src = wp_get_attachment_image_src( get_post_thumbnail_id( $post -> ID ) , 'full' );
?>
<div class="featimg circle">
<div class="img">
<?php
ob_start();
ob_clean();
get_template_part( 'caption' );
$caption = ob_get_clean();
?>
<a href="<?php echo $src[0]; ?>" title="<?php echo $caption; ?>" class="mosaic-overlay" rel="prettyPhoto-<?php echo $post -> ID; ?>"> </a>
<?php the_post_thumbnail( '600x200' ); ?>
<?php
if( strlen( trim( $caption) ) ){
?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
<?php
}
?>
</div>
</div>
<?php
}
}
?>
</div>
<footer class="entry-footer">
<div class="share">
<?php get_template_part( 'social-sharing' ); ?>
</div>
<div class="excerpt">
<?php the_content(); ?>
<?php wp_link_pages(); ?>
</div>
</footer>
</article>
<?php
}
}else{
/* not found page */
get_template_part( 'loop' , '404' );
}
在front-page.php中试试这个。您可能需要调整它以添加正确数量的帖子
if( $wp_query -> post_count > 0 ){
$postnum = 0;
foreach( $wp_query -> posts as $post ){
$postnum++;
if( $postnum%5 == 0 ) {
echo '<p> ---- insert ad here ---- </p>';
}
$wp_query -> the_post();
$post_id = $post -> ID;
?>
环路开关功能在哪里?“你叫什么?”我用你想要的细节编辑了我的问题。谢谢你的帮助,但我不知道该把代码放在哪里,因为我不懂编程,这就是为什么我要使用现成的主题。你能帮我把代码放在front-page.php的什么地方吗?我已经将front-page.php的复杂代码复制到了这把小提琴上。我试图复制您的代码并将其放置在多个位置,但它显示错误。thanksI已经调整了答案,但如果您不熟悉代码,最好让开发人员/程序员进行更改。虽然问起来很尴尬,但我将此代码放在front-page.php的何处以及如何放置,您能否将其放在我在上面的注释中发送的fiddle文件中。我问过主题的开发者,但他说他不能定制主题。
if( $wp_query -> post_count > 0 ){
foreach( $wp_query -> posts as $post ){
$wp_query -> the_post();
$post_id = $post -> ID;
?>