1

I've been trying to change the title in a dynamic page without any luck.

What I did is I created two page-templates from where I load items (occasions) from an external database. In one page-template I load the single-page occasion:

<?php include 'Occasions-files/single.php';?>

And the second page-template I load an overview of the occasions:

<?php include 'Occasions-files/file.php';?>

Now the title is default from the SEO plugin. I've tried All-in-One SEO as well as Yoast SEO but can't seem to overwrite the title. What I want is to change the title from single.php into the occasion name but leave all the other pages to the SEO plugin.

I'm not quite experienced with this and I'm not sure where to start. For testing purpose I've tried to put test manually before the get_header, then it shows the manually added title, but my header is messed up. When I put the title after the get_header, it loads the header ok but the title isn't shown and is still default

<title>test</title>
<?php get_header(); ?>

Also, I've tried to add a filter (to the functions.php) but this was also without any results or maybe I have no clue on how to insert it..?

function custom_title_function($custom_title) {
$custom_title['title'] = "Test Title";
return $custom_title; }
add_filter( 'document_title_parts', 'custom_title_function' );

Anyone has an idea on how to resolve this? Many thanks in advanced!

1

Just to make sure, does the title tag in your <head> area (presumably in header.php) look like this?

<title><?php wp_title(); ?></title>

It has to be like that for Yoast to work with it, and be able to override titles.

Anyway if you add this to your single.php (or at least before get_header() is run for that page) you should be able to modify the page title:

function custom_page_title( $title ){
    $return = $title;

    // You can check what the current post is via $wp_query->post
    global $wp_query;
    if( isset($wp_query->post) ){
        $id = $wp_query->post->ID;
        $post_title = $wp_query->post->post_title;
        // etc

        $return = $post_title;
    }

    // Or just outright change the title
    $return = 'test';

    return $return;
}
add_filter('wp_title', 'custom_page_title', 20);

Another thing to note here is that if you're using Yoast, you have to make sure that this function runs after Yoast modifies the title, otherwise Yoast will overwrite whatever changes you make. This is where the 20 comes in, as the third parameter in add_filter(). I think Yoast runs its filter at 15, so running yours at 20 (or any number higher than 15) will make sure your filter runs after Yoast's filter.

| improve this answer | |
  • Great, that works indeed! But I've got one more question... I have to get a row from the database (the title of the occasion), but whenever I call it, like I have in single.php it won't work (white screen). This is from Single.php: <?php include 'connect.php'; ?> <?php $id = $_GET['id']; $id = mysqli_real_escape_string($conn,$id); $id = $_GET['id']; $sql = "SELECT * FROM occasions WHERE Internnummer='" . $id . "'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { ?> <?php echo $row['OmschrijvingNL'];?> – Ptrckk Aug 31 '17 at 15:59
  • Hmmm, it's hard to say because a white screen probably means there is some PHP error happening. Are you able to view the error logs by chance? It could be how the external database connection is set up in connect.php. WordPress can be weird about connections to other databases. Do you have it set up with a wpdb object? Check this out, might be worth a shot to connect to the other database by using wpdb sitepoint.com/community/t/… – James Aug 31 '17 at 18:12
0

if you are using All in one SEO you can use overwrite your dynamic wp titles with the help of the following filter hook

add_filter( 'aioseop_title', 'change_wordpress_seo_title' );

function change_wordpress_seo_title( $title ){
    $title = 'modified title';
    return $title;
}
| improve this answer | |
-1
if(isset($_GET['v'])) {
    if($_GET['v'] == "about") {

   $title = "About Us";
}
else if ($_GET['v'] == "contact") {
   $title = "Contact Us";
}
}
else {
   $title = "Home";
}

<title><?php echo $title;?></title>
| improve this answer | |
  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Jan 18 '19 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.