调用函数array_splice php时出现问题

时间:2019-05-18 06:42:51

标签: php wordpress function content-management-system

我不久前正在学习PHP语言,并且尝试执行一些功能,它以一种奇怪的方式工作

我想在单个帖子的文本之间显示相关的帖子,对该函数的调用起作用,但是在文本上方, array_splice似乎不起作用

我在做什么错??

add_filter( 'the_content', 'related_inside_content' );
function related_inside_content( $content ) {
          $ads = postss(); //CALL FUNCTION!
          $p_arrayx = explode('</p>', $content );
          $p_count = 10;
          if( !empty( $p_arrayx ) ){
              array_splice( $p_arrayx, $p_count, 0, $ads );
              foreach( $p_arrayx as $key=>$value ){
                  $output .= $value;
               }
          }

      return $output;
 }

调用函数

function postss(){
$tags = wp_get_post_terms( get_queried_object_id(), 'customtags', ['fields' => 'ids'] );
$args = [
    'post__not_in'        => array( get_queried_object_id() ),
    'posts_per_page'      => 2,
    'ignore_sticky_posts' => 2,
    'orderby'             => 'relevance',
    'tax_query' => [
        [
            'taxonomy' => 'customtags',
            'terms'    => $tags
        ]
    ]
];
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
        while( $my_query->have_posts() ) :
            $my_query->the_post();
            $title =  get_the_title(); 
            $image =  get_the_post_thumbnail_url(get_the_ID(),'posts-thumb'); 
            $imagehtml = '<li>'.'<img src="'.$image . '">'.'<span class="overlayer"></span>'.'<span class="title">'. $title. '</span>'.'</li>'; 
            echo $imagehtml;
            wp_reset_postdata();
          endwhile;
 }  

}

1 个答案:

答案 0 :(得分:0)

我认为只需添加循环echo get_the_content()