I like to list a specific WooCommerce attribute values as list items like this:
<ul>
<li>Attribute value 1<li>
<li>Attribute value 2<li>
</ul>
I have this code:
global $product;
if ( $product ) {
$attributes = array( 'gyogynovenyek' );
$output = array();
foreach ( $attributes as $attribute ) {
$taxonomy = 'pa_' . $attribute;
$values = $product->get_attribute($taxonomy);
if ( ! empty($values) ) {
$output = '<li>' . $values . '</li>';
}
}
echo $output;
}
the problem, it lists the values like this:
<li>Attribute value 1, Attribute value 2</li>
How should I modify the code to list values separately? Thanks for your answers!
Okay, I find the answer:
$terms = get_terms( 'pa_gyogynovenyek' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
Related
I'm trying to use a side menu to navigate when a page has child pages.
What I have currently is nearly perfect, but instead of using the titles of the child pages in the menu, I want to use a custom field 'sidebar_title'.
I'm currently running this function that I found:
function wpb_list_child_pages() {
global $post;
if ( is_page() && $post->post_parent )
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
else
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
if ( $childpages ) {
$string = '
<nav class="sidenav">
<ul>
<li>'.get_the_title($post->post_parent).'</li>'
.$childpages.
'</ul>
</nav>';
}
return $string;
}
This gives me this result:
<nav class="sidenav">
<ul>
<li>Parent Page</li>
<li>Child Page</li>
<li>Child Page</li>
</ul>
</nav>
I just need to know how I can replace the title of the child pages with my custom field.
You need to use get_pages function so you can have control over the layout. the function you are using now is wp_list_pages which is based on get_pages so you do not need to change anything in your main request. So your full code will look like this:
$childpages = get_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
if ( $childpages ) {
$string = '<nav class="sidenav"><ul><li>'.get_the_title($post->post_parent).'</li>'
foreach( $childpages as $page ) {
$string .= '<li>' . get_post_meta($page->ID, 'sidebar_title', true) . '</li>';
}
$string .= '</ul></nav>';
return $string;
}
This appears to have done the trick.
function wpb_list_child_pages() {
global $post;
if ( is_page() && $post->post_parent )
$childpages = get_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
else
$childpages = get_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
if ( $childpages ) {
$string = '<nav class="sidenav"><ul><li>'.get_field(sidebar_title, ($post->post_parent)).'</li>';
foreach( $childpages as $page ) {
$string .= '<li>' . get_post_meta($page->ID, 'sidebar_title', true) . '</li>';
}
$string .= '</ul></nav>';
return $string;
}}
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>';
}
}
}
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 have this code in my WP theme which shows categories in breadcrumbs of products.
The problem is when product has more than 5 categories - it looks ugly.
My question is: How to show ONLY 5 categories?
Thanks!
<?php
$term_list = '';
$j=0;
foreach ($terms as $term) {
if($term->parent==0){
$j++;
if( $j <= 1 ){
$term_list .= '' . $term->name . '';
}
}
}
if(strlen($term_list) > 0){
$size = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
echo $product->get_categories( ', ', '<div class="nav-back">' . _n( 'Back to','Back to', $size, 'tdl_framework' ) . '', '</div>' );
};
?>
Add
$terms = array_slice( $terms, 0, 5 );
just before the foreach to trim it to 5 elements max.
I have a custom post type in which I have custom taxonomies setup.
I want to print out the categories(custom taxonomy) that a post is included in, but exclude one. I cannot find a solution to exclude the category though.
Here is my code to output a list of the categories the custom post type is filed under:
<?php the_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ' ); ?>
How do I exclude a specific category?
Thanks.
You could create a function in your functions.php file that calls get_the_terms to return the list of terms as an array and then remove the item you don't want.
Give this a try:
function get_excluded_terms( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$excluded_terms[] = '' . $term->name . '';
}
}
$excluded_terms = apply_filters( "term_links-$taxonomy", $excluded_terms );
return $before . join( $sep, $excluded_terms ) . $after;
}
and then use it like this:
<?php echo get_excluded_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ', array(667)); ?>