如何在wordpress主题设置面板中使用preg_replace来获取文章标题或标签


How can I use preg_replace in wordpress theme settings panel to get post title or tags

我不明白如何在php中使用preg_replace命令

我在主题设置面板中创建了一个文本区域,以显示每个帖子的默认描述,如果我可以使用{tags}或{title}混合描述来获取帖子标题或帖子标签,那就太好了。

我尝试使用以下代码,但出现了一些错误。

描述码

$description = get_post_meta($post->ID, 'field_description', true); if(!empty($description)) {
$seo_desc = get_option('mytheme_seo_description');
echo $description; 
} else if(!empty($seo_desc)) {
echo $seo_desc;

设置面板

$options[] = array( "name" => __('SEO DESCRIPTION','mytheme'),
"desc" => __('Mix {title} {tags} {categories} in a default description to grab post title, tags and categories','mytheme'),
"id" => $shortname."_seo_description",
"std" => "",
"type" => "textarea");

是否可以使用{title}{tags}{categories}来获取使用任何函数的帖子标题、标签和类别?

您可以编写一些短代码来做到这一点。唯一的区别是,它将以[title/tags/etc]为目标,而不是{title/ttags/etc},并且如果使用它们,它应该能够自动解析它们。

不管怎样,我只是用str_replace 得到的

$description = get_post_meta($post->ID, 'field_description', true);
if(!empty($description)) {
$seo_desc = get_option('mytheme_seo_description');
$seo_desc = str_replace("{title}", $post->post_title, $seo_desc);
echo $description; 
} else if(!empty($seo_desc)) {
echo $seo_desc;

我用错了变量但这只是代码的一部分

 $seo_desc = str_replace("{title}", $post->post_title, $seo_desc);

所以现在我可以在主题设置面板的文本区域中使用{title}来获得帖子标题感谢所有