douguachi0056 2015-02-23 17:55
浏览 64

在主页和类别wordpress页面的每7个帖子之间插入广告

I am trying to insert google ads between every 7th posts in my wordpress site with infinite scroll. i searched and found results but that didn't worked for me because the WordPress theme i am using is little complex. the result i found was to add this code

<?php $postnum++; if($postnum%5 == 0) { ?> YOUR AD CODE HERE <?php } ?>

after <?php endwhile; ?> in index.php, but in my case the loop is different from the general wordpress theme. the loop i have is like this and i want to know how can i implement the above code in my case.

loop code

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');
        }
    }

which then call this code, i made it a little shorter

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
    }

loop switch function

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">&nbsp;</p>--><?php
                }

                self::get( $post, $template );
            }
        }

        if( $ajax ){
            exit();
        }
    }

and post is called here in front-page.php file

<?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; ?>">&nbsp;</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' );
                        }
  • 写回答

1条回答 默认 最新

  • duansha6410 2015-02-23 21:34
    关注

    Try this in front-page.php. You may have to adjust it to add after correct number of posts.

    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;
    ?>
    

    it should replace this bit of code

                    if( $wp_query -> post_count > 0 ){
                        foreach( $wp_query -> posts as $post ){
                            $wp_query -> the_post();
                            $post_id = $post -> ID;
                ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图