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 :)
Related
I have created a custom post type and taxonomy on my website but when I view the taxonomy page it is showing the incorrect page title. Rather than showing 'Taxonomy Term Name' as the Title it is showing the title of the first post shown on that page?
This is my taxonomy page template...
<div class="one-whole">
<!-- Course Tax -->
<?php
// Get the taxonomy
$taxonomy = get_queried_object();
$id = $taxonomy->term_id;
$name = $taxonomy->name;
$slug = $taxonomy->slug;
?>
<h1><?php echo $name; ?> Training Courses</h1>
<?php echo term_description( $id, $name ) ?>
<hr>
<section class="flexbox one-whole">
<?php // Create a custom loop for all items in this taxonomy
$args = array(
'post_type' => 'htl_training',
'posts_per_page' => -1,
'order' => 'asc',
'orderby' => 'name',
'tax_query' => array ( array(
'taxonomy' => 'course-categories',
'terms' => $taxonomy->slug, // Taxonomy Term to Search in
'field' => 'slug',
'operator' => 'IN')
)
);
$posts = new WP_Query($args);
if($posts->have_posts()) :
while($posts->have_posts()) :
$posts->the_post(); ?>
<!-- Card -->
<div class="card card-title-below">
<?php $image = get_field('accrediting_body'); ?>
<?php if( !empty($image) ){ ?>
<?php
// check if the repeater field has rows of data
if( have_rows('endorsing_bodies','options') ){
// loop through the rows of data
while ( have_rows('endorsing_bodies','options') ) {
the_row();
$hook = get_sub_field('logo_hook', 'options');
$icon = get_sub_field('logo','options');
$accrediting_body = get_field('accrediting_body');
if( $accrediting_body ){ ?>
<div class="training-provider">
<?php if( $accrediting_body === $hook ) {
echo '<img src="' . $icon['url'] . '" alt="' . $hook .'" />';
} ?>
</div>
<?php
}
}
} else { } ?>
<?php } ?>
<a href="<?php the_permalink(); ?>" class="non-animated-link">
<?php htl_the_post_thumbnail(); ?>
</a>
<h4><?php the_title(); ?></h4>
</div><!--/ Card -->
<?php
endwhile;
else:
echo' Oops, there are no posts';
endif;
?>
<?php wp_reset_postdata(); ?>
</section>
<!-- Course Tax -->
</div>
How can I show the taxonomy term name as the page title?
Use following function to display archive title
the_archive_title();
More info here
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.
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/
I have a custom post type with a custom field created with Advanced Custom Fields. I can retrieve all of the posts of this type and display the images with the following code:
<?php
$args = array( 'post_type' => 'image' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $image = get_field('image_file'); ?>
<img src="<?php echo $image['url']; ?>" />
<?php endwhile; ?>
However, I want to be able to retrieve a single image url without looping through all of them. How would I go about this?
I've tried this:
<?php
$args = array( 'post_type' => 'image', 'title' => 'Welcome to bird - image 1' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $image = get_field('image_file'); ?>
<img src="<?php echo $image['url']; ?>" />
<?php endwhile; ?>
But it outputs the entire list - how can I just get a single post? If it helps, the permalink is like this:
/?image=welcome-to-bird-image-1
Unfortunately you cannot use the X_posts table columns names as arguments in the WP_Query class in order to filter your database posts.
The only solution is to use the posts_where filter and then to apply regular MySQL code to filter the posts. Something like that:
function filter_where_title($where = '')
{
$where .= " OR post_title LIKE '%" . $your_term_here . "%'";
return $where;
}
$posts = new WP_Query();
add_filter('posts_where', 'filter_where_title');
Note : The above example is general, and you have to apply extra code in order to strict the filtering in your custom post records only. The above code can affect other post types queries too.
I've now found the solution:
<?php
$args = array( 'post_type' => 'image', 'image' => 'welcome-to-bird-image-3' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $image = get_field('image_file'); ?>
<img src="<?php echo $image['url']; ?>" />
<?php endwhile; ?>
Filtering by 'image' worked as I wanted.
I'm using already designed theme for wordpress, and now instead of regular blog posts I would like to display WooCommerce products (which are custom post types I persume).
This is the current query with display loop:
<?php
$args = array(
//'posts_per_page' => '2',
'paged' => get_query_var('paged')
);
$homepage_query = new WP_Query($args);
?>
<?php //query_posts('posts_per_page=4&paged='.get_query_var('paged')); ?>
<?php if ( have_posts() ) : ?>
<?php while ( $homepage_query->have_posts() ) : $homepage_query->the_post(); ?>
<?php if($style == 'blog_style') { ?>
<div id="blog-style" class="post-box">
<?php get_template_part('content', 'blog'); ?>
</div>
<?php } else { ?>
<div class="post-box grid_4 <?php aero_post_box_class(); ?>">
<?php get_template_part('content', ''); ?>
</div>
<?php } ?>
<?php endwhile; ?>
Is there a way to add options to $args so the loop displays WooCommerce products? I'm also using pagination with this loop, which is required on this project, so that's why it's important to use this loop.
You should be able to access products through the loop, setting the post_type arg to product:
<?php
// Setup your custom query
$args = array( 'post_type' => 'product', ... );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
This is the proper way to re-create and customize the WooCommerce product loop:
if(!function_exists('wc_get_products')) {
return;
}
$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1; // if your custom loop is on a static front page then check for the query var 'page' instead of 'paged', see https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters
$ordering = WC()->query->get_catalog_ordering_args();
$ordering['orderby'] = array_shift(explode(' ', $ordering['orderby']));
$ordering['orderby'] = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
$products_per_page = apply_filters('loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page());
$products_ids = wc_get_products(array(
'status' => 'publish',
'limit' => $products_per_page,
'page' => $paged,
'paginate' => true,
'return' => 'ids',
'orderby' => $ordering['orderby'],
'order' => $ordering['order'],
));
wc_set_loop_prop('current_page', $paged);
wc_set_loop_prop('is_paginated', wc_string_to_bool(true));
wc_set_loop_prop('page_template', get_page_template_slug());
wc_set_loop_prop('per_page', $products_per_page);
wc_set_loop_prop('total', $products_ids->total);
wc_set_loop_prop('total_pages', $products_ids->max_num_pages);
if($products_ids) {
do_action('woocommerce_before_shop_loop');
woocommerce_product_loop_start();
foreach($products_ids->products as $featured_product) {
$post_object = get_post($featured_product);
setup_postdata($GLOBALS['post'] =& $post_object);
wc_get_template_part('content', 'product');
}
wp_reset_postdata();
woocommerce_product_loop_end();
do_action('woocommerce_after_shop_loop');
} else {
do_action('woocommerce_no_products_found');
}
Using the code above, you would customize the wc_get_products() arguments to get the IDs of the products you want (if you have specific criteria). Once that code is in place, all the features of a native WooCommerce loop will be available to you—pagination, ordering, etc. This method is superior to WP_Query and get_posts() because those two methods can break.
I've written a more detailed blog post about custom WooCommerce loops here: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/
You can Also get Category using thi code
$terms = get_terms('product_cat');
foreach ($terms as $term) {
$term_link = get_term_link( $term, 'product_cat' );
echo '<li>' . $term->name . '</li>';
}
If You want only parent category then
wp_list_categories('taxonomy=product_cat&orderby=order&title_li=&depth=1');