Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于页面slug的php目标页面_Php_Wordpress - Fatal编程技术网

基于页面slug的php目标页面

基于页面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 (

假设我在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 (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';
}
但是你没有提到你的目标是什么。如果您在问题中提供更多信息,我们可能会提供更好的帮助