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/
Related
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 :)
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 am in charge of managing this site F9 Properties which is built in WordPress. On the home page there is a featured properties section. I noticed that if you listed a property with two different "Status" such as "For Sale or For Lease, the property appeared twice in the carousel. Below is the code for listing the featured properties. I can see that it filters out the properties with the Status "Leased". Can anyone help me add a bit of code to list only one property per post regardless of how many different property status it has?
<?php
/* Featured Properties Query Arguments */
$featured_properties_args = array(
'post_type' => 'property',
'posts_per_page' => 100,
'meta_query' => array(
array(
'key' => 'REAL_HOMES_featured',
'value' => 1,
'compare' => '=',
'type' => 'NUMERIC'
)
)
);
$featured_properties_query = new WP_Query( $featured_properties_args );
if ( $featured_properties_query->have_posts() ) :
?>
<section class="featured-properties-carousel clearfix">
<?php
$featured_prop_title = get_option('theme_featured_prop_title');
$featured_prop_text = get_option('theme_featured_prop_text');
if(!empty($featured_prop_title)){
?>
<div class="narrative">
<h3><?php echo $featured_prop_title; ?></h3>
<?php
if(!empty($featured_prop_text)){
?><p><?php echo $featured_prop_text; ?></p><?php
}
?>
</div>
<?php
}
?>
<div class="carousel es-carousel-wrapper">
<div class="es-carousel">
<ul class="clearfix">
<?php
while ( $featured_properties_query->have_posts() ) :
$featured_properties_query->the_post();
?>
<?php
$status_terms = get_the_terms( $post->ID,"property-status" );
if(!empty( $status_terms )){
foreach( $status_terms as $status_term ){
if($status_term->name=="Leased"){}else{
?>
<li>
<figure>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php
the_post_thumbnail('property-thumb-image',array(
'alt' => get_the_title($post->ID),
'title' => get_the_title($post->ID)
));
?>
</a>
</figure>
<h4><?php the_title(); ?></h4>
<p><?php framework_excerpt(8); ?> <?php _e('Know More','framework'); ?> </p>
<span class="price"><?php property_price(); ?></span>
</li>
<?
}
}
}
?>
<?php
endwhile;
wp_reset_query();
?>
</ul>
</div>
</div>
I might be misunderstanding your setup, but I wonder why you're looping over the terms.
I think you should instead consider excluding the leased term within the WP_Query() part (hopefully you can share it).
Then your carousel would be simplified to:
<div class="carousel es-carousel-wrapper">
<div class="es-carousel">
<ul class="clearfix">
<?php while ( $featured_properties_query->have_posts() ) : $featured_properties_query->the_post(); ?>
<li><!-- YOUR POST ITEM HERE --></li>
<?php endwhile; ?>
</ul>
</div>
</div>
You can add the post ID to an array every time an iteration occurs, and check the array if the post has already been rendered:
$shown = array(); // new array
while ( $featured_properties_query->have_posts() ) :
$featured_properties_query->the_post();
$status_terms = get_the_terms( $post->ID, 'property-status' );
if( ! empty( $status_terms ) ){
foreach( $status_terms as $status_term ){
if( $status_term->name == "Leased" || in_array( $post->ID, $shown ){
continue; // post has term "Leased" or already rendered, skip
}
$shown[] = $post->ID; // add post ID to array
?>
<!-- HTML here -->
<?php
}
}
endwhile;
I have two different custom post types: "Movies" and "Press" with different custom fields.
Now I want to display both custom fields in the single.php.
home.php:
...
$query = array(
'post_type' => 'movies',
'posts_per_page' => 3,
);
$loop = new WP_Query( $query );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="movie_cover">
<img src="<?php the_field('cover'); ?>" title="<?php echo get_the_title() ?>" />
</div>
...
single.php:
get_header(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'single', 'film' ); ?>
<?php endwhile; // end of the loop. ?>
<?php get_footer(); ?>
single-film.php:
...
<img src="<?php the_field('cover'); ?>" title="<?php echo get_the_title() ?>" />
...
the field 'cover' is from "Movies", but now i want to list the field 'release' from "Press".
Does I need a second loop?
if you add movie_id custom field to press, and set it to the ID of the movie
then you can do something like inside single-film.php
query_posts('meta_key=movie_id&meta_value=' . get_the_ID());
and do another loop for the reviews
while have_posts() ...
There's a lot of additional stuff but this should get you going in the
right direction.
I'd consider using get_posts instead of query_posts to not override wordpress' main query loop, but then you'd have to do your own manual loop:
$posts = get_posts('meta_key=movie_id&meta_value=' . get_the_ID());
foreach ($posts as $p) {
echo $p->post_title;
...
}
On the Wordpress site I'm working on, it lists posts by category, but I am also after a page that lists ALL the posts (with pagination, showing 10 per page). How would I go about achieving this?
Thanks
You could create a new page template with this loop in it:
<?php
$paged = get_query_var('paged')? get_query_var('paged') : 1;
$args = [
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $paged,
];
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ?></h2>
<?php endwhile; ?>
<!-- then the pagination links -->
<?php next_posts_link( '← Older posts', $wp_query ->max_num_pages); ?>
<?php previous_posts_link( 'Newer posts →' ); ?>
For others who might be Googling this... If you have replaced the front page of your site with a static page, but still want your list of posts to appear under a separate link, you need to:
Create an empty page (and specify any URL/slug you like)
Under Settings > Reading, choose this new page as your "Posts page"
Now when you click the link to this page in your menu, it should list all your recent posts (no messing with code required).
A bit more fancy solution based on #Gavins answer
<?php
/*
Template Name: List-all-chronological
*/
function trimStringIfTooLong($s) {
$maxLength = 60;
if (strlen($s) > $maxLength) {
echo substr($s, 0, $maxLength - 5) . ' ...';
} else {
echo $s;
}
}
?>
<ul>
<?php
$query = array( 'posts_per_page' => -1, 'order' => 'ASC' );
$wp_query = new WP_Query($query);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute() ?>">
<?php the_time( 'Y-m-d' ) ?>
<?php trimStringIfTooLong(get_the_title()); ?>
</a>
</li>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts published so far.'); ?></p>
<?php endif; ?>
</ul>