In standard Woocommerce on the products page of the shop has a nice "add-to-cart"-button. I wanted to add a "more-information"-button to it in the same style.
Using the code from this page I managed to add a button by placing this code in my functions.php:
add_action('woocommerce_after_shop_loop_item_title','more_information');
function more_information() {
global $product;
$link = $product->get_permalink();
echo do_shortcode('<br>[button class="meer-informatie" link="' . esc_attr($link) . '"]meer informatie[/button]');
}
It's crucial I add a class to the button I want to add so I can style it. Point is I added the button (see image) but the link isn't working. It needs to link to the productdetail-page. So, where does it go wrong? I can't seem to figure it out (but then again: I only speak PHP a little).
Thanks in advance for any help!
Still don't really know what is wrong with the code I posted in my question but I found something that actually works. Someone might someday be looking for this or encounter the same issue so I'm going to post this as an answer to my own question. After seeking help in all kinds of directions I came up with the following code:
function more_information() {
global $product;
if ($product){
$url = esc_url($product->get_permalink() );
echo '<a rel="nofollow" href="' .$url .'" class="meer-informatie">meer informatie</a>';
}
}
add_action('woocommerce_after_shop_loop_item_title','more_information');
It works like a charm!
If you need to localize the string following Wordpress guide:
function more_information() {
global $product;
if ($product){
$url = esc_url($product->get_permalink() );
echo '<div class="bottone-cat"><a rel="nofollow" href="' .$url .'" class="tt_button tt_primary_button">';
echo __('More Information','woocommerce');
echo '</a></div>';
}
}
I tested it with WPML and it works fine.
Related
I was hoping somebody may be able to help me. I'm having trouble with if statements with the Advanced Custom Fields plugin for Wordpress. I've got three options the user can choose from, all three can be chosen, but they can also choose just one if they wish.
The issue I'm having is the code I've written displays all of the HTML tags, even the empty ones. This is causing styling issues. I want to be able to just show HTML that has been populated. I've tried the solutions on the ACF forums but to no avail.
Link: http://www.advancedcustomfields.com/resources/getting-started/code-examples/
Here's the quick (newbie!) code I've got at the minute:
<?php the_sub_field('link'); ?>
<?php the_sub_field('doc'); ?>
<p><?php the_sub_field('cap'); ?></p>
I looked on the ACF forum and tried this, but it broke the theme:
<?php if(the_sub_field('link')) {
echo '' . the_sub_field('link') . '';
} ?>
<?php if(the_sub_field('doc')) {
echo '' . the_sub_field('doc') . '';
} ?>
<?php if(the_sub_field('cap')) {
echo '<p>' . the_sub_field('cap') . '</p>';
} ?>
I'm looking for some help to make this work. I don't think I'm too far away from the right answer, however I'm a bit of a rookie with anything beyond standard front-end stuff, any thoughts would be very much appreciated.
Thanks!
Try to use get_sub_field();
<?php if(get_sub_field('link')) {
echo '' . the_sub_field('link') . '';
} ?>
<?php if(get_sub_field('doc')) {
echo '' . the_sub_field('doc') . '';
} ?>
<?php if(get_sub_field('cap')) {
echo '<p>' . the_sub_field('cap') . '</p>';
} ?>
When looping through one of these fields, this function returns a sub field from the current row.
Like Dk-Macadamia said, try to use get_sub_field() in loops instead of the_sub_field()
the difference is get_sub_field() return the value as a string, and the_sub_field() print the data,
Also get_sub_field() only work under a repeater/ fluid field type otherwise wont work,
if its not a sub field of repeater/fluid fields just try get_field()
I've done a ton of searching and I'm pulling out what's left of my hair. The PHP code on its own is:
<?php echo wp_get_attachment_url($file->ID) ?>
...the shortcode is: [embed][/embed]
I've tried all sorts of variations of 'do shortcode' but can't get things working. Can you suggest the correct code?
WordPress (3.6 or higher) has an audio shortcode, the following should work:
$mp3 = wp_get_attachment_url( $file->ID );
echo do_shortcode( '[audio src="' . $mp3 . '"]' );
More info: https://codex.wordpress.org/Audio_Shortcode
Hopefully this is simple to solve. I have a few elements which pull in form data previously entered into the database, which are added to the post as custom fields. I extract this using a php query as such:
<a href="<?php echo get_post_meta($post->ID, 'societywebsite', true); ?>" target="_blank" >Website</a>
Obviously, this appears in the Loop (content.php) and will get added to any appropriate post. Sometimes, however, there won't be any data because a user chose not to enter a website into the 'societywebsite' field on a form. When this happens, there's no need for a link to 'Website' to be there, because it wouldn't go anywhere useful.
What I'd like to know is how to have an If clause that checks if the data exists and then shows the link only if it does. I don't mind javascript, but the cleaner and less 'hacky' the solution, the better, as content.php will run multiple times for search results.
Thanks so much for any help or advice you can provide.
Another way to do this, more according to the Wordpress Codex is the following...
$societywebsite = get_post_meta($post->ID, 'societywebsite', true);
if ($societywebsite != '') {
echo '<a href="' . $societywebsite . '" target="_blank" >Website</a>';
}
You can add an 'else' at the end of this for debugging purposes.
Assuming that field is a string for a url, you could do something like this.
if (strlen(get_post_meta($post->id, 'societywebsite', true)) < 1) {
echo '<a href="' . get_post_meta($post->ID, 'societywebsite', true) . '" target="_blank" >Website</a>';
}
That will very simply check if that field has a string that is more than 1 characters. If it is more than 1 characters, it will display the link. If not...it won't do anything.
hi i need help with a small bit of code for my wordpress theme.
<a href="<?php echo $link; ?>">
<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
echo ' <li>'.$tag->name.', </li>';
}
?>
</a>
the code
<?php echo $link; ?>
this is for a link of a website that is already on the page the link part works great the only issue is i want the links (tags that are being used as keywords/anchor text)to be seo friendly for outbound links
what changes are needed? feel free to spice the link up for seo :)
If you're wanting the same link for each of the tags, then this is a solution:
<ul>
<?php
global $post;
foreach( get_the_tags($post->ID) as $tag ) {
echo '<li>' . $tag->name . '</li>';
}
?>
</ul>
Please note: I've added the unordered list start and end elements as these were missing, these could also be ordered list start and ends.
EDIT:
to reflect your code changes.
The code still looks the same...I would assume it should look something like this: (and I could be completely wrong) :)
<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
echo ' <li>'.$tag->name.'</li>';
}
?>
I am building a wordpress ecommerce template with Cart66 plugin. Using a Cart66 shortcode inside a php template page, I would like to generate the Post ID inside the shortcode. Can someone please help and tell me if this is possible. Here is the code that I am using.
<?php echo do_shortcode("[add_to_cart item=\". the_ID() .\" quantity=\"user:1\"]"); ?>
This code will lay inside loop-single.php and above
Thank you!
#silent almost had it, but it should be get_the_ID() and not the_ID() since the later one echos it out, so try:
<?php echo do_shortcode("[add_to_cart item=\"". get_the_ID() ."\" quantity=\"user:1\"]"); ?>
The right line should be:
<?php echo do_shortcode("[add_to_cart item=\"". the_ID() ."\" quantity=\"user:1\"]"); ?>
simpler correct answer:
<?php echo do_shortcode('[add_to_cart item="'. get_the_ID() .'" quantity="user:1"]');?>