SHARE
TWEET

Edit title and extra text without menu iteam change

hmbashar Oct 23rd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /**
  3.  * Replace post/page title on home, single and archive pages.
  4.  *
  5.  * @param string $title   Post title
  6.  * @param int    $post_id Post ID
  7.  *
  8.  * @return string New post tilte
  9.  */
  10. function wpse_309151_get_replace_default_title_from_meta( $title, $post_id ) {
  11.  
  12.     $post_type = get_post_type( $post_id );
  13.  
  14.     if( $post_type === 'post') {
  15.  
  16.         $disc_link = '#';
  17.  
  18.         $dis_text = 'Commerce Content is independent of Editorial and Advertising, and if you buy something our posts, we may get a small share of the sale';
  19.  
  20.         $dis_text_two = 'for more';
  21.  
  22.         $new_title = sprintf('<div class="bashar-amazon-dis-text"><p>%s <a href="%s">Click Hare</a> %s</p></div>', $dis_text, $disc_link, $dis_text_two);
  23.  
  24.         if( $new_title && !empty( $new_title ) ) {
  25.             return $new_title.$title;
  26.         }
  27.     }
  28.  
  29.     return $title;
  30. }
  31.  
  32. add_filter( 'the_title', 'wpse_309151_get_replace_default_title_from_meta', 10, 2 );
  33.  
  34. /**
  35.  * Restore default post/page title in navigation
  36.  *
  37.  * @param string   $title The menu item's title.
  38.  * @param WP_Post  $item  The current menu item.
  39.  * @param stdClass $args  An object of wp_nav_menu() arguments.
  40.  * @param int      $depth Depth of menu item. Used for padding.
  41.  *
  42.  * @return string Restored post title
  43.  */
  44. function wpse_309151_get_restore_default_title_for_navigation( $title, $item, $args, $depth ) {
  45.  
  46.     // Remove filter to not affect title
  47.     remove_filter( 'the_title', 'wpse_309151_get_replace_default_title_from_meta', 10, 2 );
  48.  
  49.     $post_id = $item->object_id;
  50.     $title   = get_the_title( $post_id );
  51.  
  52.     // Add the title filter back
  53.     add_filter( 'the_title', 'wpse_309151_get_replace_default_title_from_meta', 10, 2 );
  54.  
  55.     return $title;
  56. }
  57.  
  58. add_filter( 'nav_menu_item_title', 'wpse_309151_get_restore_default_title_for_navigation', 10, 4 );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top