基于页面slug的php目标页面
假设我在wordpress中有六页基于页面slug的php目标页面,php,wordpress,Php,Wordpress,假设我在wordpress中有六页 example.com/apples example.com/art example.com/bananas example.com/broccoli example.com/cars example.com/cats 我想针对的网页,鼻涕虫开始与具体的字母 if (page slug beginns with "a"){ echo 'content for pages with slug beginning with a'; } else if (
example.com/apples
example.com/art
example.com/bananas
example.com/broccoli
example.com/cars
example.com/cats
我想针对的网页,鼻涕虫开始与具体的字母
if (page slug beginns with "a"){
echo 'content for pages with slug beginning with a';
}
else if (page slug beginns with "b"){
echo 'content for pages with slug beginning with b';
}
else if (page slug beginns with "c"){
echo 'content for pages with slug beginning with c';
}
如何正确地编写这个您需要使用php函数获取第一个字符。将以下代码放在functions.php文件中
add_filter('the_content', 'change_content_by_firstCharacter');
function change_content_by_firstCharacter( $content ) {
global $post;
$post_slug = $post->post_name;
$firstCharacter = substr($post_slug, 0, 1);
if ( $firstCharacter == 'a' ) {
$content = 'content for a goes here';
} else {
return $content;
}
}
您需要使用php函数获取第一个字符。将以下代码放在functions.php文件中
add_filter('the_content', 'change_content_by_firstCharacter');
function change_content_by_firstCharacter( $content ) {
global $post;
$post_slug = $post->post_name;
$firstCharacter = substr($post_slug, 0, 1);
if ( $firstCharacter == 'a' ) {
$content = 'content for a goes here';
} else {
return $content;
}
}
关于这个答案,我想说,像这样获取URL是安全的:
/** Get the queried object and sanitize it */
$current_page = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object() );
/** Get the page slug */
$slug = $current_page->post_name;
然后:
/** Get the first character */
$slugBeginsWith = substr($slug, 0, 1);
/** Apply your logic */
if($slugBeginsWith == 'a')
{
echo 'content for pages with slug beginning with a';
}
elseif($slugBeginsWith == 'b')
{
echo 'content for pages with slug beginning with b';
}
elseif($slugBeginsWith == 'c')
{
echo 'content for pages with slug beginning with c';
}
但是你没有提到你的目标是什么。如果您在问题中提供更多信息,我们可能会提供更好的帮助 说到这个答案,我想说,像这样获取URL是安全的:
/** Get the queried object and sanitize it */
$current_page = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object() );
/** Get the page slug */
$slug = $current_page->post_name;
然后:
/** Get the first character */
$slugBeginsWith = substr($slug, 0, 1);
/** Apply your logic */
if($slugBeginsWith == 'a')
{
echo 'content for pages with slug beginning with a';
}
elseif($slugBeginsWith == 'b')
{
echo 'content for pages with slug beginning with b';
}
elseif($slugBeginsWith == 'c')
{
echo 'content for pages with slug beginning with c';
}
但是你没有提到你的目标是什么。如果您在问题中提供更多信息,我们可能会提供更好的帮助