Hi Sybre,
Thank you so much, that was really useful. I’m not a developer, but it’s fun to poke around with code. I’ve got a version working to change the title & description of all GeoDirectory custom post types and switching between English and French using WPML. Here’s my code:
/******************************************************************
* Edit the title for both the header and social networks
******************************************************************/
add_filter( 'the_seo_framework_pro_add_title', 'arbolife_custom_title', 10, 3 );
function arbolife_custom_title( $title = '', $args = array(), $escape = true ) {
global $post;
$blogname = the_seo_framework()->get_blogname();
$separator = the_seo_framework()->get_title_separator();
// Get the current post type
$post_type = get_post_type( $post->ID );
// Get all the GeoDirectory post types
$all_gd_post_type = geodir_get_posttypes();
// If the post type is a GeoDirectory post type & we're not in a category page
if ( in_array($post_type, $all_gd_post_type, true) ) {
$post_type_obj = get_post_type_object( $post_type );
if ( isset( $post_type_obj->labels ) ) {
$post_type_name = $post_type_obj->labels->singular_name;
} else {
$post_type_name = '';
}
$directory_name = __( '%s Directory', 'arbolife-seo' );
$directory_full = sprintf($directory_name, $blogname);
// If there is a city AND a post type name
// AND the page is not an archive (displaying a cateogry)
// AND the page is not a search result
if ( $post->post_city && $post_type_name && !is_archive() && !is_search() ){
$type_in_city = __( ' - %s in %s', 'arbolife-seo' );
$post_type_location = sprintf($type_in_city, $post_type_name, $post->post_city);
// Same as above but no city (most likely online store)
} else if ( $post_type_name && !is_archive() && !is_search() ){
$post_type_location = sprintf( ' - %s', $post_type_name);
} else {
$post_type_location = '';
}
if ( $post_type_name ) {
$replacement = sprintf( '% s%s %s %s', $post->post_title, $post_type_location, $separator, $directory_full );
$title = str_replace( "$blogname $separator", $title, $replacement );
}
}
return $title;
}
/******************************************************************
* Edit the description for both the meta tag and og for social networks
******************************************************************/
add_filter( 'the_seo_framework_description_output', 'arbolife_custom_description', 10, 3 );
add_filter( 'the_seo_framework_ogdescription_output', 'arbolife_custom_description', 10, 3 );
function arbolife_custom_description( $description = '', $args = array() ) {
global $post;
// Get the current post type
$post_type = get_post_type( $post->ID );
// Get all the GeoDirectory post types
$all_gd_post_type = geodir_get_posttypes();
// If the post type is a GeoDirectory post type & we're not in a category page & not in search page
if ( in_array($post_type, $all_gd_post_type, true) && !is_archive() && !is_search() ) {
// If there is a city (not an online store)
if ( $post->post_city ){
$country_name = __( $post->post_country, 'arbolife-seo' );
$post_location = sprintf( '%s, %s, %s - ', $post->post_address, $post->post_city, $country_name );
// Same as above but no city (most likely online store)
} else {
$post_location = '';
}
// Get the list of categories
$post_tax = $post_type . "category";
$post_categories = $post->{$post_tax};
if (is_array($post_categories)) {
$post_categories = implode(', ', $post_categories);
}
$cats_arr = array_filter(explode(",", $post_categories));
$category_list = '';
foreach ($cats_arr as $cat) {
$term_arr = get_term($cat, $post_tax);
if ( !empty($category_list) ) {
$category_list .= ', ';
}
$category_list .= strtolower($term_arr->name);
}
// Collate location and categories and create exceprt
$initial_replacement = $post_location . ucfirst($category_list);
$excerpt_length = 170 - strlen($initial_replacement);
$post_excerpt = text_truncate($post->post_content, $excerpt_length);
// Put the final description together
$description = sprintf( '%s | %s', $initial_replacement, $post_excerpt );
}
return $description;
}
/******************************************************************
* Function to truncate a post description
******************************************************************/
function text_truncate($text,$numb) {
if (strlen($text) > $numb) {
$text = substr($text, 0, $numb);
$text = substr($text,0,strrpos($text," "));
$etc = "...";
$text = $text.$etc;
}
return $text;
}
The only problem I found is that in the wp-admin area, when viewing a French listing, the title shows with the English wording (“Directory” instead of “Annuaire”, “Store” instead of “Magasin” and “in” instead of “à”. Here’s a screenshot. It’s strange that it works on the frontend, but not the backend, can this be fixed somehow?
Next I’ll move to fixing the images.
Best,
Marc
-
This reply was modified 8 years, 1 month ago by
Marc.