Skip Navigation

[Resolved] Check the value of the Metabox of current post content template

This support ticket is created 4 years, 9 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 3 voices.

Last updated by Christian Cox 4 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1298945

I am developing a Theme and Christian has already helped me by displaying the Metabox of the Theme Settings in the Content Template of new Block Editor plugin.
https://toolset.com/forums/topic/add-theme-metabox-to-toolset-blocks-plugin/

The Metabox is saving each value as expected in the database with the content template ID.
e.g. when saving value of content template for "Posts" then the metabox values get saved with the ID of the content template.

I am using following code to get the value of theme options and metaboxes:

1- get the global theme value only if metabox not set.
2- if metabox value is set then it will override the global theme settings value and get the metabox value of the current page.

// Header Settings Value
function kl_header($id)
{
  $themeHeader_value = get_option('mytheme-enable-header');

  if(getMetaValue($id,"mytheme-main-header-display") && getMetaValue($id,"mytheme-main-header-display")!='default')
  {
    return getMetaValue($id,"mytheme-main-header-display");
  }
  else 
  {
    return $themeHeader_value;
  }

}

The Problem is with this code now when using the metabox in a content template that saving the Metabox values for a Content Template this means only if this content template is displayed the theme settings will effect. so i need a way to get the metbaox values of the Template Content of current post so i can display parts of the theme conditionally. this how it should be the final results:

1- if current post metabox values are set then override global theme settings and override content template settings and return.
2- else if metabox of current post are default and metabox values of the content template of current post are not default then return values.
3- else it should return the global values of the global theme settings

priority 1: is to return metabox of current post values
priority 2: is to return metabox values of current content template of current post
priority 3: is to return Global theme settings if first 2 priorities are set to default.

how can i get the metabox values of the current content template of current post?

#1299137

Nigel
Supporter

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

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

Hi there

Christian is not online until later, but I think it would be best if he continued this with you, so let me assign this to Christian who will respond later today.

#1299599

So it sounds like you need a way to programmatically determine which Content Template is applied to the current post. In Toolset, the Content Template assigned to a post can be determined by examining the postmeta for that post, and finding the value for the hidden custom meta field "_views_template".

// $post is the global post object, or some post ID
$template_id = get_post_meta( $post, '_views_template', true );
#1299693

Awesome it lookes like its working now as expected with following code

// Header Settings Value
function kl_views_template($id){

  $themeHeader_value = get_option('mytheme-enable-header');
  $template_id = get_post_meta( $id, '_views_template', true );
  $value_content_template = get_post_meta( $template_id, 'mytheme-main-header-display', true );

  if(getMetaValue($id,"mytheme-main-header-display") && getMetaValue($id,"mytheme-main-header-display")!='default'){

      return getMetaValue($id,"mytheme-main-header-display");

  } elseif(getMetaValue($id,"mytheme-main-header-display") && getMetaValue($id,"mytheme-main-header-display")=='default' && ($value_content_template!='default') ) {

      return $value_content_template;
      
  } else {

    return $themeHeader_value;
  }

}

I just replaced this:

get_post_meta( $post, '_views_template', true )

with:

get_post_meta( $id, '_views_template', true )

just let me know if you think this is ok. Thanks again Christian

#1299771

Okay great, glad it works. It looks like you're getting the ID of the Content Template correctly based on the post ID and get_post_meta, so I can confirm that much looks good. Good luck with this project!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.