Support

Home Forums Event Espresso Premium Adding a custom field to the title issue

Adding a custom field to the title issue

Posted: November 17, 2014 at 2:32 pm

Viewing 7 reply threads


Cara Livorio

November 17, 2014 at 2:32 pm

Hello,
I used this code to add a custom field to the title but I realized that it also adds the field to all the main navigation tabs. HOw can I fix this?

this is the code

//this can go in the themes functions.php
// or into a site specific/custom functions plugin.

function modified_ee_title($title, $sep) {
global $post;
$postype = get_post_type( $post );
//if not an EE event, return
if($postype != “espresso_events”) { return $title; }

//get the custom/meta fields
$meta = get_post_custom($post_id);

//if the meta field called abc doesnt exist, return
//obviously change abc to the actual meta key…
if( !isset($meta[‘abc’]) ) { return $title; }

//modify the title
$title = $title . ‘ ‘ . $meta[‘abc’][0];

//return the modified title
return $title;

}
add_filter( ‘the_title’, ‘modified_ee_title’, 10, 2 );

thanks


Josh

  • Support Staff

November 17, 2014 at 6:05 pm

Hi Cara,

This looks like the code that Dean posted here:

http://pastebin.com/jRzLjsGj

I can suggest wrapping the conditional so that it also checks to see if it’s in the loop.

So where it has this:

if( !isset($meta['abc']) ) { return $title; }

you can change to this:

if( !isset($meta['abc']) || ! in_the_loop() ) { return $title; }

Aside from that, it looks like there’s a typo where it says:

$meta = get_post_custom($post_id);

that should be changed to:

$meta = get_post_custom($post->id);


Cara Livorio

November 18, 2014 at 5:14 am

Hi JOsh, unfortunately your correction do not make the field show at all…

is there another way to target just the title in the events?


Dean

November 18, 2014 at 6:12 am

Hi,

Changing it here should do it:

//if not an EE event, return
if($postype != "espresso_events" || !in_the_loop() ) { return $title; }

Screenshot http://take.ms/0usMK

I’ve updated the code here http://pastebin.com/jRzLjsGj


Cara Livorio

November 18, 2014 at 1:55 pm

HI Dean,

this modified code with your suggestion is not returning anything

function modified_ee_title($title, $sep) {
global $post;
$postype = get_post_type( $post );
//if not an EE event, return
if($postype != “espresso_events” || !in_the_loop() ) { return $title; }

//get the custom/meta fields
$meta = get_post_custom($post_id);

//if the meta field called abc doesnt exist, return
//obviously change abc to the actual meta key…
if( !isset($meta[‘abc’]) ) { return $title; }

//modify the title
$title = $title . ‘ ‘ . $meta[‘abc’][0];

//return the modified title
return $title;

}
add_filter( ‘the_title’, ‘modified_ee_title’, 10, 2 );


Josh

  • Support Staff

November 18, 2014 at 3:03 pm

Hi Cara,

It looks like the typo still needs fixing. Where it says:

$meta = get_post_custom($post_id);

that should be changed to:

$meta = get_post_custom($post->id);

Also, it’s not clear from the way you are posting code to the forums, but you will need to make sure that your code has proper single quotes (not curly quotes).

Here’s the code I’m using on my test site, and I can verify it works:


Cara Livorio

November 18, 2014 at 3:44 pm

Yes, it is working!!

thank you so much!!!

you guys are the best


Dean

November 19, 2014 at 3:13 am

Glad you got it working!

For future reference/readers, I’ve updated the code to fix that typo (it was working on my system for some bizarre reason!)

Viewing 7 reply threads

The support post ‘Adding a custom field to the title issue’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso