How to show image url in wordpress foreeach loop? - wordpress

I need to display sub categories from a particular category
<?php
$subcategories = get_categories('&child_of=22&hide_empty');
foreach ($subcategories as $subcategory) {
echo '<a href="' . get_category_link( $subcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $subcategory->name ) . '" ' . '>' . $subcategory->name.'</a>';
echo '<p>'.$subcategory->description.'</p>';
}
?>
I tried this
echo '<img class='hline' src='bloginfo('template_url')/img/hline.png' alt=''>';
its not working

You need to look at the basics of strings and concatenation in PHP.
You open your string with single quotes so you can't then use single quotes for HTML attributes unless you escape them first (\'hline\'). Instead you should be using double quotes for those attributes.
Then you need to break out of your string in order to use your PHP function, bloginfo(). With that said it makes more sense to use get_template_directory_uri() instead (or stylesheet directory depending on your setup).
Complete example:
echo '<img class="hline" src="' . get_template_directory_uri() . '/img/hline.png" alt="" />';
Documentation: http://php.net/manual/en/language.types.string.php

I would use instead:
get_template_directory_uri()
And I like to write it like this but that depends internally on you.
<img class="hline" src="<?php echo get_template_directory_uri();?>/img/hline.png" alt="" />';

Related

WP/WooComm Adding a variable to a complicated shortcode

I have a shortcode where I need to dynamically insert the current WordPress/ WooCommerce category to it.
The raw shortcode is
[content_control]
[product_table category="product-cateegory-name" columns="sku,name,cf:size,cf:pgk_qty,cf:case_qty,cf:case_wt,stock,price,buy,cf:dia_pitch,cf:length,cf:inner_box_q,cf:inner_box_length,cf:inner_box_width,cf:inner_box_height,cf:outer_box_qty,cf:outer_box_length,cf:outer_box_width,cf:outer_box_height,cf:outer_box_weight" column_breakpoints="all,all,all,all,all,all,all,all,all,none,none,none,none,none,none,none,none,none,none,none,none"]
[/content_control]
I was trying
<?php echo do_shortcode("[content_control]"); ?>
<php
$cate = get_queried_object();
$cateID = $cate->term_slug;
?>
<?php echo do_shortcode("[product_table category="'. $cateID .'" columns="sku,name,cf:size,cf:pgk_qty,cf:case_qty,cf:case_wt,stock,price,buy,cf:dia_pitch,cf:length,cf:inner_box_q,cf:inner_box_length,cf:inner_box_width,cf:inner_box_height,cf:outer_box_qty,cf:outer_box_length,cf:outer_box_width,cf:outer_box_height,cf:outer_box_weight" column_breakpoints="all,all,all,all,all,all,all,all,all,none,none,none,none,none,none,none,none,none,none,none,none"]"); ?>
<?php echo do_shortcode("[/content_control]"); ?>
But I think inserting the variable this way won't work.
I think there is something basic I'm missing
This worked. I had to escape the "s within the PHP and connect the two arguments in the shortcode via a ' . '
$cate = get_queried_object()->slug;
echo do_shortcode("[product_table category=\"' . $cate . ' \" columns=\"sku,name,cf:size,cf:pgk_qty,cf:case_qty,cf:case_wt,stock,price,buy,cf:dia_pitch,cf:length,cf:inner_box_q,cf:inner_box_length,cf:inner_box_width,cf:inner_box_height,cf:outer_box_qty,cf:outer_box_length,cf:outer_box_width,cf:outer_box_height,cf:outer_box_weight\"' . 'column_breakpoints=\"all,all,all,all,all,all,all,all,all,none,none,none,none,none,none,none,none,none,none,none,none\"']"); ?>```

qtranslate-X woocommerce grouped Products

I searched the internet for a possible solution but wasnt able to find one.
The Labels of linked products in a grouped product apear like:
[:de]german value[:en]english value[:]
My goal is to get the title/label in the current languge of my site
do I need to edit the call of my label in grouped.php?
if you need any further information tell me.
You have to edit grouped.php inside /wp-content/themes/YOUR_THEME/woocommerce/single-product/add-to-cart/grouped.php
Wrap all the instances of $grouped_product->get_name() into a __() function.
In my case I had the following code in grouped.php:
<?php echo $grouped_product->is_visible() ? '<a href="' . esc_url( apply_filters( 'woocommerce_grouped_product_list_link', get_permalink( $grouped_product->get_id() ), $grouped_product->get_id() ) ) . '">'
. $grouped_product->get_name() . '</a>' : $grouped_product->get_name(); ?>
so I used the translation function __() to get rid of qTranslate-X language tags:
<?php echo $grouped_product->is_visible() ? '<a href="' . esc_url( apply_filters( 'woocommerce_grouped_product_list_link', get_permalink( $grouped_product->get_id() ), $grouped_product->get_id() ) ) . '">'
. __($grouped_product->get_name()) . '</a>' : __($grouped_product->get_name()); ?>

how to get wp plugin url with echo in variable?

I'm working on a plugins in wordpress. I want to push some data in my database
I d like to get someting like this for my logos:
$logoname = "<img src='".$pluginlocation."".$name.".gif'>";
with something like that (that, which does not work) :
$pluginlocation = echo plugin_dir_url( __FILE__ ) .'/image' ;
tx ;)
Try below code :
<?php
echo '<img src="' . plugins_url( 'images/your_image_name.gif', dirname(__FILE__) ) . '" > ';
?>
for more detail on this please refer https://codex.wordpress.org/Function_Reference/plugins_url

I need to make the links open in a new window php

Working with a Wordpress theme and currently the icons in the header open in the current window which will take traffic away from the site. I would like them to open in a new window/tab. I know target="_blank" will get the result I need, just not sure of the syntax.
foreach( $gdl_social_icon as $social_name => $social_icon ) {
$social_link = get_option($social_icon['name']);
if( !empty($social_link) ) {
echo '<div class="social-icon"><a href="' . $social_link . ' ">' ;
echo '<img src="' . $social_icon['url'] . '" alt="' . $social_name . '"/>';
echo '</a></div>';
}
}
If I understand correctly what you're after, all you need is
echo '...<a href="' . $social_link . ' " target="_blank">';
Replace 4 line:
echo '<div class="social-icon"><a target="_blank" href="' . $social_link . ' ">' ;

Wordpress - output custom field as ul list

I have a custom field who's content I would like to output as a ul list.
The custom field contains words that are separated with spaces.
I'm trying to use this code here but it's not working.
<?php
$list_items = get_post_meta($post->ID, 'idid');
if($list_items){
$list_items = explode(" ", $list_items) {
echo '<ul>';
foreach($list_items as $list_item)
echo '<li>' . $list_item . '</li>';
echo '</ul>';
}
}
?>
1- add ; before explode function, and remove accolades.
2- declare a second variable different than $list_items where to
put result of explode.
3- second parameter of get_post_meta() should be the slug of your custom field (in your case is it idid?), add also true parameter.
Your code will look like:
<?php
$list_items = get_post_meta($post->ID, 'idid', true);
if($list_items){
$list_items2 = explode(" ", $list_items);
echo '<ul>';
foreach($list_items2 as $list_item)
echo '<li>' . $list_item . '</li>';
echo '</ul>';
}
?>

Resources