dongshi8359 2015-06-23 12:48
浏览 44
已采纳

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

I don't understand how do you use the preg_replace command in php

I created a text area in theme settings panel to display a default description for every post and would be nice if I can mix the description using {tags} or {title} to grab the post title or post tags.

I have tried using the following code but got some errors.

description code

$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;

setting panel

$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");

Is it possible to use {title} {tags} {categories} for grabbing the post title,tags and categories using any function?

  • 写回答

2条回答 默认 最新

  • duanchao4445 2015-07-15 16:02
    关注

    Anyway I just got it using 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;
    

    I was using the wrong variable but it was just a pice of code

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

    So now I can use {title} in textarea in the theme settings panel to get the post title thanks for all

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面