Taxonomy - Display terms for post - wordpress

I've created the custom shortcode to display terms of a custom taxonomy.
// First we create a function
function list_terms_forme_juridique_taxonomy( $atts ) {
// Inside the function we extract custom taxonomy parameter of our
shortcode
extract( shortcode_atts( array(
'custom_taxonomy' => 'forme_juridique',
),
$atts ) );
// arguments for function wp_list_categories
$args = array(
taxonomy => $custom_taxonomy,
title_li => ''
);
// We wrap it in unordered list
echo '<ul>';
echo wp_list_categories($args);
echo '</ul>';
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);
It works well, but the issue is that ALL the terms are displayed. I'd like to display only the terms corresponding to the post itself.
Any help ?

Maybe get_the_terms() will work for you - it returns the terms of the custom taxonomy that are attached to the post (you must have the ID of the current post).

Related

Display CPT list page (archive) with category term on slug

I have the CPT (Custom Post Type) "news".
In the "category" taxonomy I have "food" and "health".
The archive page is accessed via the url:
www.mysite.com/news
Is there any way to show "news" by categories? Ex:
www.mysite.com/food/news
www.mysite.com/health/news
Thank you all in advance.
In that link I found the following solution:
The solution for me had three parts. In my case the post type is called trainings.
Add 'rewrite' => array('slug' => 'trainings/%cat%') to the
register_post_type function.
Change the slug to have a dynamic
category. "Listen" to the new dynamic URL and load the appropriate template.
So here is how to change the permalink dynamically for a given post type. Add to functions.php:
function vx_soon_training_post_link( $post_link, $id = 0 ) {
$post = get_post( $id );
if ( is_object( $post ) ) {
$terms = wp_get_object_terms( $post->ID, 'training_cat' );
if ( $terms ) {
return str_replace( '%cat%', $terms[0]->slug, $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'vx_soon_training_post_link', 1, 3 );
...and this is how to load the appropriate template on the new dynamic URL. Add to functions.php:
function archive_rewrite_rules() {
add_rewrite_rule(
'^training/(.*)/(.*)/?$',
'index.php?post_type=trainings&name=$matches[2]',
'top'
);
//flush_rewrite_rules(); // use only once
}
add_action( 'init', 'archive_rewrite_rules' );
Thats it! Remember to refresh the permalinks by saving the permalinks again in de backend. Or use the flush_rewrite_rules() function.
But I have a question that I couldn't do there:
This part:
Add 'rewrite' => array('slug' => 'trainings/%cat%') to the register_post_type function.
where do I do that? Or is this already embedded in the later code?

how to create shortcode using post content for each post when the post is published

I want to create short code every time when post is published using that specific post content so that i have short codes for each different post, for this i wrote this code, but its not working, can anyone please help.
add_action('publish_adsmanager_banner','create_banner_shortcode');
function create_banner_shortcode()
{
add_shortcode( 'banner_'.get_the_ID().'', 'custom_shortcode' );
}
function custom_shortcode($post_id) {
$message = get_the_content($post_id);
return $message;
}
What if you will just pass an id as an attribute to this shortcode?
add_shortcode( 'my_banner', 'custom_shortcode' ); // [my_banner id="123"]
function custom_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'id' => null,
),
$atts
);
$message = get_the_content( $atts['id'] );
return $message;
}
In your original idea, you cannot create an id-named shortcode just once, it should be registered on each wp init. But in this case, it is hard to get a post context just from the shortcode name, you have to pass an id on each shortcode registration. The solution above will allow you to keep DRY especially if you know the ID of each banner when using them.

Is it possible to call a custom field from ACF or Pods into a function?

I am fluent in HTML and CSS but not so much in PHP. Any help would be appreciated.
I have a custom function in Wordpress as seen below:
function prefix_calculation_global_calculation_vars( $vars ) {
return array(
'setup_cost' => 200,
);
}
add_filter( 'pewc_calculation_global_calculation_vars', 'prefix_calculation_global_calculation_vars' );
Is it possible to call an Advanced Custom Fields (ACF) field or a Pods field and have the value inserted into the function above where the 200 is?
Providing get_the_ID() works you could try this:
function prefix_calculation_global_calculation_vars( $vars ) {
return array(
'setup_cost' => get_field('setup_cost', get_the_ID()),
);
}
add_filter( 'pewc_calculation_global_calculation_vars', 'prefix_calculation_global_calculation_vars' );
You would have to grab it via the $wpdb->postmeta table
<?php echo get_post_meta(get_the_ID(), 'setup_cost', TRUE); ?>

Wordpress - Load posts by ID returns NULL

I'm using a page template with a $_GET variable to return information for a single post that's part of a custom post type but it doesn't return anything. Any ideas?
<?php
$args = array(
'p' => $_GET['funeralID'], // id of a page, post, or custom type
'post_type' => 'post'
);
$video_post = new WP_Query($args);
while ( $video_post->have_posts() ) : $video_post->the_post(); ?>
<?php the_field('archive_video_link'); ?>
I would add the variable to the WordPress' array of 'recognized query variables'...
add_action('init','add_my_vars');
function add_my_vars() {
global $wp;
$wp->add_query_var('funeralID');
}
Then the value of 'funeralID' can be found via get_query_var('funeralID'); See Codex

Add & Separate Custom Post Types in Taxonomy.php / Taxonomy Page

I have a taxonomy.php file to display taxonomy terms. I added a filter in functions.php to include post types for the taxonomy page query. This filter:
add_filter( 'pre_get_posts' , 'ucc_include_custom_post_types' );
function ucc_include_custom_post_types( $query ) {
global $wp_query;
/* Don't break admin or preview pages. */
if ( !is_preview() && !is_admin() && !is_page() && !is_single() ) {
$args = array(
'public' => true ,
'_builtin' => false
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types( $args , $output , $operator );
$post_types = array_merge( $post_types , array( 'post' ) );
if ($query->is_feed) {
// Do feed processing here.
} else {
$my_post_type = get_query_var( 'post_type' );
if ( empty( $my_post_type ) )
$query->set( 'post_type' , $post_types );
}
}
return $query;
}
Returns any and all post types you want. But I am trying to find a way to separate them. I tried to use a normal loop but I don't know how to fetch the current taxonomy tag from the page.
I have 2 questions which are all related but seeing what is the best way to go about this. Pretend I have 3 posts types ('post' 'post2' 'post3')
Is there a loop that can be used in taxonomy.php that will display a particular post type? So it can be possible to have one loop for each post type? So when I click on a taxonomy term, the taxonomy.php will return:
--Taxonomy Page --
Loop for custom type post 1 (show post with current taxonomy tag in this specific post type)
Loop for custom type post 2
Loop for custom type post 3
If there are multiple loops, will this affect the pagination? Or will pagination only work for posts?
I have used many single loops in the taxonomy.php page to no avail. I feel I have to echo the current taxonomy term variable to a new variable:
$term = $wp_taxonomies??
Any way for multiple loops in the taxonomy.php pages?
Probably the easiest way to do this is ignore the existing $wp_query and create three new queries in your taxonomy template. So don't start "The Loop" in your template, just create a new query and loop with that one. Repeat this for the other post types. This also means you don't need to hook into the pre_get_posts filter, you create a custom query just for you.
You will have to think about the UI for the next pages, indeed. This depends on the reason you want the post types separated. If it is enough that you see the three together on the first page, you could go with three separate "next page" links, so one per post type.

Resources