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>';
}
}
}
Related
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>';
}
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'
);
Hello friends i want so image and description in categories list widget for showing description i use below code:
in function.php
function wpb_catlist_desc() {
$string = '<ul>';
$catlist = get_terms( 'category' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $key => $item ) {
$string .= '<li>'. $item->name . '<br />';
$string .= '<em>'. $item->description . '</em> </li>';
}
}
$string .= '</ul>';
return $string;
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
and in class-wp-widget-categories.php
echo do_shortcode('[wpb_categories]');
it is showing categories name and his description now i want to show categories image also. any one please help me how can i get that. i had try Categories Images plugin for show images but can't able to show
Image get z_taxonomy_image_url($id) and z_taxonomy_image($id)
if(z_taxonomy_image_url($item->term_id))
{
$string .= '<img src ="'.z_taxonomy_image_url($item->term_id).'" style="width: 20%;">';
}
You can get the image using following code and append it to $string.
$catlist = get_terms( 'category' ); // get category list
$string ="<ul>";
foreach ( $catlist as $key => $item )
{
$thumbnail_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
$image_dis='<img src="' . $image . '" alt="' . $cat->name . '" />';
}
$string .= '<li>'. $item->name . '<br />';
$string .= $image_dis . '<br />';
$string .= '<em>'. $item->description . '</em> </li>';
}
$string .="</ul>";
echo $string;
My Wordpress site use first image as post thumbnail, code:
add_filter('the_content','replace_content');
function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if(empty($first_img)) {
$first_img = "/path/to/default.png";
}
return $first_img;
}
Some posts have no image in its content, so i want to use different default thumbnails for them: posts in category A use picture1, posts in cagory B use category2...
How can I do this?
thank you
You need to get the current post category first, then, make a if statement in your code.
Check this link : https://developer.wordpress.org/reference/functions/get_the_category/#comment-305
How about this, here we are using has_post_thumbnail to check for image attachments, if none exist we are setting up the image and image source. From there we check for category assignment, if there are no category matches we use a default thumbnail.
<?php
if ( ! has_post_thumbnail() ) {
$themefolder = get_bloginfo('template_directory');
echo '<img src="' . $themefolder . '/images/';
if ( is_category( 'Category A' ) ) {
echo 'no-image-a.png';
} elseif ( is_category( 'Category B' ) ) {
echo 'no-image-b.png';
} else {
echo 'no-image.png';
}
echo '" alt="' . get_the_title() . '">' . PHP_EOL;
}
?>
Finally, It's my code:
add_filter('the_content','replace_content');
function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if(empty($first_img)) {
$categories = get_the_category();
if ( ! empty( $categories ) ) {
foreach ( $categories as $category ) {
if( $category->name == 'Category A' ){ $first_img = "/path/to/default1.png";}
elseif ( $category->name == 'Category B' ){ $first_img = "/path/to/default2.png";}
else {$first_img = "/path/to/default3.png";}
}
}
}
return $first_img;
}
For ACF form plugin, I used field group function which contains 10 fields, and loop to display in post like below:
<?php $fields = get_field_objects($post_id);
if( $fields)
{
foreach( $fields as $field_name => $field )
{
if($field['type']=='text' || $field['type']=='textarea' ){
echo '<li>';
echo '<label>' . $field['label'] . '</label>';
echo '<span>' . $field['value'] . '</span>';
echo '<span>' . $field['order_no'] . '</span>';
echo '</li>';
}
}
}?>
Now I want to sort it by order_no, how can I do it?
You could try something like that before you print out:
function objectSort( $a, $b ) {
return $a->order_no == $b->order_no ? 0 : ( $a->order_no > $b->order_no ) ? 1 : -1;
}
usort( $fields, 'objectSort' );
Note: This is not meant to be a full correct code, I just want to give you an idea for the solution.