Tahák k tvorbě webů

WordPress

Plugin Advanced Custom Fields (ACF)

Zobrazí obsah pole

	the_field('page_title');

Získá obsah pole

	$page_title = get_field('page_title');
	echo $page_title;

Obrázek

	$featured_image = get_field('featured_image');
	$featured_image_lg = $featured_image['sizes']['large'];
	echo '<img src="' . $featured_image_lg . '" class="img-fluid" />';

Vztah team_member <=> location

Šablona pro team_member:

	$locations = get_field('location');  // typ pole je Vztah
	foreach ($locations as $post) {      // musí být $post
		setup_postdata($post);           // zpřístupním $post
		echo the_title();                // mám zpřístupněné všechny fce $post
		echo the_field('location_addr'); // mám zpřístupněné ACF pro location
		wp_reset_postdata();			 // za foreach budu mít zase původní $post
	}

Šablona pro location:

	$args = [
		'post_type'		=> 'team',
		'meta_query'	=> [
			'key'		=> 'location',
			'value'		=> '"' . get_the_ID() . '"',
			'compare'	=> 'LIKE'
		]
	];
	$team_members = get_posts($args);
	foreach ($team_member as $post) {
		setup_postdata($post);
		echo the_title();
		echo the_field('team_member_name');
		wp_reset_postdata();
	}

Filtruje příspěvky na základě ACF pole

Tento kód se uplatní jen na příspěvky typu "reference". Pokud je zadaný $_GET parametr typ audio nebo video, tak se zobrazí jen příspěvky, které mají v ACF definován typ_reference audio nebo video.

function reference_pre_get_posts($query) {
	$post_type = $query->get('post_type');
	if ($post_type == 'reference') {
		if (isset($_GET['typ'])) {
			$typ = $_GET['typ'];
			if (in_array($typ, array('audio', 'video'))) {
				$query->set('meta_key', 'typ_reference');
				$query->set('meta_value', $typ);
			}
		}
	}
}
add_filter('pre_get_posts', 'reference_pre_get_posts');