如果存在字符串,请在_content中检查


Check in the_content if a string exists

情况如下。

我已经设置了一个使用Magento Wordpress集成网站。集成工作,所以我可以在Magento网站调用任何我想从wordpress。

我想在产品页面上显示一篇来自wordpress的帖子,其中包含一个特定的单词。在我看来,我必须在帖子的_content()中搜索产品的标题,然后带上我需要的post_meta。

问题是我不能让它工作。我试过这个:

<?php $name_of_product = $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
<?php echo $name_of_product ; ?>
<?php   $args = array( 'post_type' => 'avada_portfolio', 'posts_per_page' => 103 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();           
    <?php  
        $pos = strpos( get_the_content(), "[Dominos]" );
        var_dump($pos);
        if ( ! (FALSE == $pos) ) {  
        the_content();  
        the_title(); 
        }
        else{echo ("NOTHING HERE");}
    echo '</div>';
    endwhile;                                       
?>

但没有起作用。有什么建议吗?

基于var_dump()输出,您应该能够简单地使用:

if ( strpos( get_the_content(), '13801580' ) !== false) {
    the_content();  
    the_title(); 
}

首先检查您的$content,也许它是空的:

<?php
$args = array( 'post_type' => 'avada_portfolio', 'posts_per_page' => 103 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();           
        $content = get_the_content();
        if($content){
          echo 'content present';
        }else{
          echo 'no content!!!';
        }
    endwhile;                                       

如果不是空的,这是短代码,你可以使用下一个代码,例如:

 $content = '[mwi_product sku="13801580,13801584,13801584,13801578,13801580" title="true" title_tag="h2" desc="false" img="true" img_width="250" price="false" type="false" btn_color="blue" btn_link="button" cols="9"/]';
    $need_find = array('13801580', '13801584');
    foreach($need_find as $find){
      if(strpos( $content,  $find) !== false) { 
          echo 'Find your text: '.$find.'</br>';
      }
    }