0

I am using shortcode and in that I want to change the title by setting the value from the database using custom tables.I have tried using the_title filter as follows

add_filter('the_title','set_page_title', 10, 2);
function set_page_title($title, $id){
   if(in_the_loop()) {


         return 'sdfdsfdsf';
   }
   return $title;
}

But my problem is I want to change this only on a specific page not all page.

This is my shortcode callback function

function bmg_company_detail_shortcode($args, $content) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'bmg_company';
    $company_id = $_GET['id'];
    $output = '';

    $result = $wpdb->get_results($wpdb->prepare("SELECT id,maincategoryid,name,website,yearfounded,supportpageurl,contactpage,country,about, isactive,contact_name,contact_email,contact_phone,support_phone,open_times,support_email, username, password  from $table_name where id=%s", $company_id));



  if($result) {
      foreach($result as $row) {
           set_page_title('',$row->name);
          $output = '
            <h3 class="bmg-company-title">'. $row->name .'</h3>

          ';
      }
  }

  return $output;
}

I want to send the parameter to the function set_page_title but that doesn't work. I want this to happen only on page called company.

0

You can use the is_page() function to only change it on a certain page. I'm assuming the slug of your company page is in fact company

add_filter( 'the_title','wpse_277956_the_title', 10, 2 );

function wpse_277956_the_title( $title, $id ){
   if( in_the_loop() && is_page( 'company' ) ) {    
         return bmg_company_detail_shortcode( false, false );
   }
   return $title;
}
| improve this answer | |
  • Why are you returning shortcode callback function in the_title callback function? Your answer solved my problem partially. Now i want to set a data to the_title callback function. I want to set from database in shortcode function – Abhilash Narayan Aug 24 '17 at 10:48
0

Getting the id from URL of the page got me fixed.

add_filter('the_title','set_page_title', 10, 2);
function set_page_title($title, $id){
   if(in_the_loop() && is_page( 'company' )) {
       $cid = isset($_GET['id']) ? $_GET['id'] : '';  
       if($cid != '') {
            return get_company_name($cid);
       } else {
            return $title;   
       }
   }
   return $title;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.