Skip Navigation

[Resolved] Showing a custom field in replacement of the default page title

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 34 replies, has 2 voices.

Last updated by Nigel 6 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1077682

Hi

Hoping someone can help me with this, what I would like to do is keep my WordPress page name/title as it is but then create a custom field called for example "Printable Title"

Then have my page print this on the actual page template for the heading instead of the standard page title which will be used elsewhere.

Is this achievable with toolset?

I am thinking (without editing the theme files as I am using a theme that updates) I need to write some kind of function for functions.php to achieve this?

Any help would be greatly appreciated.

#1078303

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Matt

Toolset templates only take over the content area of the post, the title is output by your theme.

So, one solution would be to edit the theme PHP files and where the title is output replace that with your custom field.

But if it is a "standard" theme it will output the title using the WP function the_title.

And there is a filter for that which means you can "intercept" it before it is rendered and replace it with something else.

It works everywhere the_title is output, and you presumably only want this to happen on certain pages (e.g. single custom posts of a certain type).

So your code would use the_title filter.

It would check for which page you are on, e.g. using is_singular( 'some-post-type' ).

It would get the custom field value using get_post_meta (note that Types custom fields are stored with a 'wpcf-' prefix, e.g. 'wpcf-my-field').

It would return that value in place of the current title.

See the following documentation:
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title
https://developer.wordpress.org/reference/functions/is_singular/
https://developer.wordpress.org/reference/functions/get_post_meta/

Hopefully there is enough info there for you to be able to implement this.

#1081442

Right OK I don't really want to modify the theme files.

What I am thinking now is look at this the opposite way round.

So modifying the title in wordpress this is then output on the page itself = fine.

Then having a custom field for the alternate page title, this one is actually only printed within toolset content templates, so this is where toolset might be able to help.

To explain further we have a custom taxonomy let's call them A, B, C for this example. I now set the title for A to 1 instead so it prints 1 on the heading of the page. However, on the content template where the custom taxonomy for a post displays instead of it printing 1 if it belongs to the custom taxonomy it prints the custom field as A instead. I still want it to link to the page though (1).

Does that make sense?

#1082852

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Matt

Not really 🙂

The example with letters and numbers is a bit abstract and the way you describe it I don't know if A is a term of the custom taxonomy or a post that has the term 1 assigned to it or... whatever.

Perhaps you could use real examples.

Please make it clear where you are talking about displaying what, e.g. displaying single posts of a particular type and outputting the name of a taxonomy term on that post, or displaying a taxonomy archive (i.e. at site.com/colour/red/ display all posts that have the term red of the colour taxonomy assigned) etc.

#1083217

Haha yeah I tried to make it simpler but I think I made it more complicated lol.

Ok so the taxonomy is called "country".

Right now some examples are Canada, USA, UK etc of taxonomy names.

What I wanted to do is change these so I might change USA to America, but I still want it to be called USA in content templates. Just the heading of the page says America.

So I have made a new custom field called "Pretty Name" (pretty-name) put USA in here and then was thinking I rename the actual page/taxonomy to America.

However I want to display the pretty name on the content template but this doesn't actually seem to show up when I go to insert a custom field?

Also another issue is how to show multiple if the post belongs to multiple countries. I want to link the country name to the page URL as well.

Thanks.

#1083694

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Matt

I'm sort of following you, that you are using a taxonomy where you want to be able to output a display name for the term (e.g. "America") instead of the term name itself (e.g. "USA").

But what I'm not sure about from your description is which you want to display when.

To recap, WordPress can display:

- static pages, e.g. the "Sample Page" on a new installation. Taxonomies don't really apply here, unless you insert a View that displays posts (with the taxonomy), or a View that displays taxonomy terms;
- single posts, e.g. a custom post, which may have the taxonomy assigned. The title displayed is the title of the post, but you may want to output the name of taxonomy terms assigned to the post;
- post archives, i.e. a listing of posts (of which the blog is a special case), where again you may want to display the name of assigned taxonomy terms alongside the post title and summary
- taxonomy archives, which are still listings of posts, but only those posts with a particular taxonomy term assigned, determined by the special URL (e.g. site.com/country/canada), where the page itself would normally have as a title the term name, and where you might, optionally, want to repeat that term name against the individual posts listed on that page

Can you clarify from the above in which places it is you want to output the taxonomy but display a display name instead of the actual taxonomy term name?

#1083704

Hey

Yes of course. So its on a single post (it is in fact a custom post type).

