Add Text to Wordpress Post Title, Exclude From Menu - php

I am using the Avada theme for WordPress and wanted to add "..." after each WordPress title, but the code I found adds "..." after the menu text. How can I exclude the menu from being added as well?
/* Adds ... After Blog Titles */
function add_suffix_to_title($title, $id = null){
if (! is_admin()) {
return ''.$title.'...';
}
return $title;
}
add_action( 'the_title', 'add_suffix_to_title', 10, 1 );
Thank you in advance!

You need to add some more condition to exclude menu, like bellow:
function add_suffix_to_title($title, $id = null){
if (! is_admin()) {
if(is_singular(array('post','page')) || is_archive() || is_home()){
if(in_the_loop()){
return $title . '...';
}
}
}
return $title;
}
add_action( 'the_title', 'add_suffix_to_title', 10, 1 );

Related

How to Use Title() Filter Hook to Change Page Titles, but Not Menu Titles

I want to use a filter hook to add an image before titles of my pages, but it also adds them to my menu titles. How can I get around this?
function scissorTitle ($title)
{
$scissortitle .= "<img src='*image_link*'>" . $title;
return $scissortitle;
}
add_filter( 'title', 'scissorTitle', 10);
The filter title has two parameters $title and $id you can use $id to change the only specific title of the page Check docs. Your full code should look like this:
function scissorTitle ($title, $id) {
global $post;
if (is_singular() && $post->ID == $id) {
$title = '<img src="image_link">' . $title;
}
return $title;
}
add_filter( 'the_title', 'scissorTitle', 10, 2);

Add custom canonical for front page - Yoast

I want to add a custom url for the front page using YOAST. I'm trying this approach but it doesn't do anything, can you help me?
function design_canonical($url) {
global $post;
$url == ( 'www.cator.com/cator/');
}
add_filter( 'wpseo_canonical', 'design_canonical' );
any idea where is the problem?
thanks
You first need to check if your page is home or not which you can check through is_home()
function design_canonical($url) {
if (is_home()) {
return 'your-custom-url.com'; //Enter here your custom url
} else {
return $url;
}
}
add_filter( 'wpseo_canonical', 'design_canonical' );

how to exclude menu items in the_title filter wordpress

I am having hard time excluding menu items from the_filter function I have to modify the post titles.
I want to modify the single post title as well as the posts in my sidebar, using in_the_loop() is working fine but it is also excluding my sidebar/archive post titles, breadcrumbs etc.
is there a way to detect menu items and exclude them? other then using in_the_loop()
here is my code:
function update_the_title( $title, $post_id ) {
if (not menu items) {
return $title + 'some string';
} else {
return $title;
}
}
add_filter( 'the_title', 'update_the_title');
Thanks
you have to try like this
Not tested
function update_the_title( $title, $post_id ) {
$menuLocations = get_nav_menu_locations();
$menuID = $menuLocations['primary']; // Get the *primary* menu ID
$primaryNav = wp_get_nav_menu_items($menuID);
if (array_search($post_id, array_column($primaryNav, 'ID'))) {
return $title + 'some string';
} else {
return $title;
}
}
add_filter( 'the_title', 'update_the_title', 10, 2);

How to redirect the post/page id by giving custom redirect url in wordpress

In my wordpress theme i have included meta box for Title, description and redirect section. I have a input field for the redirect box under every post and page id. If i add any url to a particular page/post id for that redirect box, the particular page/post id should redirect to my own url given in that redirect box. Is there any function to do this? I have searched in stackies and i get the following code. But its not working.
function my_permalink_redirect($permalink) {
global $post;
if ($post->ID == your_post_id_here) {
$permalink = 'http://new-url.com/pagename';
}
return $permalink;
}
add_filter('get_the_permalink','my_permalink_redirect');
Try:
add_filter( 'the_permalink', 'filter_function_name_7062', 10, 2 );
function filter_function_name_7062( $permalink, $post ){
global $post;
if ($post->ID == 684) {
$permalink = 'http://sample.com/1';
}
if ($post->ID == 444) {
$permalink = 'http://sample.com/2';
}
return $permalink;
}
Try:
function my_permalink_redirect($permalink) {
global $post;
if ($post->ID == your_post_id_here) {
$permalink = 'http://new-url.com/pagename';
wp_redirect("'.$permalink.'", 301);
exit;
}
}
add_filter('get_the_permalink','my_permalink_redirect');

