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

时间:2015-02-23 17:55:44

标签: php wordpress wordpress-plugin wordpress-theming

我试图在我的wordpress网站的每7个帖子之间插入谷歌广告,并且无限滚动。我搜索并找到了结果,但这对我没有用,因为我使用的WordPress主题并不复杂。我发现的结果是添加此代码

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

在index.php中的<?php endwhile; ?>之后,但在我的情况下,循环与一般的wordpress主题不同。我所拥有的循环是这样的,我想知道如何在我的情况下实现上述代码。

循环代码

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

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

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

并在front-page.php文件中调用post

<?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 个答案:

答案 0 :(得分:0)

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

它应该替换这段代码

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