How to get taxonomy in a div - wordpress

I want to add a taxonomy in my div with the different other fields, but I'm new to wordpress and O don't find how doing this, if someone can help me please ?
<?php/* Template Name: Archive Projets */ ?>
<?php get_header(); ?>
<?php
$posts = get_posts(array(
'posts_per_page' => 4,
'post_type' => 'projects'
));
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<div>
<?php the_title(); ?>
<div><?php the_field('url')?></div>
<div><?php the_field('')?></div> /* HERE I WANT TAXONOMY */
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Here's a code that will get the taxonomies of the current post and display them in a list of insert this code under <div><?php the_field('url')?></div>
// Get all terms
$terms = get_the_terms( $post->ID , array( 'Taxonomy_name') );
// Dispaly the taxonomies, if there one or more.
foreach ( $terms as $term ) {
echo '<div>' . $term->name . '</div></br>';
}
Make sure to change the 'Taxonomy_name' with your taxonomy name. let me know if I got you wrong or if you need any help with the code.

A quick google search brings up get_terms().
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
foreach($terms as $current_term) {
//You can now loop through them and get the ones you want, display them all, or whatever else it is you want to do. Note: each $current_term is an object of type WP_Term (or error if there are no results).
}

Related

Query one category name from multiple post type

I've got a problem with a query. I've got 4 different post_type.
I would like to retrieve the posts from all post type from one category named 'une'. But it shows only post type from two post type => spectacles and post.
Here is my code :
<?php
query_posts(array(
'post_type' => array('post','videos','photos','spectacles'),
'showposts' => -1,
'category_name' => 'une',
)
); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; else: ?>
<?php endif; ?>
It is like category_name doesn't work for my query.
I found the solution to my problem, i add this to the functions.php
function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'nav_menu_item', 'spectacles','historique','photos','videos'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

Returning the name of a category in Wordpress

I have a loop for a custom post type an all is working well, except I cannot return the actual category name that the post has assigned to it.
I have the following code:
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC'
));
if($posts) {
foreach($posts as $post) {
setup_postdata( $post );
?>
<div>
<div class="mix <?php get_cat_name( ); ?>" ><img src="<?php the_field('portfolio_image'); ?>" alt=""></div> <!-- ACF -->
</div>
<?php
}
}
wp_reset_postdata();
?>
This is just one of the WP Codex functions I have tried including tring all with echo. I imagine my problem is something else?
Many thanks in Advance.
According to the codex an id is required for this method (https://codex.wordpress.org/Function_Reference/get_cat_name). Leaving out the id will not work.
One solution would be to use get_the_category($postId) to retrieve the category for your post (https://codex.wordpress.org/Function_Reference/get_the_category). You can leave out the postId here
post_id
(integer) (optional) The post id.
Default: $post->ID (The current post ID)
Note that the method returns an object - not the category name. You can try the following code to see if it's working:
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
try this code
<?php $terms = get_the_terms($post->ID, 'TAXONOMY' );
if ($terms && ! is_wp_error($terms)) :
$term_slugs_arr = array();
foreach ($terms as $term) {
$term_slugs_arr[] = $term->name;
}
$terms_slug_str = join( " ", $term_slugs_arr);
endif;
echo $terms_slug_str;?>
Replace your code
from
get_cat_name( );
to
$categories = get_the_terms($post->ID,'custom-texonomy-name');
$cat = key($categories);
echo $categories[$cat]->name;

wordpress and future posts

I need to show future posts in all posts list on page.
my code
$args = array('posts_per_page' => 10,
'category' => $category->cat_ID );
?>
<?php $posts = get_posts($args); ?>
what arguments I can to write for showing all posts from category with future posts?
And it my archive page code, what should to show posts list from category
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>
this solution mast to show future and past posts too
You should specify the post_status to be publish and future in your $args, for example:
$args = array(
'posts_per_page' => 10,
'category' => $category->cat_ID,
'post_status' => array(
'publish',
'future'
)
);
To get the posts with these args, use (for example):
$posts = get_posts($args);
To display these posts, use (for example):
foreach( $posts as $p ) {
// display the post title
echo '<h2>' . apply_filters('the_title', $p->post_title) . '</h2>';
// display the post date
echo '<h4>' . date('F jS, Y', strtotime($p->post_date)) . '</h4>';
// display an excerpt of the post
echo wp_trim_excerpt($p->post_content);
}
An alternative is to use query_posts / WP_Query to get these posts. In this case, to display the posts, you would use a loop, similar to the one in your question. Such example can be seen here: http://codex.wordpress.org/Class_Reference/WP_Query#Standard_Loop

How to show Previous and Next link in WordPress

Below is my example code that is dynamically get the posts.
<?php
global $post;
$args = array( 'numberposts' => 4 );
$the_posts = get_posts( $args );
foreach( $the_posts as $post ){ ?>
//The Post Content Goes here...
?>
The code above will works correctly but my question is, since this is not a default blog page or a category, how can I use the posts_nav_link() so that I can still access the rest of the pages? I tried to used it but it doesn't work unless if the current page is a category. Hope you guys can help me this.
If you giving you paging in your custom post type. then i think you can do very simple you have to use wordpress plugin like wp-pagenavi after then add your custom post type in this plugin in admin panel after then add
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php
global $post;
$args = array( 'numberposts' => 4 );
$the_posts = get_posts( $args );
foreach( $the_posts as $post ){ ?>
//The Post Content Goes here...
?>
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
Without plugin you can use like this
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 3, 'cat' => '-10, -72&paged=' . $paged) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail(); ?> <?php the_title(); ?>
<span><?php the_time('d.m.y') ?></span>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php posts_nav_link(' — ', __('« Previous Page'), __('Next Page »')); ?>

Making custom WooCommerce loop

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');

Resources