Why does the_title() filter is also applied in menu title?

I have created below function to hide page title. But when I execute this code, it also hides the menu name.
function wsits_post_page_title( $title ) {
if( is_admin())
return $title;
$selected_type = get_option('wsits_page_show_hide');
if(!is_array($selected_type)) return $title;
if ( ( in_array(get_post_type(), $selected_type ) ) && get_option('wsits_page_show_hide') )
{
$title = '';
}
return $title;
}
add_filter( 'the_title', array($this, 'wsits_post_page_title') );
Nikola is correct:
Because menu items also have titles and they need to be filtered :).
To make this only call in the posts, and not in menus, you can add a check for in_the_loop() - if it is true, you're in a post.
So change the first line in the function to:
if( is_admin() || !in_the_loop() )
and all should be well.
It's a bit of a hack but you can solve this by adding your action to loop_start.
function make_custom_title( $title, $id ) {
// Your Code Here
}
function set_custom_title() {
add_filter( 'the_title', 'make_custom_title', 10, 2 );
}
add_action( 'loop_start', 'set_custom_title' );
By embedding the_title filter inside of a loop_start action, we avoid overwriting the menu title attributes.
You can do something like that :
In your function.php :
add_filter( 'the_title', 'ze_title');
function ze_title($a) {
global $dontTouch;
if(!$dontTouch && !is_admin())
$a = someChange($a);
return $a;
}
In your template :
$dontTouch = 1;
wp_nav_menu( array('menu' => 'MyMenu') );
$dontTouch = 0;
Posting this answer because it was the search result I ended up clicking on while searching about targeting the filter hook the_title while ignoring the filter effect for navigation items.
I was working on a section in a theme which I wanted to add buttons to the page title within the heading one tag.
It looked similar to this:
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title ) . '</h1>'.PHP_EOL; ?>
I was then "hooking in" like this:
add_filter( 'the_title', 'my_callback_function' );
However, the above targets literally everything which calls the_title filter hook, and this includes navigation items.
I changed the filter hook definition like this:
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title, $post->ID, true ) . '</h1>'.PHP_EOL; ?>
Pretty much every call to the_title filter passes parameter 1 as the $post->post_title and parameter 2 as the $post->ID. Search the WordPress core code for apply_filters( 'the_title'* and you'll see for yourself.
So I decided to add a third parameter for situations where I want to target specific items which call the_title filter. This way, I can still receive the benefit of all callbacks which apply to the_title filter hook by default, while also having the ability to semi-uniquely target items that use the_title filter hook with the third parameter.
It's a simple boolean parameter:
/**
* #param String $title
* #param Int $object_id
* #param bool $theme
*
* #return mixed
*/
function filter_the_title( String $title = null, Int $object_id = null, Bool $theme = false ) {
if( ! $object_id ){
return $title;
}
if( ! $theme ){
return $title;
}
// your code here...
return $title;
}
add_filter( 'the_title', 'filter_the_title', 10, 3 );
Label the variables however you want. This is what worked for me, and it does exactly what I need it to do. This answer may not be 100% relevant to the question asked, but this is where I arrived while searching to solve this problem. Hope this helps someone in a similar situation.
The global $dontTouch; solution didn't work for me for some reason. So I simply removed the filter around the menu thus in header.php:
remove_filter( 'the_title', 'change_title' );
get_template_part( 'template-parts/navigation/navigation', 'top' );
add_filter( 'the_title', 'change_title' );
And all is well.
I think you're looking for this:
function change_title($title) {
if( in_the_loop() && !is_archive() ) { // This will skip the menu items and the archive titles
return $new_title;
}
return $title;
}
add_filter('the_title', array($this, 'change_title'), 10, 2);

Categories