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

时间:2015-06-23 12:48:19

标签: php wordpress

我不明白你如何在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}来使用任何功能获取帖子标题,标签和类别?

2 个答案:

答案 0 :(得分:0)

你可以写一些shortcodes来做到这一点。唯一的区别是它会定位[title / tags / etc]而不是{title / tags / etc},而应该能够在使用它们时自动解析它们。

答案 1 :(得分:0)

无论如何,我只是使用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);

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