个性化阅读
专注于IT技术分析

在首页的每第7个帖子和类别WordPress页面之间插入广告

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

<?php $ postnum ++; if($ postnum%5 == 0){?>你的广告代码在这里<?php}?>

在<?php之后?>在index.php中, 但就我而言, 该循环不同于一般的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文件中将帖子称为此处

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


在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;
            ?>
赞(0)
未经允许不得转载:srcmini » 在首页的每第7个帖子和类别WordPress页面之间插入广告

评论 抢沙发

评论前必须登录!