将 WordPress 标题设置为两个单独的变量
Setting WordPress Title into Two Separate Variables
我有一个自定义的 post 类型,其中有一堆 post 的格式都像这样
Artist - Song Title
例如
The Smashing Pumpkins - Quiet
我试图将 'Artist' 放入变量 $artist 并将 'Song Title' 放入变量 $song
$artistsong = get_the_title();
$songeach = explode("-", $artistsong);
$artist = $songeach[0];
$song = $songeach[1];
但这不起作用。 Echo-ing $artist 获得完整标题
The Smashing Pumpkins - Quiet
回显 $song 不输出任何内容
如果我只是从纯文本开始,这有效,但不适用于 'get_the_title()'
$song = "The Smashing Pumpkins - Quiet";
$songeach = explode("-", $song);
$artist = trim($songeach[0]);
$song = trim($songeach[1]);
echo $artist;
//echos 'The Smashing Pumpkins'
echo $song;
//echos 'Quiet'
除了 get_the_title() 之外,还有其他方法可以将完整标题放入最初的变量中,这似乎对我不起作用,还是我遗漏了其他东西?
将此代码添加到您的 functions.php
function get_the_title_keep_hyphen( $post = 0 ) {
$post = get_post( $post );
$title = isset( $post->post_title ) ? $post->post_title : '';
$id = isset( $post->ID ) ? $post->ID : 0;
if ( ! is_admin() ) {
if ( ! empty( $post->post_password ) ) {
/**
* Filter the text prepended to the post title for protected posts.
*
* The filter is only applied on the front end.
*
* @since 2.8.0
*
* @param string $prepend Text displayed before the post title.
* Default 'Protected: %s'.
* @param WP_Post $post Current post object.
*/
$protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
$title = sprintf( $protected_title_format, $title );
} elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {
/**
* Filter the text prepended to the post title of private posts.
*
* The filter is only applied on the front end.
*
* @since 2.8.0
*
* @param string $prepend Text displayed before the post title.
* Default 'Private: %s'.
* @param WP_Post $post Current post object.
*/
$private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
$title = sprintf( $private_title_format, $title );
}
}
/**
* Filter the post title.
*
* @since 0.71
*
* @param string $title The post title.
* @param int $id The post ID.
*/
return $title;
}
并在您的 single.php
中使用此代码
$artistsong = get_the_title_keep_hyphen();
$songeach = explode(" - ", $artistsong);
$artist = $songeach[0];
$song = $songeach[1];
看到最后一行
我从return apply_filters( 'the_title', $title, $id );
变成了return $title;
因为 apply_filters
函数将连字符从 -
=> –
.
这是因为破折号。
试试 $songeach = explode("P", $artistsong);
你就会明白我的意思了。您可以尝试在艺术家和歌曲名称之间使用不同的字符 - 虽然可能不理想。
我有一个自定义的 post 类型,其中有一堆 post 的格式都像这样
Artist - Song Title
例如
The Smashing Pumpkins - Quiet
我试图将 'Artist' 放入变量 $artist 并将 'Song Title' 放入变量 $song
$artistsong = get_the_title();
$songeach = explode("-", $artistsong);
$artist = $songeach[0];
$song = $songeach[1];
但这不起作用。 Echo-ing $artist 获得完整标题
The Smashing Pumpkins - Quiet
回显 $song 不输出任何内容
如果我只是从纯文本开始,这有效,但不适用于 'get_the_title()'
$song = "The Smashing Pumpkins - Quiet";
$songeach = explode("-", $song);
$artist = trim($songeach[0]);
$song = trim($songeach[1]);
echo $artist;
//echos 'The Smashing Pumpkins'
echo $song;
//echos 'Quiet'
除了 get_the_title() 之外,还有其他方法可以将完整标题放入最初的变量中,这似乎对我不起作用,还是我遗漏了其他东西?
将此代码添加到您的 functions.php
function get_the_title_keep_hyphen( $post = 0 ) {
$post = get_post( $post );
$title = isset( $post->post_title ) ? $post->post_title : '';
$id = isset( $post->ID ) ? $post->ID : 0;
if ( ! is_admin() ) {
if ( ! empty( $post->post_password ) ) {
/**
* Filter the text prepended to the post title for protected posts.
*
* The filter is only applied on the front end.
*
* @since 2.8.0
*
* @param string $prepend Text displayed before the post title.
* Default 'Protected: %s'.
* @param WP_Post $post Current post object.
*/
$protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
$title = sprintf( $protected_title_format, $title );
} elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {
/**
* Filter the text prepended to the post title of private posts.
*
* The filter is only applied on the front end.
*
* @since 2.8.0
*
* @param string $prepend Text displayed before the post title.
* Default 'Private: %s'.
* @param WP_Post $post Current post object.
*/
$private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
$title = sprintf( $private_title_format, $title );
}
}
/**
* Filter the post title.
*
* @since 0.71
*
* @param string $title The post title.
* @param int $id The post ID.
*/
return $title;
}
并在您的 single.php
$artistsong = get_the_title_keep_hyphen();
$songeach = explode(" - ", $artistsong);
$artist = $songeach[0];
$song = $songeach[1];
看到最后一行
我从return apply_filters( 'the_title', $title, $id );
变成了return $title;
因为 apply_filters
函数将连字符从 -
=> –
.
这是因为破折号。
试试 $songeach = explode("P", $artistsong);
你就会明白我的意思了。您可以尝试在艺术家和歌曲名称之间使用不同的字符 - 虽然可能不理想。