How to output custom field name and value in Wordpress - wordpress

I created a custom post type and am adding a few custom fields into it.
Currently my loop looks like this:
<?php
//* The Query
$exec_query = new WP_Query( array (
'post_type' => 'jobs',
'job_role' => 'fryking',
'posts_per_page' => 4,
'order' => 'ASC'
) );
//* The Loop
if ( $exec_query->have_posts() ) {
while ( $exec_query->have_posts() ): $exec_query->the_post();
echo '<div class="subcategory">';
echo '<h3 class="cat_title">';
the_title();
echo '</h3>';?>
<div class="cat_content">
<div class="left">
<?php the_content();
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
the_field('hake_and_chips');
?>
</div>
<div class="right">
<?php
echo '<div class="menu_image" style="background: url('. $url.')">';
echo '</div>';?>
</div>
</div>
</div>
<?php
endwhile;
//* Restore original Post Data
wp_reset_postdata();
}
?>
I managed to get my field value with this code:
the_field('hake_and_chips');
How can I get the field name?
Hope you can help

These fields are stored in post meta table so you can get this custom field value using get_post_meta function also.
Try this code to get single value of custom field:
echo get_post_meta($post->ID, 'hake_and_chips', true);
Hope this will helpful for you. Thanks.

Related

ACF loop for grid, get/display custom field

I'm using this piece of code in my Wordpress template:
<?php
$args = array( 'numberposts' => '12', 'post_type' => 'training', 'post_status' => 'publish' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<div class="col-xs-12 col-md-4"><article><div class="kartel"><a href="' . get_permalink($recent["ID"]) . '">';
if ( has_post_thumbnail( $recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'medium');
}
echo '</a></div><h3>' . $recent["post_title"].'</h3> ';
echo '<em>Doelgroep //</em>
<p>One-liner/super korte omschrijving</p>';
echo 'Tell me more ';
echo '</article></div>';
}
wp_reset_query();
?>
Thing is that I now want to add a custom field (let's say 'custom_field') that displays a custom excerpt underneath the thumbnail for the grid. I can get the usual fields (excerpt, title, etc.) but not the custom fields. For example the_field ('custom_field'); isn't working..
Any ideas/suggestions?
Hope to hear from you guys!
Filt
First of all change your approach to queries and Wordpress loops using the WP_Query class.
<?php
$args = array( 'numberposts' => '12', 'post_type' => 'training', 'post_status' => 'publish' );
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ){
$loop->the_post(); ?>
<div class="col-xs-12 col-md-4">
<article>
<div class="kartel">
<a href="<?php the_permalink(); ?>">
<?php if( has_post_thumbnail("medium") ) {
the_post_thumbnail( "medium" );
}
?>
</a>
</div>
<a href="<?php the_permalink(); ?>">
<?php the_title("<h3>","</h3>"); ?>
</a>
<em>Doelgroep //</em>
Tell me more
</article>
</div>
<?php }
}
wp_reset_postdata();
?>
Later, in the loop of your posts, you can recall the custom field using:
the_field ('custom'); //which prints the result on screen
$var = get_field ('custom'); //or echo get_field ('custom') which returns the content in a variable.
If you want to recall a specific custom field inserted in a post or page or custom post type, you must use the following syntax:
the_field ('custom', $ post_id);
get_field ('custom', $ post_id)
That's all :)

Custom post type, sort taxonomy terms by a meta box value

I've have created a custom post type with a taxonomy. I need to sort the taxonomy posts by a meta value in my custom post type.
I am using the plugin Advanced Custom Fields to insert a separate meta value field to my posts in my custom post types. So far I've
been able to get posts taxonomies sort by its value. My problem is that I get "all" of my taxonomies post printing out on the front-end.
I would like to get the posts for each taxonomies it's related to, not ALL. I've been testing to get the term id by implement it on the
wp_query array but with no success. I simply want to display my taxonomies individually and grab its related posts, is this possible, what am I doing wrong?
Here's my code...
<?php
get_header();
if( have_posts() ) {
?>
<section class="section container">
<div class="row">
<?php
// gets our taxonomy title
global $wp_query;
$term = $wp_query->get_queried_object();
$title = $term->name;
?>
<!-- show our taxonomy title on the front-end of the site -->
<header id="<?php echo $term->slug;?>" class="col-md-12">
<h1><span class="glyphicon glyphicon-circle-arrow-left" aria-hidden="true"></span><?php echo $title; ?></h1>
</header>`enter code here`
<?php
// wp_query-loop for our custom post typ "Personal", sort by meta_value
$wp_query = new WP_Query(array(
'post_type' => 'Personal',
'posts_per_page' => -1,
'meta_key' => 'sorterings_nummer',
'orderby' => '$term->term_id meta_value',
'ordertax' => 'DESC',
'order' => 'ASC'
));
// gets our custom post type posts with a thumbnail, title and contact details
while( $wp_query->have_posts() ) {
$wp_query->the_post();
$class = get_field('sorterings_nummer') ? 'class="sorterings_nummer"' : '';
$titel = get_post_meta(get_the_ID(), 'titel');
$telefon = get_post_meta(get_the_ID(), 'telefon');
$mobil = get_post_meta(get_the_ID(), 'mobil');
$mail = get_post_meta(get_the_ID(), 'mail');
add_filter('the_content', 'wpautop');
?>
<article class="col-xs-6 col-sm-4 col-md-4 col-lg-3" >
<div class="align-center">
<div class="content--thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<header class="content--title">
<h2><?php the_title(); ?></h2>
</header>
<section class="content--contactDetails">
<h3 class="titel"><?php echo $titel[0] ? $titel[0]: ''; ?></h3>
<p class="telefon"><strong><?php echo $telefon[0] ? $telefon[0]: ''; ?></strong></p>
<p>
<a class="mail" href="mailto:<?php echo $mail[0] ?>"><?php echo $mail[0] ? $mail[0]: ''; ?></a>
<a class="mobil" href="tel:<?php echo $mobil[0] ?>"><?php echo $mobil[0] ? $mobil[0]: ''; ?></a>
</p>
</section>
</div>
</article>
<?php
} // while content
?>
</div> <!-- .row -->
</section> <!-- .container -->
<?php
} // if
get_footer();
Please try tax_query() for retrieving posts assigned to the particular taxonomy term.
Please find your updated $wp_query() code below:
$wp_query = new WP_Query(array(
'post_type' => 'Personal',
'posts_per_page' => -1,
'tax_query' => array(array('taxonomy' => 'taxonomy_slug_name', 'field' => 'id', 'terms' => $term->term_id )),
'meta_key' => 'sorterings_nummer',
'orderby' => 'meta_value',
'ordertax' => 'DESC',
'order' => 'ASC'
));
Please write your 'taxonomy_slug_name' in the taxonomy column of tax_query().
Hope, this may be helpful to you.

