0
<?php // SEARCH TAGS FOR A SPECIFIC TAG
  $tags = CollectionAttributeKey::getByHandle('recipe_tags'); // attribute handle of tags to search
  $tagToFind = 'Videos'; // declare the specific tag to find
  $selectedTags = array($page->getAttribute($tags->getAttributeKeyHandle())); // get tags associated with each page and put them in an array

  foreach ($selectedTags as $tag) { // output tags separately

    echo $tag; // ECHO TEST - check that individual tags associated with each page are outputting correctly

    if ($tag == $tagToFind) { // if $tag is equal to $tagToFind
      echo ' Found';
    } else {
      echo ' Not found';
      }
  }
?>

echo $tag;各ページに関連付けられたタグのリストを出力するので、「ビデオ」がタグのリストにあるかどうかを確認する方法に問題があると確信しています。

上記は次のリストを出力します:Breakfast Brunch Budget Meal Easy Lunch Quick Meals Supper Vegetarian VideosそしてNot foundVideos がリストにあるにもかかわらず。

また、次のように in_array を使用して「ビデオ」を探してみました。

if (in_array($tagToFind, $selectedTags, true)) { // search the array for $tagToFind - true = strict
  echo ' Found';
} else { 
    echo ' Not found';
  }

しかし、同じ結果が得られます-私はphpが初めてなので、これが簡単であれば申し訳ありません。

どんな助けでも大歓迎です。

乾杯

4

3 に答える 3

3

$selectedTags は 1 つの文字列の配列のようです。これは、1foreach回だけループするためです。

あなたはやってみてください

$selectedTags = explode(" ",$page->getAttribute($tags->getAttributeKeyHandle()));

次に、in_array関数を使用します

于 2012-08-22T07:36:31.850 に答える
0

次に、使用する方が良いです...。

if(strcmp($tag , $tagToFind))
{
    echo "Found";
}
else
{
    echo "Not found";
}

これがあなたのために働くかもしれません

于 2012-08-22T10:58:00.597 に答える
0
$page->getAttribute($tags->getAttributeKeyHandle()) 

文字列を返すようです。

その場合

$selectedTags = array($page->getAttribute($tags->getAttributeKeyHandle())); 

意味がありません - 1 つの長い文字列を含む配列を取得します。

あなたがしたいことは:

$selectedTags = $page->getAttribute($tags->getAttributeKeyHandle());

if(stristr($selectedTags, $tagToFind)){
  // do something
}
else{
  // do something else
}
于 2012-08-22T07:40:11.207 に答える