Shortcode issue - wordpress

I am currently creating a shortcode in order to display custom taxonomy terms as a list in my template :
// 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'
);
I run in the 2 following issues :
The shortcode (render) is displayed at the top of my page, not where I've placed it in the page;
PHP Console flag the 2 followings errores :
Use of undefined constant taxonomy - assumed 'taxonomy'
Use of undefined constant title_li - assumed 'title_li'
Any help appreciated!
Thanks

Firstly the output of your shortcode is displaying at the top of your page because you're echoing the output. You should create a $output variable and build it up with what you want to display, and then return it. For example:
$output = '';
$output .= '<ul>';
$output .= wp_list_categories($args);
$output .= '</ul>';
return $output;
Secondly you're getting the errors because you've not quoted the keys in your array declaration. Therefore PHP assumes they should be constants that were previously defined.
$args = array(
taxonomy => $custom_taxonomy,
title_li => ''
);
Should be:
$args = array(
'taxonomy' => $custom_taxonomy,
'title_li' => ''
);

Related

Get all values of a ACF field in all posts and the links to the posts

I have created an ACF field where I can add 1 keyword per post. I want to get now a list of all keywords set in all my posts and sort it by alphabet and add a link to the post where it was found. Every keyword will be unique. So it would be a kind of table of content. How can I do that programatically? I have no idea right now on how to loop through posts and get field values.
Place the following function in functions.php . Use it directly in your template or as shortcode or w/e
function keywords_post_list() {
//We build our query for posts containing any value in meta field
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'keyword', //change with your meta key
'value' => '',
'compare' => '!='
)
);
$query = new WP_Query($args);
global $post;
$items = array();
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
//Looping each post we collect the keyword and the link for a post.
//Grab any other information if you need and add in the array
$keyword = get_post_meta($post->ID,'keyword',true);
$link = get_the_permalink($post->ID);
//Our array
$items[] = array('keyword'=> $keyword,'link' => $link);
endwhile;
endif;
// We need to sort results by keyword currently ASC
array_multisort(array_column($items, 'keyword'), $items);
// If we need to sort DESC uncommnet bellow coment above
// array_multisort(array_column($items, 'keyword'),SORT_DESC, $items);
// error_log(print_r($items,true));
if($items):
echo '<ol class="keyword-list">';
foreach($items as $item):
$keyword = $item['keyword'];
$link = $item['link'];
echo '<li>'.$keyword.'</li>';
endforeach;
echo '</ol>';
endif;
}

How to create a filter to display custom post types

I have a custom post type 'projects' and have an overview page that displays these post types with featured image & post title. I have also created a custom taxonomy for that Post Type and assigned the posts to the categories from that taxonomy.
What I want to achieve now is that on the Overview Page where all the posts are listed, above them should be something like a filter bar with the custom taxonomy categories displayed.
My question now is: What WordPress functions do I need so that when someone clicks on one of the categories, only the posts assigned to that category will be displayed? I don't want the page to refresh or load another page. Here is an example of what I want to achieve: https://www.hauserlacour.de/en/work
Also, I am not a coder. I use Pinegrow to convert my static html sites to a wordpress theme. but in Pinegrow I have the option of a lot of WP function. That's why I just need to understand how the set up of something like above would work.
Many thanks in advance!
If you know a bit more about the the WP_Query you can use the tax_query as below:
$args = array(
'post_type' => 'project',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => array( 'project_cat' ), // <-- NO! Does not work.
'field' => 'slug',
'terms' => array( 'project_cat1', 'project_cat2')
)
)
);
$query = new WP_Query( $args );
Reference: https://developer.wordpress.org/reference/classes/wp_tax_query/
or you can simply list the taxonomy terms and simply redirect to taxonomy detail page where respective projects will be listed automatically.
$args = array( 'hide_empty=0' );
$terms = get_terms( 'my_term', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$count = count( $terms );
$i = 0;
$term_list = '<p class="my_term-archive">';
foreach ( $terms as $term ) {
$i++;
$term_list .= '' . $term->name . '';
if ( $count != $i ) {
$term_list .= ' ยท ';
}
else {
$term_list .= '</p>';
}
}
echo $term_list;
}
Reference: https://developer.wordpress.org/reference/functions/get_terms/

Why wc_get_template_part() not working inside wc_get_products()?

