我需要一个功能,在三个帖子后给我添加,然后在四个帖子后给我添加第五个

问题描述 投票:0回答:2

我需要一个功能,在三个帖子之后给我添加,然后在四个帖子之后给我添加第五个,然后在八个帖子之后给我新的添加,然后在中间有一个大横幅,然后再次相同的场景相同的系列开始。转到循环,然后转到分页等...

帖子1 帖子2 图片
帖子 4 帖子 5 帖子 6
图片 帖子 8 帖子 9
帖子 10 帖子 11 帖子 12
帖子 13 帖子 14 图片

大图

帖子 16 帖子 17 图片
19 后 20 后 21 后
图片 23 后 24 后

分页 1 2 3 4 5 ....

像这样帮助我enter image description here

wordpress wordpress-theming custom-wordpress-pages
2个回答
0
投票

您需要将代码添加到主题中的循环中。如果这是您的主页,那么您将编辑index.php 中的循环。如果它是一个类别页面或者您希望它也应用于这些页面,您将在 archive.php 中编辑循环(假设您没有针对类别的模板文件,在这种情况下您也可以在那里编辑它).

循环的开始看起来像(或完全)像这样:

<?php if (have_posts()): while (have_posts()) : the_post(); ?>

在此行之前,添加一个包含广告 HTML 的数组,如下所示:

<?php 
  $ads = [
    '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
    '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
    '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
    // and so on
  ];
?>

另外,在循环开始之前,添加一个计数器变量和您希望广告显示的位置数组:

<?php 
  $count = 0; 
  $ad_positions = [3, 7, 11, 15]; // enter the desired positions
?>

在循环内,添加以下代码以递增计数器变量,并根据计数器变量输出广告:

<?php
  $count++;
  if( in_array( $count, $ad_positions ){
    $ad = array_shift( $ads ); // gets and removes the first ad from the array
    echo $ad;
  } 
?>

0
投票
<?php
$query = $GLOBALS['wp_query'];
$ads = [
    '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
    '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
    '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
    // and so on
  ];
  $count = 0; 
  $ad_positions = [3, 7, 11, 15]; // enter the desired positions
if ( have_posts() ) :
    $nuxy_mod_option = apply_filters( 'stm_me_get_nuxy_mod', 'list', 'listing_view_type' );
    if ( wp_is_mobile() ) {
        $nuxy_mod_option = apply_filters( 'stm_me_get_nuxy_mod', 'grid', 'listing_view_type_mobile' );
    }
    if ( function_exists( 'stm_is_multilisting' ) && stm_is_multilisting() && wp_is_mobile() ) {
        $mltSlug         = get_query_var( 'post_type' );
        $nuxy_mod_option = apply_filters( 'stm_me_get_nuxy_mod', 'grid', $mltSlug . '_view_type_mobile' );
    }
    $view_type = apply_filters( 'stm_listings_input', $nuxy_mod_option, 'view_type' );
    /*Filter Badges*/
    do_action( 'stm_listings_load_template', 'filter/badges' );
    if ( boolval( apply_filters( 'is_listing', array() ) ) && 'sold_car' !== $type ) {
        do_action( 'stm_listings_load_template', 'classified/filter/featured' );
    }
    ?>
    <div class="stm-isotope-sorting stm-isotope-sorting-<?php echo esc_attr( $view_type ); ?>">
        <?php
        if ( 'grid' === $view_type ) :
            ?>
        <div class="row row-3 car-listing-row car-listing-modern-grid">
            <?php
        endif;
            $template = 'partials/listing-cars/listing-' . $view_type . '-loop';
        if ( apply_filters( 'stm_is_ev_dealer', false ) || boolval( apply_filters( 'is_listing', array( 'listing', 'listing_two', 'listing_two_elementor', 'listing_three', 'listing_three_elementor', 'listing_one_elementor' ) ) ) || apply_filters( 'stm_is_dealer_two', false ) || apply_filters( 'stm_is_listing_five', false ) || apply_filters( 'stm_is_listing_six', false ) ) {
            $template = 'partials/listing-cars/listing-' . $view_type . '-directory-loop';
        } elseif ( apply_filters( 'stm_is_listing_four', false ) ) {
            $template = 'partials/listing-cars/listing-four-' . $view_type . '-loop';
        } elseif ( apply_filters( 'stm_is_boats', false ) && 'list' === $view_type ) {
            $template = 'partials/listing-cars/listing-' . $view_type . '-loop-boats';
        } elseif ( apply_filters( 'stm_is_motorcycle', false ) ) {
            $template = 'partials/listing-cars/motos/' . $view_type;
        } elseif ( apply_filters( 'stm_is_aircrafts', false ) ) {
            $template = 'partials/listing-cars/listing-aircrafts-' . $view_type;
        }
        while ( have_posts() ) :
            the_post();
            get_template_part( $template );     
            $count++;
            if( in_array( $count, $ad_positions ){
                $ad = array_shift( $ads ); // gets and removes the first ad from the array
                echo $ad;
            } )
            endwhile;
        if ( 'grid' === $view_type ) :
            ?>
        </div>
    <?php endif; ?>
    </div>
    <?php
    if ( is_null( $navigation_type ) || 'pagination' === $navigation_type ) :
stm_listings_load_pagination();
    else :
        $ppp   = $query->query['posts_per_page'];
        $paged = ( ! empty( $query->query['paged'] ) && 1 !== $query->query['paged'] ) ? $query->query['paged'] + 1 : 2;
        if ( $ppp < $query->found_posts ) {
            echo "<a class='btn stm-inventory-load-more-btn' href='#' data-ppp='" . esc_attr( $ppp ) . "' data-page='" . esc_attr( $paged ) . "' data-nav='load_more' data-offset='1'>" . esc_html__( 'Load More', 'motors' ) . '</a>';
        }
    endif;
    wp_reset_query(); //phpcs:ignore
    ?>
<?php else : ?>
    <h3><?php esc_html_e( 'Sorry, no results', 'motors' ); ?></h3>
<?php endif; ?>
<?php if ( apply_filters( 'stm_is_aircrafts', false ) ) : ?>
    <script>
jQuery(document).ready(function (){
            var showing = '<?php echo esc_html( $query->found_posts ); ?>';
            jQuery('.ac-total').text('<?php echo esc_html( $query->found_posts ); ?>');
            if(showing === '0') {
jQuery('.ac-showing').text('0');
            }
        });
    </script>
<?php endif; ?>
© www.soinside.com 2019 - 2024. All rights reserved.