Skip Navigation

[Résolu] Syntax help with rendering custom fields from shortcode in functions.php

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.

This topic contains 14 réponses, has 3 voix.

Last updated by Eric Il y a 7 années et 6 mois.

Assigned support staff: Caridad.

Auteur
Publications
#198260

Hi,

I'm trying to pull some images uploaded via Types custom fields into a shortcode in a footer widget. I think my syntax might be off.

add_shortcode('ad_banner', 'eh_ad_banner');

function eh_ad_banner() {

     // Current Issue page
     if ( is_page(6) ) {
          echo do_shortcode(types_render_field('ad-image-current-issue', array ('size' => 'full')));
     // News page
     } else if ( is_page(14) ) {
           echo do_shortcode(types_render_field('ad-image-news', array ('size' => 'full')));
     } 
     etc....

}

I've tried placing this shortcode in the post body as well as in a footer widget, but can't get the types fields values to display. FWIW before I added the types fields I tested the output on the various pages with a little plain HTML and it worked fine, so I think my types syntax is wrong.

Thanks,
Eric

#198268

Dear Eric

The only thing I see is that you dont need do_shortcode there.

add_shortcode('ad_banner', 'eh_ad_banner');
 
function eh_ad_banner() {
 
     // Current Issue page
     if ( is_page( 6 ) ) {
          echo types_render_field('ad-image-current-issue', array ('size' => 'full'));
     // News page
     } else if ( is_page( 14 ) ) {
           echo types_render_field('ad-image-news', array('size' => 'full'));
     } 
 
}

Do you use the shorcode like this? [ad_banner]

Regards,
Caridad

#198373

Hi Caridad,

That's actually how I had the code initially, but it didn't work, so I tried adding the do_shortcode as a last-ditch effort.

Yes, that's how I use the shortcode [ad_banner]. It's supposed to get displayed in a footer widget. I'm using Genesis.

I deactivated all my plugins, except Types/Views, and switched to the 2014 theme. Then I dropped that shortcode into the body of a page. Still no luck.

Any ideas?

#198637

This probably means that its not finding page 6 or 14. Could it be that by the time you are checking the page, the global $post variable is already pointing elsewhere?

Try with code instead, which tries to check the main query:

add_shortcode('ad_banner', 'eh_ad_banner');
  
function eh_ad_banner() {
    global $wp_query;

     // Current Issue page
     if ( $wp_query->is_page( 6 ) ) {
          echo types_render_field('ad-image-current-issue', array ('size' => 'full'));
     // News page
     } else if ( $wp_query->is_page( 14 ) ) {
           echo types_render_field('ad-image-news', array('size' => 'full'));
     } 
  
}

Let me know if it works or not.
Regards,
Caridad

#199076

Hmm. That didn't work either.

This is on a dev server. I can give you access if that'll help.

BTW, if I remove the "echo types_render_field" line and replace it with some basic html, like <h1>Hello, world</h1> it shows up. So I think it finds the page ok.

Thanks,
Eric

#199539

Dear Eric,

Access to the dev server will definitely help. Let me enable the private details area below, just below the comment area.

Thanks
Caridad

#199836

Credentials sent.

#200090

Dear Eric,

Lets take the first part of the condition:

if ( $wp_query->is_front_page() ) {
		return types_render_field('ad-image-home', array ('raw' => 'true'));
}

1) An important thing is that a shortcode must always return the content, not echo it.

2) The ad-image-home custom field is in a group that is displayed when a certain template is selected. But the home page doesn't have this template selected so the ad-image-home field cant be filled in here.

Does this give you a clue of what is happening?

Regards,
Caridad

#200094

Hi Caridad,

1) Yes, I had tried using "return" in an earlier version of the function, but it hadn't worked. Not sure why I switched to "echo." Probably grasping at straws.

2) Ah! I think you're on to something! When creating the custom field group for ad-image-home (and the other related custom banner ad fields), under "Where to display this group" I chose a specific Content Template: "Ad Banners."