Cant find full loop example on the web with wc_get_template_part() and wc_get_products(), so looking for help here:
global $woocommerce;
global $product;
$args = array(
'limit' => 15,
'category' => array('printers', 'laptop')
);
$query_cats = wc_get_products($args);
foreach ($query_cats as $query_cat) {
echo $query_cat->get_id();
echo $query_cat->get_title();
// echo "<pre>";
// var_dump($query_cat);
wc_get_template_part('content', 'product');
}
?>
Titles and ids are displayed, var_dump also, bu wc_get_template_part - no. I have add_theme_support('woocommerce'); and also body_class();
WooCommerce content-product.php template only works only with standard loop(with instance of Wp_Query). May be following solution can help:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => [
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => ['printers', 'laptop'],
)
],
);
$product = new WP_Query( $args );
while ( $product->have_posts() ) {
$product->the_post();
wc_get_template_part( 'content', 'product' );
}
wp_reset_postdata();
Thanks
Yes, it can be done (2022 update)
If you want to avoid native WP_Query or using shortcodes, this actually can be done using wc_get_products().
You just need to setup and reset postdata within your foreach loop and setup WooCommerce loop properly.
global $post; // Do not forget this!
$args = array(
'limit' => 15,
'category' => array('printers', 'laptop')
);
$products = wc_get_products( $args );
// Set loop properties
wc_set_loop_prop('columns', 5);
// Start custom WC loop
woocommerce_product_loop_start();
foreach( $products as $product ) {
// Setup postdata
$post = get_post( $product->get_id() );
setup_postdata( $post );
// Get template part
wc_get_template_part( 'content', 'product' );
}
// End loop and reset postdata
woocommerce_product_loop_end();
wp_reset_postdata();
Important note: this will only work if queried products are within any HTML element with the woocommerce class (otherwise WooCommerce CSS won't load for your products). In some templates, the woocommerce class is already part of the DOM (e.g. <body> element), but if it isn't, wrap your loop within an element as such:
<div class="woocommerce my-products-loop">
<?php // Your loop goes here ?>
</div>
TIP: You can set various loop properties using wc_set_loop_prop in the "Set loop properties" part of the code

ACF Field Wordpress - Custom Shortcode Problems

I have a small problem
I wrote a simple shortcode function to display my acf value in the grid of the visual grid composer, this is my simple custom shortcode
function my_module_add_grid_shortcodes( $shortcodes ) {
$shortcodes['vc_say_hello'] = array(
'name' => __( 'Say Hello', 'my-text-domain' ),
'base' => 'vc_say_hello',
'category' => __( 'Content', 'my-text-domain' ),
'description' => __( 'Just outputs Hello World', 'my-text-domain' ),
'post_type' => Vc_Grid_Item_Editor::postType(),
);
return $shortcodes;
}
add_shortcode( 'vc_say_hello', 'vc_say_hello_render' );
function vc_say_hello_render() {
if( get_field('item') ):
the_field('item');
else:
echo "<h2>ITEM LOCKED</h2>";
endif;
}
When I call the shortcode in the builder, "ITEM Locked" is displayed, even if the value of the element is set in the post !!!
Looks like get_field doesn't know where to get it from in the shortcode. Try adding the current post id as the second parameter
add_shortcode( 'vc_say_hello', 'vc_say_hello_render' );
function vc_say_hello_render() {
$id = get_the_ID();
if( get_field('item', $id) ):
the_field('item', $id);
else:
echo "<h2>ITEM LOCKED</h2>";
endif;
}

Advanced custom fields - relationships logic reversed

On our site we currently have 'work' posts that we create and then associate with an author and genre post type.
The overall goal is:
When we view an author or genre post we want to list all the work posts that we have associated/related with that certain author/genre.
We are using the following code which seems to have us half way...
<?php $args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'related_posts',
'value' => $post->id
)
)
);
$posts_array = get_posts( $args );
var_dump($posts_array);
if( $posts_array ) {
echo '<ul>';
foreach( $posts_array as $related ) {
echo '<li>';
echo '' . $related->post_title . '';
echo '</li>';
}
echo '</ul>';
}
?>
However the 'value' field in the array doesn't work. It technically should be passing the id of the current post (author or genre) and selecting the related content. When we remove this from the array it does bring all the posts in whether they are related or not.
In summary, we think that the 'value' problem may be the key in resolving the issue as that is what should be filtering the posts.
Thanks in advance
did you try $post->ID? (uppercase not lowercase)

Resources