ACF + Isotope: Filter Post Object Data

I'm trying to filter data from a custom post-type called "Clients", based on category. What I need to appear is the logo for each particular client.
I've set up a repeater field of Post Objects, so I can change the order that the logos will be displayed.
What I have currently works, however I cannot figure out how to incorporate the Post Object selector, so that what appears is determined by the instances I've added via the Repeater.
Here is the link to the site. Appreciate any answers!
See below for screenshot of my dashboard setup:
<ul id="filters">
<?php
$terms = get_terms("category", array(
'orderby' => 'slug'
)); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php $the_query = new WP_Query(array(
'post_type' => 'clients',
'posts_per_page' => '-1',
'order' => 'ASC'
)); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="isotope-list-container">
<div id="isotope-list" class="row small-up-1 medium-up-2 large-up-3">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<div class="<?php echo $termsString; ?> portfolio columns"> <?php // 'portfolio' is used as an identifier (see Setp 5, line 6) ?>
<div class="portfolio-item-container">
<?php
$image = get_field('logo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
</div> <!-- end portfolio item -->
<?php endwhile; ?>
</div> <!-- end isotope-list -->
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
I was able to make this considerably simpler by using the Post Types Order plugin to reorder my posts.
https://wordpress.org/plugins/post-types-order/

Advanced Custom Fields and Google Maps Multiple Markers

I have some code that generates a Google Map to show a marker based on the location field supplied via a custom field named 'event_map'. It works great.
<?php
$location = get_field('event_map');
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
</div>
<?php endif; ?>
I now want to add a map that shows ALL of the markers from ALL of the posts from the 'tour-dates' custom post type. How would I go about doing this assuming all of these posts have a field called 'event_map' to draw location data from? I am stumped and any help appreciated.
Get the posts with such post type and loop on it then use the ACF API get_field (the second parameter let us define the post ID in which your custom field value belongs to).
<?php
$tours = get_posts( array(
'post_type' => 'tour-dates',
'posts_per_page' => -1
));
if( !empty($tours) ): ?>
<div class="acf-map">
<?php foreach($tours as $tour): ?>
<?php
$location = get_field('event_map',$tour->ID);
if( !empty($location) ): ?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
This query should get all tour-dates posts that have information in the event_map field.
$new_loop = new WP_Query( array(
'post_type' => 'tour-dates',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_map',
'compare' => 'EXISTS'
)
)
) );
I'm not sure what you're using to display the map, so I can't help beyond that.

Displaying Custom Category loop in homepage

Thing is, In the homepage of my theme, I want to show post from different category in Different Div. Each DIV will contain 3 post from a category. I need a loop that can pick last 3 post from a specific Category. Can't find any suitable ans for it.
To explain things more easily, here is a demo picture of the Content section,
http://i.imgur.com/5QSzAIS.png
It will be a great help, if someone help me with the code !
<?php query_posts('cat=10&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This should get you started. You need to use this code twice. Where it says cat=10, you should enter your category ID (you can check this when you click on a Category from the admin panel, the the browser it will show something like this http://yourwebsite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=4&post_type=post)
Where it says tag_ID is the ID of your category.
I'm currently using a different method on a page of the site I'm building which allows me to run multiple loops in one page and specify the category for each one. This method I personally like better as it is more straightforward to me, and I can define the category with the slug instead of the ID.
Instead of using have_posts() and such, you use WP_Query() after defining your array and then wp_reset_postdata() to end your loop. The benefit is that you can keep running loops this way.
I'm also loading the data from custom fields in my posts using get_post_meta, but this method will work without that stuff.
<div class="audioGrid">
<?php
$args = array( 'post_type' => 'post',
'category_name' => 'audio',
'posts_per_page' => 3,
'order' => 'DESC' );
$query1 = new WP_Query($args);
while ( $query1->have_posts() ) {
$query1->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'audio-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'audio_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Audio Grid ?>
<?php wp_reset_postdata(); ?>
<div class="videoGrid">
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'order' => 'DESC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'video-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'video_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Video Grid ?>
<?php wp_reset_postdata(); ?>
Another cool thing I'm doing is using a custom field to define the order of things and using meta_key and meta_value_num to get that number and force the order how I want, and since this site isn't complicated, defining the order this way is convenient. I just use leading zeroes to make it easy: 001, 002, 003, etc
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'meta_key' => 'video_order',
'orderby' => 'meta_value_num',
'order' => 'ASC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
Anyway, hope this helps if you need to use multiple loops to pull posts from different categories.

Resources