پرش به محتویات

"Filter: bricks/active_templates"

الگوهای اعمال شده در یک صفحه را به صورت برنامه نویسی اصلاح کنید (@since 1.8.4)

این جایگزینی برای تنظیم یک الگوی فعال از طریق فیلتر bricks/screen_conditions/scores است.

یادداشت

لطفاً توجه داشته باشید که این فیلتر پس از bricks/screen_conditions/scores اجرا می‌شود

Example: Do not use the Single template if the post has been edited with Bricks

در این مثال، می‌خواهیم اگر پست تکی حاوی داده‌های Bricks (ویرایش با Bricks) باشد، از استفاده از یک الگو حذف کنیم.

add_filter( 'bricks/active_templates', 'set_my_active_templates', 10, 3 );
function set_my_active_templates( $active_templates, $post_id, $content_type ) {
  // Return if single post $content_type is not 'content'
  if ( $content_type !== 'content' ) {
    return $active_templates;
  }

  // Return: Current post type is not 'post'
  $post_type = get_post_type( $post_id );

  if ( $post_type !== 'post' ) {
    return $active_templates;
  }

  /**
   * $active_templates is an array with different important keys
   *
   * $active_templates['header'] is the header template ID, set it to 0 if do not want to use any template
   * $active_templates['content'] is the content template ID, set it to current post ID if do not want to use any template
   * $active_templates['footer'] is the footer template ID, set it to 0 if do not want to use any template
   *
   * $active_templates['search'] is the search template ID, will only be used if $content_type is 'search'
   * $active_templates['archive'] is the archive template ID, will only be used if $content_type is 'archive'
   * $active_templates['error] is the error template ID, will only be used if $content_type is 'error'
  */

  // Check if the current post has Bricks data, return value is an array
  $bricks_data = \Bricks\Database::get_data( $post_id, 'content' );

  if ( count( $bricks_data ) > 0 ) {
    // Has Bricks data: Don't use any template, set the $active_templates['content'] to current post ID
    $active_templates['content'] = $post_id;

    // To disable header & footer (e.g. landing page) set $active_templates['header'] & $active_templates['footer'] to 0
    $active_templates['header'] = 0;
    $active_templates['footer'] = 0;
  }

  return $active_templates;
}

Example: Change a single template based on a custom field

سناریویی مانند داشتن چندین قالب تک برای یک نوع پست سفارشی وجود دارد. با استفاده از این فیلتر می‌توانید تصمیم بگیرید که کدام قالب را بر اساس یک فیلد سفارشی اعمال کنید.

add_filter( 'bricks/active_templates', 'set_active_templates_by_custom_field', 10, 3 );

function set_active_templates_by_custom_field( $active_templates, $post_id, $content_type ) {
  // Return if single post $content_type is not 'content'
  if ( $content_type !== 'content' ) {
    return $active_templates;
  }

  // Return: Current post type is not 'project'
  $post_type = get_post_type( $post_id );

  if ( $post_type !== 'project' ) {
    return $active_templates;
  }

  // Get the custom field value from Metabox
  $value = absint( rwmb_meta( 'use_template_id' ) );

  // Value not empty: Set $active_templates['content'] to the value
  if ( $value > 0 ) {
    $active_templates['content'] = $value;
    // If single template, the page settings will be used, so no need to set header and footer
  }

  return $active_templates;
}

Example: Disable active template in the builder

از Bricks 1.12، الگوهای اعمال شده روی یک صفحه اکنون در Builder نیز نمایش داده می‌شوند. قبلاً فقط عنصر محتوای پست قابل مشاهده بود، که استایل دادن به عناصر اطراف را دشوار می‌کرد. اگر رفتار قدیمی‌را ترجیح می‌دهید و می‌خواهید الگوی اعمال شده فقط در سازنده را غیرفعال کنید، می‌توانید از این فیلتر استفاده کنید:

add_filter( 'bricks/active_templates', 'disable_template_in_builder', 10, 3 );

function disable_template_in_builder( $active_templates, $post_id, $content_type ) {
  // Only run my logic in the Builder
  if ( bricks_is_builder() ) {
    $active_templates['content'] = 0;
  }

  return $active_templates;
}

توضیح:

  • این عملکرد فقط زمانی اجرا می‌شود که Bricks در حالت Builder (bricks_is_builder()) باشد.
  • با تنظیم $active_templates['content'] = 0 از اعمال الگوی تک فعال در Builder جلوگیری می‌کند.
  • قسمت جلویی بی‌تأثیر باقی می‌ماند. این الگو همچنان هنگام مشاهده پست/صفحه به طور معمول استفاده می‌شود.
  • این امر به طور موثر قابلیت مشاهده عناصر محتوای پست خارجی ** معرفی شده در Bricks 1.12 را غیرفعال می‌کند و رفتار سازنده قبلی را بازیابی می‌کند.