How to convert page slug string into Title string for breadcrumbs?
-
Hello,
as my page titles are a bit longer than what I want them to be for my Yoast breadcrumbs, I decided to use the page slugs instead and included$link[‘slug’] = get_post_field( ‘post_name’, get_post() );
in the class-breadcrumbs.php file. It almost does the job and returns the slug as it is, but I also need hyphens replaced by whit space and each word capitalized. I am sure the solution is not that complicated, but I am not into php and can’t find any information on internet either. Please if anyone can give me a hand on this? Thank you
-
There are some simple php functions to do this.
// Change hypens to whitespace $title = str_replace( '-', ' ', $title ); // Capitalise every word in your title $title = ucwords( $title );
You can find documentation for these functions on php.net
http://php.net/manual/en/function.ucwords.php
http://php.net/manual/en/function.str-replace.phpGood luck!
-
This reply was modified 5 years, 6 months ago by
Dark Roast.
THANK YOU FOR YOUR QUICK REPLY, BUT PHP IS NOT MY STRENGHT AND I HAVE NO IDEA HOW TO DO IT.
THAT LINE THAT I ADDED
” $link[‘slug’] = get_post_field( ‘post_name’, get_post() ); ”
WAS A PURE MIRACLE THAT I FOUND IT AND IT WORKED.
I HAVE INCLUDED THE SNIPPET WHERE THE MAGIC HAPPENS. IF YOU CAN PLEASE TELL ME WHERE AND WHAT TO INCLUDE, I WOULD BE VERY GREATFUL. THANK YOU/** * Retrieve link url and text based on post id * * @param int $id Post ID. * * @return array Array of link text and url */ private function get_link_info_for_id( $id ) { $link = array(); $link['slug'] = get_post_field( 'post_name', get_post() ); $link['url'] = get_permalink( $id ); $link['text'] = WPSEO_Meta::get_value( 'bctitle', $id ); if ( $link['text'] === '' ) { $link['text'] = strip_tags( get_the_title( $id ) ); } /** * Filter: 'wp_seo_get_bc_title' - Allow developer to filter the Yoast SEO Breadcrumb title. * * @api string $link_text The Breadcrumb title text * * @param int $link_id The post ID */ $link['text'] = apply_filters( 'wp_seo_get_bc_title', $link['text'], $id ); return $link; } /** * Retrieve link url and text based on term object * * @param object $term Term object. * * @return array Array of link text and url */ private function get_link_info_for_term( $term ) { $link = array(); $bctitle = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'bctitle' ); if ( ! is_string( $bctitle ) || $bctitle === '' ) { $bctitle = $term->name; } $link['slug'] = get_post_field( 'post_name', get_post() ); $link['url'] = get_term_link( $term ); $link['text'] = $bctitle; return $link; } /** * Retrieve link url and text based on post type * * @param string $pt Post type. * * @return array Array of link text and url */ private function get_link_info_for_ptarchive( $pt ) { $link = array(); $archive_title = ''; if ( isset( $this->options[ 'bctitle-ptarchive-' . $pt ] ) && $this->options[ 'bctitle-ptarchive-' . $pt ] !== '' ) { $archive_title = $this->options[ 'bctitle-ptarchive-' . $pt ]; } else { $post_type_obj = get_post_type_object( $pt ); if ( is_object( $post_type_obj ) ) { if ( isset( $post_type_obj->label ) && $post_type_obj->label !== '' ) { $archive_title = $post_type_obj->label; } elseif ( isset( $post_type_obj->labels->menu_name ) && $post_type_obj->labels->menu_name !== '' ) { $archive_title = $post_type_obj->labels->menu_name; } else { $archive_title = $post_type_obj->name; } } } $link['slug'] = get_post_field( 'post_name', get_post() ); $link['url'] = get_post_type_archive_link( $pt ); $link['text'] = $archive_title; return $link; } /** * Create a breadcrumb element string * * @todo The <code>$paged</code> variable only works for archives, not for paged articles, so this does not work * for paged article at this moment. * * @param array $link Link info array containing the keys: * 'text' => (string) link text * 'url' => (string) link url * (optional) 'title' => (string) link title attribute text * (optional) 'allow_html' => (bool) whether to (not) escape html in the link text * This prevents html stripping from the text strings set in the * WPSEO -> Internal Links options page. * @param int $i Index for the current breadcrumb. * * @return string */ private function crumb_to_link( $link, $i ) { $link_output = ''; if ( isset( $link['text'] ) && ( is_string( $link['text'] ) && $link['text'] !== '' ) ) { $link['text'] = trim( $link['text'] ); if ( ! isset( $link['allow_html'] ) || $link['allow_html'] !== true ) { $link['text'] = esc_html( $link['text'] ); } $inner_elm = 'span'; if ( $this->options['breadcrumbs-boldlast'] === true && $i === ( $this->crumb_count - 1 ) ) { $inner_elm = 'strong'; } if ( ( isset( $link['url'] ) && ( is_string( $link['url'] ) && $link['url'] !== '' ) ) && ( $i < ( $this->crumb_count - 1 ) ) ) { if ( $i === 0 ) { $link_output .= '<' . $this->element . ' typeof="v:Breadcrumb">'; } else { $link_output .= '<' . $this->element . ' rel="v:child" typeof="v:Breadcrumb">'; } $title_attr = isset( $link['title'] ) ? ' title="' . esc_attr( $link['title'] ) . '"' : ''; $link_output .= '<a href="' . esc_url( $link['url'] ) . '" rel="v:url">' . $link['text'] . '</a>'; } else { $link_output .= '<' . $inner_elm . ' class="breadcrumb_last">' . $link['slug'] . '</' . $inner_elm . '>'; // This is the last element, now close all previous elements. while ( $i > 0 ) { $link_output .= '</' . $this->element . '>'; $i--; } } }
-
This reply was modified 5 years, 6 months ago by
Steve Stern (sterndata).
-
This reply was modified 5 years, 6 months ago by
Steve Stern (sterndata). Reason: put code in backticks
Hey toddius,
You should never alter a plugin file itself. If you’ll update the plugin you’ll lose your modifications.
You can modifie the plugin with hooks or filters.
This code should do your trick;
function my_breadcrumb_filter() { $link['slug'] = get_post_field( 'post_name', get_post() ); // Change hypens to whitespace $breadcrumb = str_replace( '-', ' ', $link['slug'] ); // Capitalise every word in your title $breadcrumb = ucwords( $breadcrumb ); return $breadcrumb; } add_filter('wp_seo_get_bc_title', 'my_breadcrumb_filter' );
You can put this snippet in your functions.php or in a custom plugin.
-
This reply was modified 5 years, 6 months ago by
- The topic ‘How to convert page slug string into Title string for breadcrumbs?’ is closed to new replies.