Apparently, my understanding is flawed. I thought the options in that section were for determining where those custom entry fields get displayed on the backend only. My goal was to display those custom entry fields on the backend ONLY for a particular content template, hoping to avoid confusion for my client. For example, I don't want them visible (on the backend) in the standard post editor. Hmmm.

Essentially, there's a single page that uses the Ad Banner content template, where we'll assign ads for the main pages of the site, and the blog posts. Those ads get pulled into the footer, with the proper ad displayed based on the page id. It "should've" been simple, but I may have made it more complicated than necessary.

I'll experiment a little and report back.

Thanks,
Eric

#200112

Confused.

For the custom field group (Ad Banners, in my case), I checked every box for every post type and template. Still no values appearing on the frontend.

I'm now thinking that my initial understanding of the "Where to display this group" options was correct—that clicking the various checkboxes determines when those custom field groups get displayed in the editor, not the frontend. Is that right? Reading the following line also makes it sound like we're only controlling when custom fields appear in the editor: "This group will appear on all edit pages where content belongs to taxonomy:..." Let me know if I'm understanding that correctly.

FYI, I'm using "return" instead of "echo" in my function. Still no luck.

If you open the page "Ad Banners" in the editor, it might make it easier to understand what I'm trying to accomplish. Does it make sense, or should I detail it a bit more? I'm really just trying to make a super-simple ad system for my client. We need to be able to assign specific ads to each of the site's main pages, and a common ad for all posts.

Thanks,
Eric

#200432

Dear Eric,

Caridad is off today, I will help you with it. What is the last version of the code are you using? In which page can I see that? Please explain a bit more where you need.

#200451

Hi Adriano,

Do you have the login credentials I sent to Caridad, or should I issue new ones for you?

My client would like the ability to upload banner ads to the footer of their site on both a page-by-page basis, and as a common ad for all posts. We’re trying to keep this simple.They don’t want to use a full-blown ad plugin or ad rotation.

The requirements are:

All banner ads to appear in a common location, the footer (using the Footer 1 widget from Genesis).
The ability to upload different banners and URLs to the main pages (home pg plus 6 others)
The ability to upload a common banner and URL that will appear on all standard blog posts (rather than assigning them one-by-one to each post)
The ability to upload a common banner and URL for all posts of a custom post type
The ability to upload a default ad and URL as a fallback.

Here’s how I thought it would work, and it almost does...

Create a custom field group (Ad Banners) with image and URL fields corresponding to the main pages, posts, and a default fallback, like so:

ad-banner-home
ad-banner-URL-home

ad-banner-some-page
ad-banner-URL-some-page

ad-banner-another-page
ad-banner-URL-another-page

etc…

Create a page with a content template “ad banners?? that would display all these fields on a single page in the editor. I don't want all these custom fields displayed in the editor for every page/post. The Ad Banners page would function like a dashboard for all the banner ads, rather than assigning the ads in the editor for individual pages, which wouldn’t be realistic for all the blog posts.

Create a function that would check for page-id, and return the corresponding custom fields.

Call that function with a shortcode in the "Footer 1" widget.

If you login and open the editor for the page “Ad Banners,?? I think it’ll start to make sense.

The function (at the very bottom of functions.php) is a little messy right now. There are several places where I’m just testing to see if we’re getting the correct page ID, and then displaying some HTML in the footer as proof. Load /editorial-team for example. The function/shortcode works in terms of getting the page-id and displaying HTML, but I can’t seem to get the custom field values to return.

I hope that makes sense. I know this sounds convoluted for something that should be really simple.

Thanks,
Eric

#200965

Hi,

I still need help with this, please. Site launch is delayed pending the inclusion of this feature...

Adriano, I replied to you ~30 minutes of your post, but never heard back. Was that at the end of your day Friday? The time difference is throwing me off.

Do you need more information from me? I'd really like to get this resolved ASAP so my client can launch their site.

Thanks,
Eric

#201208

Hello?

It's been a few days and I'm no closer to a resolution. Is this issue still assigned? I would really appreciate some help. It's holding up a launch.

Thanks,
Eric

#201219

Never mind. I'm going to look for another solution. 🙁