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
Related
I'm trying to create a conditional statement that checks to see if a post has a thumbnail, and if so, echos out a div that has the post thumbnail as the background image.
<?php if( the_post_thumbnail() ) {
echo '
<div class="post-hero" style="background-image: url(' . wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ');">';
'</div>';
};?>
The problem is that my editor (vsCode) is throwing an error: an unexpected echo. I'm thinking it has to do with the second echo within the url.
I've tried changing echo to print, but that didn't work as well.
I'm stuck, and I'm new to PHP programming. Anything glaring in my code?
There is a error in your echo string and the_post_thumbnail() needs to be replaced by has_post_thumbnail(). See this fixed version:
if(has_post_thumbnail() )
echo '<div class="post-hero" style="background-image: url(\'' . wp_get_attachment_url( get_post_thumbnail_id($post->ID)). '\');"></div>';
}
There were two errors in my code, one answered by #Amacado, and I changed
if( the_post_thumbnail() )
to
if( has_post_thumbnail() )
It worked then. Thanks!
I would like to get the post thumbnail and link it to the post in WordPress. This seems logical to me but it's not producing the desired result.
<?php
if(is_category()){
echo '' . '<img src="' . the_post_thumbnail( 'full' ) . '"';
}
?>
Instead, it's producing this in the DOM:
What do I need to do to get this working?
This worked for me:
echo '<img src="' . get_the_post_thumbnail() . '';
Have you tried the 'get_the_post_thumbnail_url()' function? As described on this page https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
Also I'm not sure if the 'full' size is correct, maybe first try it without the optional size parameter. This only works while "in the loop", or you'll have to specify the post ID as the first parameter.
You might want to create an if statement to display the post tile if there isn't a post thumbnail (the function returns false if this is the case).
<?php
if(is_category()){
echo '' . '<img src="' . get_the_post_thumbnail_url( 'full' ) . '"/>';
}
?>
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.
recently i install wordpress Themeforest VideoTube Theme. But when i use codecanyon Social Locker plugin, then facing this issue. My wordpress theme only support do_shortcode
My short code is...
[sociallocker id="2378"] [/sociallocker]
and i want to put this code in the middle position of my short code.
<div class="player player-small <?php print apply_filters( 'aspect_ratio' , 'embed-responsive embed-responsive-16by9');?>">
Kindly help me... how can i to do this? What query i set to in my wordpress theme functions.php file?
You can use do_shortcode like this way,
$text_to_be_wrapped_in_shortcode = '<div class="player player-small <?php print apply_filters( 'aspect_ratio' , 'embed-responsive embed-responsive-16by9');?>">';
echo do_shortcode( '[sociallocker id="2378"]' . $text_to_be_wrapped_in_shortcode . '[/sociallocker]' );
This should work,
$text_to_be_wrapped = "<div class='player player-small ".apply_filters( 'aspect_ratio' , 'embed-responsive embed-responsive-16by9')."'";
echo do_shortcode( '[sociallocker id="2378"]' . $text_to_be_wrapped . '[/sociallocker]' );
I have a Wordpress website with Virtue theme. And I want the page titles on every page (except the homepage) give a typing effect. I installed this plugin and it wrotes I just need to insert this code:
<?php echo do_shortcode( '[typed string0="Projects" typeSpeed="40" startDelay="0" backSpeed="40" backDelay="500"]' ) ?>
into the .php file in the theme where I need it. I just need to change the "Projects" to the actual page's title.
Where should I insert it?
You should concatenate that string with get_the_title():
<?php echo do_shortcode( '[typed string0="' . get_the_title() . '" typeSpeed="40" startDelay="0" backSpeed="40" backDelay="500"]' ) ?>
I could find the solution! I had to insert it in page-header.php like:
<?php echo do_shortcode( '[typed string0="'. apply_filters('kadence_page_title', kadence_title() ) .'" typeSpeed="40" startDelay="0" backSpeed="40" backDelay="500"]' ); ?>