0

Wordpressタイトルの特定の文字の前にあるものをすべて削除する必要があります

私はすでにここで見つけた別のコードを試しましたが、それを機能させることができませんでした私はこのようなものが必要です

echo strstr(get_the_title(),"-",true);

また

echo $str = 'get_the_title()';
$str = substr($str, 0, strpos($str, '-'));

最初のコードは通常のタイトルを出力するだけです

2番目のimでは、通常の文字の代わりにphpコードを実行する方法がわかりません。

アップデート:

harryg&jrodのおかげで今は動作しています

$str = get_the_title(); //This would be your post title (get_the_title)
$char =  " - "; //Define the separator
$strpos = strpos($str, $char); //Find out where it occurs
$str = substr($str, $strpos+strlen($char)); //Extract the substring after the separator
echo $str;

どういうわけかワードプレスは私のhpyhenを一気に会話するので私はそれを追加しました

remove_filter( 'the_title', 'wptexturize' );

私のfuntions.phpに、それは機能しました。多分それは将来誰かを助けるでしょう。すべての回答をありがとう!

4

3 に答える 3

0

これにも使用できますpreg_replace

$str = 'Lorem ipsum dolor sit amet - consetetur sadipscing elitr'; // your title
$sep =  ' - '; // separator
$short = preg_replace('/^(.*?)'.$sep.'(.*)$/si', '$2', $str, 1); // your short title
echo $short; // result: "consetetur sadipscing elitr"

これにより、区切り文字が最初に出現する前のすべての文字が置き換えられます。セパレータが複数回存在し、セパレータが最後に出現$strする前にすべてを置き換えたい場合は、次を使用します。

$short = preg_replace('/^(.*)'.$sep.'(.*)$/si', '$2', $str, 1); // your short title

特殊文字を使用する場合$sepは、それらをエスケープする必要があることに注意してください。

于 2013-02-13T18:03:06.240 に答える
0

これで済むはず…

$str = "this comes before - this comes after"; //This would be your post title (get_the_title)
$char =  " - "; //Define the separator
$strpos = strpos($str, $char); //Find out where it occurs
$str = substr($str, $strpos+strlen($char)); //Extract the substring after the separator
echo $str; //Result will be "this comes after"

$charタイトルにあるセパレーターと正確に一致する必要があることに注意してください。html エンコードされている場合 ( —em-dash など)、html エンティティをセパレータとして使用する必要があります。また、セパレーターの最初のインスタンスでタイトルの開始をトリミングします。セパレーターが複数ある場合は、タイトルのどこにあるかに応じてコードを調整する必要があります。

于 2013-02-13T17:10:43.330 に答える
-1

$char="enter the character before which you want to delete the connect here";
$str =get_the_title(); $substr = explode($char,$str); echo $substr[1];

分解機能を使用すると、文字列を特定の文字の配列に分割できます

于 2013-02-13T17:05:01.323 に答える