Condition – Post Type

Bricks bietet von Beginn an viele Bedingungen, mit deren Hilfe Inhaltselemente bedingt ausgegeben werden können.

Es fehlt dabei jedoch eine Bedingung, mit der direkt auf den Beittragstyp (post_type) geprüft werden kann. Man kann diese fehlende Bedingung zwar auch mit Hilfe einer Kombination aus Echo-Anweisung und dem entsprechenden Wordpress API-Funktionsaufruf bilden, was aber nicht so elegant ist.

Zum Ausgleich bietet dafür die Bricks-Academy eine Anleitung, wie man eigene Bedingungen (Conditions) erstellen kann.

Abweichend vom Beispiel auf Bricks-Academy enthält nachfolgender Code keinen Block für die Gruppe an Filtern. Der Filter wird mit diesem Code direkt in der Gruppe „Beitrag“ eingefügt.

Code

Für die Verwendung dieser Condition den nachfolgenden Code entweder in die function.php einfügen, oder mittels Snippets-Plugin oder einem ähnlichen Plugin einbinden.

add_filter( 'bricks/conditions/options', 'sc_bricks_condition_options_post_type' );
function sc_bricks_condition_options_post_type( $options ) {
  // Ensure key is unique, and that group exists
  $options[] = [
    'key'   => 'sc_condition_post_type',
    'label' => esc_html__( 'Post type', 'bricks' ),
    'group' => 'post',
    'compare' => [
      'type'        => 'select',
      'options'     =>  [
        '==' => esc_html__( 'is', 'bricks' ),
        '!=' => esc_html__( 'is not', 'bricks' ),
      ],
      'placeholder' => esc_html__( 'is', 'bricks' ),
    ],
    'value'   => [
      'type'        => 'text',
    ],
  ];

  return $options;
}

add_filter( 'bricks/conditions/result', 'sc_bricks_condition_result_post_type', 10, 3 );
function sc_bricks_condition_result_post_type( $result, $condition_key, $condition ) {
	
	// If $condition_key is not 'my_post_type', we return the $render as it is
	if ( $condition_key !== 'sc_condition_post_type' ) {
		return $result;
	}

	// Now you can perform your logic by using the $condition variable
	// $condition['compare'] is the compare operator, might be empty
	// $condition['value'] is the user value, might be empty

	// In my example, if compare is empty, we set it to '==' as default
	$compare    = isset( $condition['compare'] ) ? $condition['compare'] : '==';
	$user_value = isset( $condition['value'] ) ? $condition['value'] : '';

	$condition_met = false;

	// Get the current post type of the page
	$current_post_type = get_post_type();

	switch( $compare ) {
		case '==': // "is"
			$condition_met = $current_post_type === $user_value;
			break;
		case '!=': // "is not"
			$condition_met = $current_post_type !== $user_value;
			break;
	}

	return $condition_met;
}

Code basierend auf der Dokumentation von bricks.academy

Durch die weitere Nutzung der Seite stimmst du der Verwendung von Cookies zu. Weitere Informationen

Die Cookie-Einstellungen auf dieser Website sind auf "Cookies zulassen" eingestellt, um das beste Surferlebnis zu ermöglichen. Wenn du diese Website ohne Änderung der Cookie-Einstellungen verwendest oder auf "Akzeptieren" klickst, erklärst du sich damit einverstanden.

Schließen