It has the taxonomy country assigned to it.

On the actual taxonomy archive page I want to show the normal default title of the page here (I think its best to show the normal one here).

Then on the actual single post (which is a custom post type) I want to display the taxonomies under countries it belongs to. However instead of printing on the page the default titles for these I want to use the custom field "Pretty Name" for each one.

Thanks

#1083865

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Gotcha.

So, you have a taxonomy "Country" and a term meta field for "Pretty name".

Where you display a single custom post using a Content Template you want to output the Pretty name(s) of the countries assigned to that post.

You can output the taxonomy (name, slug, id, whatever) using the wpv-post-taxonomy shortcode, but that only outputs standard fields, not term meta.

For that you will need to create a View which displays taxonomies (not posts), specifically your country taxonomy terms.

It will need a filter where the "Taxonomy is set by the page where this View is inserted".

In the Loop Output section you can output the pretty name of the country terms like so:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          [wpv-taxonomy-field name="pretty-name"]
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Then insert this View into your post Content Template wherever it is you want the pretty name of the country to appear.

#1083880

Fantastic that makes sense. I have set that up and it works - one small issue I am having though is in the content I want to list them comma separated (like they were originally).

I have coded the loop like the following:

	<wpv-loop wrap="4" pad="true">
		[wpv-item index=1]
				[wpv-post-body view_template="Loop item in Country tax view"]
		[wpv-item index=other], [wpv-post-body view_template="Loop item in Country tax view"]
		[wpv-item index=4], [wpv-post-body view_template="Loop item in Country tax view"]
		[wpv-item index=pad]
		[wpv-item index=pad-last]
	</wpv-loop>

But its displaying them like this:
USA , France , Germany

As opposed to:
USA, France, Germany

Its adding a space before each comma. Is there a better way for me to code this loop?

#1084507

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Matt

You don't really need to use an inline Content Template for this, you can add the shortcode for the taxonomy field directly into the Loop Output editor.

I tried outputting just the taxonomy field (the pretty name) for the first loop item, then ", pretty-name" for subsequent iterations, and removed all unnecessary whitespace, so that my Loop Output section looked like this:

	<!-- wpv-loop-start -->
		<wpv-loop>[wpv-item index=1][wpv-taxonomy-field name="pretty-name"][wpv-item index=other], [wpv-taxonomy-field name="pretty-name"]</wpv-loop>
	<!-- wpv-loop-end -->

That worked. The next version of Views will include a new output format to output results as a comma-separated list, which will make this easier.

#1085792

That solved my comma issue but one thing I noticed actually when switching from a field insert to the view insert is its adding some formatting causing it to go on a new line when I don't want it to. How can I avoid this? As there isn't any formatting in the view itself.

#1087396

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Coming soon to Views is a raw output mode, but in the meantime all Views output content within a wrapper div.

Divs are block-level elements, i.e. start a new line.

Use your browser dev tools to inspect the source to determine a selector you can use to target this div, and change its display type to inline-block, which means it won't start a new line.

#1090454

Ah OK I see.

OK for now then I have inspected the element and identified its the following:

<div id="wpv-view-layout-25489-TCPID23624" class="js-wpv-view-layout js-wpv-layout-responsive js-wpv-view-layout-25489-TCPID23624" data-viewnumber="25489-TCPID23624" data-pagination="{"id":"25489","base_permalink":"/tv/mrmerc/?wpv_view_count=25489-TCPID23624&wpv_paged=WPV_PAGE_NUM","query":"normal","type":"disabled","effect":"fade","duration":500,"speed":5,"pause_on_hover":"disabled","stop_rollover":"false","cache_pages":"enabled","preload_images":"enabled","preload_pages":"enabled","preload_reach":1,"spinner":"builtin","spinner_image":"<em><u>hidden link</u></em>","callback_next":"","manage_history":"enabled","has_controls_in_form":"disabled","infinite_tolerance":"0","max_pages":1,"page":1,"loop":{"type":"","name":"","data":[],"id":0}}" data-permalink="/tv/mrmerc/?wpv_view_count=25489-TCPID23624">

So how can I target this div specifically to ensure it doesn't start a new line?

Cheers.

#1091192

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Target that View specifically with the following custom CSS:

#wpv-view-layout-25489-TCPID23624 {
  display: inline-block;
}
#1091246

Hey Nigel,

Added that to custom CSS but didn't make any difference? Still started on a new line?

Any ideas why this didn't sort it?