Hot answers tagged

35

The WPSEO plugin by Yoast has a filter for the title: 'wpseo_title'. You'll need to add something like this: add_filter('wpseo_title', 'filter_product_wpseo_title'); function filter_product_wpseo_title($title) { if( is_singular( 'product') ) { $title = //your code } return $title; } More info at the WordPress SEO API Docs page.


34

Try with this: <?php echo get_the_title( $ID ); ?>


20

Updating the posts $all_posts = get_posts( 'posts_per_page' => -1, 'post_type' => 'post' ); foreach ( $all_posts as $single ) { wp_update_post( array( 'ID' => $single->ID, 'post_title' => to_title_case( $single->post_title ) // see function below )); } Converting a string to "Title Case" And, while not ...


18

Problem Description: Let me rephrase the question first. You want to: Set new title to all post and page type from a meta field. You want this to happen everywhere (home page, single page, widgets etc.) However, you don't want this title change to happen if the title is on the Navigation Menu. Solution: Before I give you the CODE, let me explain a few ...


14

I ran your filter in my development area. It didn't work. Then I switched off the Yoast SEO plugin, which I knew was also messing with the page title. Then it worked. So my suggestion would be another plugin is messing with it. In the case of Yoast, it was a filter call to pre_get_document_title returning non empty. In that case wp_get_document_title is ...


12

I just found the answer and instead of putting this in the Title: <!--:en-->My English Title<!--:--><!--:fr-->My French Title<!--:--> We need to put this code: [:en]My English Title[:fr]My French Title and qTranslate does the rest :)


12

Here is how you do it, First remove woocommerce single title action, and create your own function to handle the title, later add back the action using your newly created function. <?php remove_action('woocommerce_single_product_summary','woocommerce_template_single_title',5); add_action('woocommerce_single_product_summary', 'woocommerce_my_single_title'...


10

As of Wordpress 4.4, you can use the Wordpress filter document_title_parts to change the title. Add the following to functions.php: add_filter('document_title_parts', 'my_custom_title'); function my_custom_title( $title ) { // $title is an array of title parts, including one called `title` $title['title'] = 'My new title'; if (is_singular('post')) {...


10

the_title is a Loop tag. It "Displays or returns the title of the current post" and it is supposed to be used inside the Loop, not outside of it. What you are doing-- calling it outside the Loop-- is not quite correct, and you are getting inconsistent results. What happens is this: The $post variable gets set to the first post in the Loop very early in ...


10

The two title tags can be explained as that you are using a theme that is written for Wordpress4.1 and actually is using 4.1. As from 4.1 you don't need to call wp_title() in the head any more, you can make use of new title_tag theme support tag which automatically adds the wp_title() tag in the header The parent theme you are using are most probably ...


9

You could change the post title when it is viewed: add_action( 'the_post', 'wpse_94856_title_update' ); function wpse_94856_title_update( $post ) { if ( empty ( $post->post_title ) ) return; $new_title = mb_convert_case( $post->post_title, MB_CASE_TITLE, "UTF-8" ); if ( $post->post_title === $new_title ) return; ...


9

wp_get_document_title() has some interesting filters - pre_get_document_title and document_title_parts. /** * Filter the parts of the document title. * * @since 4.4.0 * * @param array $title { * The document title parts. * * @type string $title Title of the viewed page. * @type string $page Optional. Page number if paginated. * ...


9

You can see everything added to wp_head in the file /wp-includes/default-filters.php. If your theme supports the title tag, you can remove it entirely with remove_action: remove_action( 'wp_head', '_wp_render_title_tag', 1 ); Though it may be simpler/better to use remove_theme_support( 'title-tag' ) in a child theme, which is what _wp_render_title_tag ...


9

A post or page has only one title, the title tag <title> is the document title. The filter wp_title filters the output of wp_title() function, which was used before to output the title of the document. In WordPress 4.1, the title-tag support in themes was introduced and wp_get_document_title() is used instead of wp_title(). So, if your theme supports ...


8

Also, run the slug from sanitize_title_with_dashes() through wp_unique_post_slug() to ensure that it's unique. It will automatically append '-2', '-3' etc. if it's needed.


8

/** * Wraps the first half of the provided string inside a span with the class lol_class. * * @param string $title The string. * @return string The modified string. */ function wpsa_82312_widget_title($title) { // Cut the title into two halves. $halves = explode(' ', $title, 2); // Throw first word inside a span. $title = '&...


8

It looks like your site's title is empty. Fill it out or try for example: add_filter( 'wp_title', function( $title ) { return trim( $title ); } ); to remove the blank space in front. For your setup, the following part of wp_title() is responsible for the blank space: $title = $prefix . implode( " $sep ", $title_array ); So when you're on a single ...


7

Sorry to dig this question up from grave, but there's a better solution provided since WordPress 3.1. The enter_title_here filter. function change_default_title( $title ){ $screen = get_current_screen(); // For CPT 1 if ( 'custom_post_type_1' == $screen->post_type ) { $title = 'CPT1 New Title'; // For CPT 2 } elseif ( '...


7

You can use markup in titles. H<sub>2</sub>O will work just fine. I would use H₂O with a real ₂ because markup will be stripped in title attributes and in feeds. Note that WordPress will not create a pretty permalink for the correct character. The slug for my example will look like this: h2o-h%e2%82%82o. You need my plugin Germanix URL to get ...


7

You can do this : add_action('admin_init', 'wpse_110427_hide_title'); function wpse_110427_hide_title() { if (current_user_can('subscriber')) remove_post_type_support('post', 'title'); } This would hide title for subscriber. Replace 'post' with your custom post type


7

There are actually two problems: The & is encoded as &amp;, so you have to use html_entity_decode() first. Multi-byte characters need more than one byte, and strlen() will fail with them. Don’t use strlen(). So use something like this: $title = html_entity_decode( get_the_title(), ENT_XML1, 'UTF-8' ); $length = mb_strlen( $title, 'utf-8' );


7

I use a code which differs from yours a little bit. There is no need to get the current screen using the enter_title_here-filter because you have already a post-object: /** * Filter: Modifies the standard placeholder text * @param string $title * @param WP_Post $post * @return string */ function my_enter_title_here( $title, $post ) { if ( '...


7

After some experimenting I came to the following suggestion: could it be, that the <title> tag is "hard coded" inside your parent theme's header.php? If that is the case, you could try to remove the <title> tag from your child theme's header.php (copy your parent's header.php into your child theme folder) and then add the theme support back ...


7

Use in_the_loop() conditional tag to make sure you are modifying the title inside the Loop and to prevent the hook changing titles somewhere else: function customize_wishlist_title( $title ){ $current_user = wp_get_current_user(); $loggeduser = $current_user->user_login; // give attention to the following condition: if ( is_page( 258 ) &...


7

When WordPress inserts a post, it runs the title through a filter called sanitize_title to get the slug. By default there is a function called santize_title_with_dashes attached to this filter with priority 10. This function simply strips out the |. If it is surrounded by spaces those spaces will be converted to hyphens. So your task is to run a filter on ...


6

For those wishing to change the document's title attribute, I found that using the wp_title filter no longer works. Instead, use the pre_get_document_title filter: add_filter("pre_get_document_title", "my_callback"); function my_callback($old_title){ return "My Modified Title"; } Source


6

No need for functions and filters all you need to do is to use get_adjacent_post instead of next_post_link and prev_post_link, Note that get_adjacent_post is used to get previous and next post, you can read about it here To get previous post and it's title attribute use this $prev_post = get_adjacent_post(false, '', true); if(!empty($prev_post)) { echo '<...


6

Instead of replacing spaces you should use the build in function sanitize_title() which will take care of the replacing for you. Like this: sanitize_title( $post_title, $post->ID ); Also, you should use a unique slug. Which you can get with the function wp_unique_post_slug() So putting it all together a solution might be: function ...


6

Start with downloading the plugin called Force Post Title. Here's the plugin with one row (2 with the comment line) added to the bottom, based on our comments. What happens is that a small jQuery script is added to the page Create Post/Page. The script will check if the title field is empty when the user clicks the submit button. Since this is such a ...


6

simply use this where ever you want to display your title with limited words <?php echo wp_trim_words( get_the_title(), 5 ); ?> replace number 5 in above code with whatever number of words you need to display. Regards.


Only top voted, non community-wiki answers of a minimum length are eligible