Как добавить цикл WordPress под функцию в WordPress - PullRequest
0 голосов
/ 04 марта 2019

Я хочу добавить гугл-добавления в отдельное новостное сообщение.Я использовал приведенный ниже код для этого, но когда я поставил страницу цикла WordPress не работает.Как я могу решить эту проблему.Я хочу получить изображение из пользовательского поста.здесь я приложил то, что я делаю в моей функции. php

function prefix_insert_post_ads( $content ) {
        //The last condition here ensures that ads are only added to posts
        if ( is_single() && !is_admin() && get_post_type() === 'post' ) {
            return prefix_insert_ads( $content );
        }

        return $content;
    }

    function prefix_insert_ads( $content ) {

        $closing_p = '</p>';
        $paragraphs = explode( $closing_p, $content );
        foreach ($paragraphs as $index => $paragraph) {
            $paragraphs[$index] .= $closing_p;

            if ( in_array($index, array(3)) ) {
                $the_query = new WP_Query( array( 'post_type' =>'advertisement','orderby'=>'post_date','order'=>'desc','posts_per_page' => '2',$i=-1, 'offset'=> 1) );
         if ( have_posts())   : while ( $the_query->have_posts() ) : $the_query->the_post();



                $thumbnail_id = get_post_thumbnail_id(); 
                $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
                $thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true); 


                //Replace the html here with a valid version of your ad code
                $paragraphs[$index] .= '<div class="hidden-lg"><img class="img img-responsive center-img" src="http://www.test.oddly.co/hospitality/wp-content/uploads/2019/02/ad4.jpg" height="" width="300px" alt=""></div>';

endwhile;
endif; wp_reset_Query();

            }
            else if ( in_array($index, array(5)) ) {
                //Replace the html here with a valid version of your ad code
                $paragraphs[$index] .= '<div class="hidden-lg"><img class="img img-responsive center-img" src="http://www.test.oddly.co/hospitality/wp-content/uploads/2019/02/ad4.jpg" height="" width="300px" alt=""></div>';
            }
            else if ( in_array($index, array(9)) ) {
                //Replace the html here with a valid version of your ad code
                $paragraphs[$index] .= '<div class="hidden-lg"><img class="img img-responsive center-img" src="http://www.test.oddly.co/hospitality/wp-content/uploads/2019/02/ad4.jpg" height="" width="300px" alt=""></div>';
            }



        }

        return implode( '', $paragraphs );
    } 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...