INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Customised search result title depending upon tag/cat/special field
2

Customised search result title depending upon tag/cat/special field

Customised search result title depending upon tag/cat/special field

(OP)
Almost there with the general design of the new site but this issue has been bugging me the last couple of days:

I'd like to customise my archive.php page so that the title in the search result relates to what was searched. For example if a user clicks the tag 'sailing' the search result will list the results with the title 'Here is a list of all posts with the tag 'sailing'. I'd like to do the same with archives, categories and even special fields.

Someone suggested

CODE

<?php echo wp_specialchars($s); ?>
but gave no clues as to how this is used. I'm at a loss!

Any clues? Thank you.

RE: Customised search result title depending upon tag/cat/special field


I use this in my header.php to give different titles depending on the page in question. This is a very much cut-down version without any page/post/author stuff, but you should get the general idea:

CODE

<? if (is_tag()) { ?>
    <title>Posts tagged with &ldquo; <? wp_title(', true); ?> &rdquo; - <? bloginfo('name'); ?></title>
<? } else if (is_day()) { ?>
    <title>Daily archive for <? echo get_archive_page_date(); ?> - <? bloginfo('name'); ?></title>
<? } else if (is_month()) { ?>
    <title>Monthly archive for <? echo get_archive_page_date(); ?> - <? bloginfo('name'); ?></title>
<? } else if (is_year()) { ?>
    <title>Yearly archive for <? echo get_archive_page_date(); ?> - <? bloginfo('name'); ?></title>
<? } else if (is_category()) { ?>
    <title>Posts in the <? wp_title(', true); ?> category - <? bloginfo('name'); ?></title>
<? } ?>

Hope this helps,
Dan

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Customised search result title depending upon tag/cat/special field

(OP)
Hi Dan,

I use similar code too. The problem is the specialchar thing. I'm not sure how to implement it within the conditional statements and, more importantly, how to configure it. I've tried just adding it as a single line and nothing happens and I've tried changing the $s to get_tag but still nothing.

RE: Customised search result title depending upon tag/cat/special field


I'm confused. Why do you need to use 'specialchars' at all? I thought you simply wanted the page title to reflect the tag in question?

Dan

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Customised search result title depending upon tag/cat/special field

(OP)
Hi Dan,

I'm far more confused than you, believe me! OK so I now understand I don't need that specialchar, but I'm still struggling with this. First of all I just tried copying your code, which returned errors. So I stripped it all back to this:

CODE

    <?php     if (is_tag('')) { ?>
     <title>Posts tagged with &ldquo; <? wp_title('', true); ?> &rdquo; - <? bloginfo('name'); ?></title>
  
<? } ?>

And whilst I didn't get any errors and it returned my tag word search result, the title did not display. Can you see anything wrong here?
 

RE: Customised search result title depending upon tag/cat/special field

no reason why your code should not work (make sure you are echoing within <title> tags. something like this might also work

notes:
i don't know what the $s variable is.  but whatever it might be, you need to get it into the scope of the function below.
if you need to conditionalise whether or not the suffix is applied, you should wrap the return in an if().  remember to add also an 'else' in which you return just $title.

CODE

/**
 * @package Title Adjust
 * @author Justin Adie
 * @version 0.1.0
 */
/*
Plugin Name: Title Adjust
Plugin URI: #
Description: allows the user to append text to a title
Author: Justin Adie
Version: 0.1.0
Author URI: http://rathercurious.net
*/

function addtotitle($title){
 global $s; //bring the $s variable into the global scope
 /*
 if (somecondition is true) { */
  return $title . " - $s";
 /*
 } else {
  return $title;
 } */
}
add_filter('wp_title', 'addtotitle');

RE: Customised search result title depending upon tag/cat/special field


Quote:

Can you see anything wrong here?

No... but perhaps you havwe another title being output? Do a 'view source' and see how many <title> elements you have being delivered to the page.

Dan

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Customised search result title depending upon tag/cat/special field

(OP)
OK chaps, I got it in the end. Appreciate your patience and help. Thanks.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close