- This topic is empty.
-
AuthorPosts
-
June 3, 2016 at 12:49 am #242421
gulliver
ParticipantI’ve previously used conditionals to modify the title tag (in the header), but since enabling title-tag support have no idea how to do some of the stuff I’d done when using wp_title().
Specifically, I’m trying to add page numbers to multi-page posts and prepend some pages with the parent/grandparent name.
June 6, 2016 at 10:55 pm #242592Ilan Firsov
ParticipantNot really sure but from a little search (https://developer.wordpress.org/reference/hooks/wp_title/)[wp_title filter] should still work, maybe try that.
<?php add_filter( 'wp_title', 'my_new_title', 10, 3 ); function my_new_title( $title, $sep, $seplocation ) { return "123 {$sep} {$title}"; }
June 6, 2016 at 11:15 pm #242593gulliver
ParticipantThanks.
Toward the bottom of that page is a note that wp_title is being deprecated and to approach things differently.As a non-coder, I’d just about managed to grasp stuff like
if (is_404()) echo ‘file not available’;… but how to use the revised format of
if (is_404()) {$title[‘title’] = ‘file not available’;}is still largely a mystery to me and don’t know how to appropriately revise previously-used code.
June 6, 2016 at 11:41 pm #242594Ilan Firsov
ParticipantThis looks to be working:
<?php add_filter( 'pre_get_document_title', 'my_404_title' ); function my_404_title( $title ) { if( is_404() ) { return 'new 404 page title'; } return $title; }
June 7, 2016 at 12:17 am #242596gulliver
ParticipantThanks. Appreciated. :-)
Yes, that does work.
I’ve read about pre_get_document_title but not yet been able to understand it enough to be able to use it.For example, my old header (which used wp_title) had a conditional to add page numbers to multi-page posts/pages.
// Add a page number if necessary: if ($paged >= 2 || $page >= 2 ) echo ' ' . sprintf( __('(page %s)'), max($paged, $page) );
I still don’t know how to modify things to include this with title-tag. And I’ve never been able to prepend some pages with the parent/grandparent name.
June 7, 2016 at 12:57 am #242598Ilan Firsov
ParticipantCan you post the whole old function? I can’t really tell from where you are getting the $paged and $page variables and I don’t think they are available as global variables.
June 7, 2016 at 1:18 am #242599gulliver
ParticipantThanks, Ilan.
Rather than a function, I’d been using conditionals within the title element of the header.php – an edited example of which is below.
<title><?php // Set <title> tag based on what is being viewed. global $page, $paged; // Add the site name and separator. bloginfo('name'); echo ' | '; // Add the site description for the front page. $site_description = get_bloginfo('description', 'display'); if ($site_description && (is_front_page())) echo "$site_description"; // Show page title on posts page. if (is_home()) wp_title(''); // Title for 404. if (is_404()) echo 'file not available'; // Add a page number if necessary: if ($paged >= 2 || $page >= 2 ) echo ' ' . sprintf( __('(page %s)'), max($paged, $page) ); // Show page title. if (is_page()) wp_title(''); //else bloginfo('name'); ?></title>
June 7, 2016 at 1:52 am #242600Ilan Firsov
ParticipantI think you may be able to use the same logic in the filter you just need to return the new title at the end instead of echoing it
Try this<?php add_filter( 'pre_get_document_title', 'my_custom_page_title' ); function my_custom_page_title( $title ) { global $page, $paged; // Add the site name and separator. $new_title = get_bloginfo('name') . ' | '; // Add the site description for the front page. $site_description = get_bloginfo('description', 'display'); if ($site_description && (is_front_page())) $new_title .= $site_description; // Show page title on posts page. if (is_home()) $new_title .= $title; // Title for 404. if (is_404()) $new_title .= 'file not available'; // Add a page number if necessary: if ($paged >= 2 || $page >= 2 ) $new_title .= ' ' . sprintf( __('(page %s)'), max($paged, $page) ); // Show page title. if (is_page()) $new_title .= $title; return $new_title; }
June 7, 2016 at 3:28 am #242601gulliver
ParticipantThanks.
I can’t get it to work.General-template.php in core includes the line below, which seems to add the numbering to posts – but not pages.
if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) { $title['page'] = sprintf( __( 'page %s' ), max( $paged, $page ) ); }
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.