WordPress.org

Plugin Directory

WCK - Custom Fields and Custom Post Types Creator

A must have tool for creating custom fields, custom post types and taxonomies, fast and without any programming knowledge.

How do I display my custom fields in the front end?

Let's consider we have a meta box with the following arguments: - Meta name: books - Post Type: post And we also have two fields defined: - A text custom field with the Field Title: Book name - And another text custom field with the Field Title: Author name

You will notice that slugs will automatically be created for the two text fields. For 'Book name' the slug will be 'book-name' and for 'Author name' the slug will be 'author-name'

Let's see what the code for displaying the meta box values in single.php of your theme would be:

<?php $books = get_post_meta( $post->ID, 'books', true ); 
foreach( $books as $book){
    echo $book['book-name'] . '<br/>';
    echo $book['author-name'] . '<br/>';
}?>

So as you can see the Meta Name 'books' is used as the $key parameter of the function get_post_meta() and the slugs of the text fields are used as keys for the resulting array. Basically CFC stores the entries as custom fields in a multidimensional array. In our case the array would be:

<?php array( array( "book-name" => "The Hitchhiker's Guide To The Galaxy", "author-name" => "Douglas Adams" ),  array( "book-name" => "Ender's Game", "author-name" => "Orson Scott Card" ) );?>

This is true even for single entries.

How to query by post type in the front-end?

You can create new queries to display posts from a specific post type. This is done via the 'post_type' parameter to a WP_Query.

Example:

<?php $args = array( 'post_type' => 'product', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    the_title();
    echo '<div class="entry-content">';
    the_content();
    echo '</div>';
endwhile;?>

This simply loops through the latest 10 product posts and displays the title and content of them.

How do I list the taxonomies in the front-end?

If you want to have a custom list in your theme, then you can pass the taxonomy name into the the_terms() function in the Loop, like so:

<?php the_terms( $post->ID, 'people', 'People: ', ', ', ' ' ); ?>

That displays the list of People attached to each post.

How do I query by taxonomy in the front-end?

Creating a taxonomy generally automatically creates a special query variable using WP_Query class, which we can use to retrieve posts based on. For example, to pull a list of posts that have 'Bob' as a 'person' taxomony in them, we will use:

<?php $query = new WP_Query( array( 'person' => 'bob' ) ); ?>

Requires: 3.1 or higher
Compatible up to: 4.1.5
Last Updated: 2015-2-26
Active Installs: 10,000+

Ratings

4.7 out of 5 stars

Support

0 of 5 support threads in the last two months have been resolved.

Got something to say? Need help?

Compatibility

+
=
Not enough data

0 people say it works.
0 people say it's broken.

0,1,0
71,7,5 100,1,1
100,1,1
100,2,2 100,1,1 100,1,1 100,1,1 100,1,1
100,1,1
100,1,1 100,1,1 100,2,2 100,1,1
100,3,3
100,2,2 100,2,2 100,1,1 100,1,1
100,2,2
0,1,0
0,1,0 0,1,0
100,1,1 100,1,1
100,2,2
100,5,5 100,4,4 100,1,1
100,1,1 100,2,2
100,4,4 100,1,1
100,1,1