Display Post Terms and add link - wordpress

I've created this shortcode to display my terms (custom taxonomy) on specific post (custom post types) :
// First we create a function
function list_terms_forme_juridique_taxonomy() {
global $post;
$terms = wp_get_post_terms( $post->ID, 'forme_juridique',array('fields'
=> 'names') );
ob_start();
if( count( $terms) > 0) {
echo '<ul>';
echo '<li>' . implode( '</li><li>', $terms) . '</li>';
echo '</ul>';
}
return ob_get_clean();
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);
I am trying to add a link (url) on my terms in order to redirect to the term page but I didn't succeed yet.
Any help ?

Use get_term_link()function for this:
// First we create a function
function list_terms_forme_juridique_taxonomy() {
global $post;
$terms = wp_get_post_terms($post->ID, 'forme_juridique');
ob_start();
if( count( $terms) > 0) {
echo '<ul>';
foreach($terms as $term){
$term_link = get_term_link($term, 'forme_juridique');
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
return ob_get_clean();
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);

Related

Hide certain acf fields wordpress

Help
Code in template:
<?php
$groupID = '';
$fields = get_fields($groupID);
$fields = get_field_objects();
if( $fields )
{
foreach( $fields as $field_name => $field )
{
if( $field['value'] )
{
echo '<ul>';
echo '<li>' . $field['label'] . ': <strong>' . $field['value'] . '</strong></li>';
echo '</ul>';
}
}
}
?>
I need to hide the fields:
  field_5c0a8d44cf56e
  field_5c0a8d4ecf56f
How can i do this?
Your question is not much clear for me, and from my knowledge what I have understood,
for an acf group you don't want to loop through, the control is yours,
so you can print it straightaway
but if you really want to do it in a loop then,
if( $fields )
{
foreach( $fields as $field_name => $value )
{
if( $value && !in_array($field_name, ["field_5c0a8d44cf56e", "field_5c0a8d4ecf56f"])
{
echo '<ul>';
echo '<li>' . $field_name . ': <strong>' . $value . '</strong></li>';
echo '</ul>';
}
}
}

how to customize wordpress shortcode function to get every category post by shortcode Variable?

I'm using following code to create a WordPress shortcode [educationposts] to show posts of category named "education".
How to set this shortcode function to be flexible as can show any category by changing shortcode like this [posts=education] or [posts=blog]
Thanks
/*** show post category ***/
function pwp_postsbycategory() {
$the_query = new WP_Query( array( 'category_name' => 'education', 'posts_per_page' => 5 ) );
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<li>';
$string .= '' . get_the_post_thumbnail($post_id, array( 75, 75) ) . get_the_title() .'</li>';
} else {
// if no featured image is found
$string .= '<li>' . get_the_title() .'</li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('educationposts', 'pwp_postsbycategory');
/*** show post category ***/
I solved it by set an $atts in function.
/*** show post category ***/
function pwp_postsbycategory($atts) {
$the_query = new WP_Query( array( 'category_name' => $atts['cat'], 'posts_per_page' => 5 ) );
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<li>';
$string .= '' . get_the_post_thumbnail($post_id, array( 75, 75) ) . get_the_title() .'</li>';
} else {
// if no featured image is found
$string .= '<li>' . get_the_title() .'</li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('posts', 'pwp_postsbycategory');
/*** show post category ***/

Display Custom Taxonomy by Shortcode

I need to create a function that allows me to display a taxonomy via shortcode.
I try this but doesn't work:
function category_in_content($atts){
global $post;
return get_the_terms( $post, 'course_category' );
}
add_shortcode( 'catcorso', 'category_in_content' );
The taxonomy name is "course_category".
By default add_shortcode return html.
You can try the following code:
add_shortcode( 'catcorso', 'category_in_content' );
function category_in_content($atts){
global $post;
$html = '';
$taxonomy = 'course_category';
$terms = get_the_terms( $post, $taxonomy );
if ( !empty( $terms ) ) {
foreach ($terms as $term) {
$html .= '' . $term->name . '';
}
}
return $html;
}
solved with this:
function cat_title(){
global $post;
$categories = get_the_terms( $post, 'course_category' );
if ( isset( $categories[0] ) ) {
return '' . esc_html( $categories[0]->name ) . '';
}
}
add_shortcode( 'catcorso', 'cat_title' );

I need to add menu in wordpress using woo commerce product category?

Here I am created a code for menu.It gives an menu but cann't add product category. Help will be appreciated.thank you.
// Filter wp_nav_menu() to add additional links and other output function new_nav_menu_items($items) {
$homelink = '<li class="home">' . __('Home') . '</li>';
// add the home link to the end of the menu
$items = $items . $homelink;
return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
First you need to create menu in word press admin menu blank menu.
Now go to function.php file (theme file) add following code in it.
You can get products cateogorty list from this function,
function get_product_terms( $term_id ) {
$html = '';
$args = array( 'hide_empty' => 0, 'parent' => $term_id );
$terms = get_terms('product_cat', $args);
foreach ($terms as $term) {
$html .= '<li';
if( $term_id == 0 ) {
$html .= ' class="top_li"';
}
$html .= '>' . $term->name . '';
if( $list = get_product_terms( $term->term_id )) {
$html .= '<ul class="second_level">'.$list.'</ul>';
}
$html .= '</li>';
}
return $html;
}
You can add products category to menu using this function,
// Filter wp_nav_menu() to add additional links and other output
function new_nav_menu_items($items) {
// Woo function
/*//product_cat
$terms = get_terms( 'product_cat', $args );
print_r($terms);*/
if( $list = get_product_terms( 0 )) {
$menu1link = '<li class="home">' . __($list) . '</li>';
$homelink = '<li class="home">' . __('Home') . '</li>';
// add the home link to the end of the menu
$items = $items . $homelink;
$items = $items .$menu1link;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );

List custom taxonomies children links

I was wondering if you could help me display a taxonomies child links, for example, I have a custom post type of "courses" with a custom taxonomy of "study_type" and categories like "security", "professional" etc. Currently I am correctly displaying the category title and description. I need to know how to get a permalink for the courses that fall into the categories and display them underneath the category description. Thanks for any help or advice you have to offer. Here is me code:
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$taxonomy = $type;
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
}
}
}
?>
use get_term_children
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$terms = get_terms( $type, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
$termchildren = get_term_children( $term->term_id, $type );
if($termchildren){
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
}
}
} ?>

Resources