I am using the WordPress Advanced Custom Field plugin. I have a custom built download button and shortcode. Now I am using ACF fields to populate the download btn shortcode. Everything seems to be working and download button is displaying as expected. The only problem is that ACF fields are not populating in the shortcode but its showing outside the shortcode code.
<?php
if (get_field('dn_btn_url1')) {
echo do_shortcode( '[download_btn url="'.get_field('dn_btn_url1').'" filename="'.get_field('dn_btn_txt1').'"]' );
}
?>
The code without the if statement is working as expected but it doesn't hide the shortcode.
The get_field function gets the post id from the global $post object so when it's passed into the shortcode function, it seems like it can't get the right post id. Try putting your URL from the field into a variable so the URL is already passed through the function and into the shortcode function.
Try this:
<?php
if (get_field('dn_btn_url1')) {
$url = get_field('dn_btn_url1');
echo do_shortcode( '[download_btn url="' . $url . '" filename="' . $url . '"]' );
}
?>
You can do that by using sprintf function please try it once
<?php
if(get_fiedl('dn_btn_url1')) {
$url = get_field('dn_btn_url1');
$filename = get_field('dn_btn_txt1');
$shortcode = sprintf(
'[download_btn url="'.$url.'" ,
filename="'.$filename.'"]');
echo do_shortcode( $shortcode );
?>
Related
I'm using Wordpress Avada theme and try to modify the 'Read more' link to always go to an external site.
I'm using the following code :
function my_read_more_link( $read_more ) {
$post = get_post();
$url = 'https:/www.websitename.com/';
$postslug = $post->post_name;
$link = $url . $postslug;
$excerpt = 'Read more at websitename.com!';
return $excerpt;
}
add_filter( 'avada_blog_read_more_link', 'my_read_more_link' );
In Theory this is working because the blog page shows the following:
<a href="https:/www.websitename.com/the-post-title-here">
Read more at websitename.com!
</a>
But this is formatted as text, not as a link. I've provided some screenshots for more clarity :
Screenshot from the blog page
https://imgur.com/a/Ztof7wB
Screenshot from the source code : (output)
https://imgur.com/a/YV7JACp
Any help is appreciated!
I am currently trying to do research on how to output a result from a WordPress custom plugin to the frontend of WordPress. I have trying adding filters, do actions, add actions. But nothing seems to work. I know the add_action works for child themes because I'm currently using a script to do so.
The script below works for the child theme but not in a plugins functions.php page.
function displayPages()
{
$pageIDs = "List of page ids";
if(is_page($pageIDs))
{
include_once 'script.js';
}
}
add_action('wp_footer', 'displayPages')
Here is the current code I was trying use to fix the problem with the plugin.
function getPageIDs()
{
include_once($_SERVER['DOCUMENT_ROOT'].'/stage/wp-config.php' );
global $wpdb;
$row = $wpdb->get_row( 'SELECT pages FROM schemalocalbusiness WHERE id = 1');
$pageIDs = $row->pages;
$pageIDsArray = explode(",", $pageIDs);
foreach($pageIDsArray as $perma)
{
echo ' ' . esc_url( get_permalink($perma) ) . '<br>';
}
}
function includeShemaOnPage()
{
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(getPageID() == $acturla_link)
{
print 'SUCCESS';
}
}
add_action('wp_footer', 'includeShemaOnPage');
Any and all help would be much appreciated! Thank you in advance!
I am using get_posts to retrieve posts information from database. It returns "Post title", "thumbnail", "post category", and "post excerpt". Everything is working fine but the problem is I am unable to show post excerpt.
Here is my code:
function widget ($args,$instance) {
extract($args);
$title = $instance['title'];
$catid = $instance['catid'];
$numberposts = $instance['numberposts'];
$date = $instance['date'];
$rss = $instance['rss'];
// retrieve posts information from database
global $wpdb;
$posts = get_posts('post_type=post&numberposts='.$numberposts.'&category='.$catid);
$out = '<ul>';
if ($posts) {
foreach($posts as $post) {
setup_postdata($post);
$out .= '<li>'.get_the_post_thumbnail($post->ID,'medium').'</li>';
$out .= '<li>'.$post->post_title.'</li>';
$out .= '<li>'.$post->post_excerpt.'</li>';
if ($date) $out .= '<li>'.date('d/m/Y', strtotime($post->post_date_gmt)).'</li>';
}
}
if ($rss) $out .= '<li>Category RSS</li>';
$out .= '</ul>';
//print the widget for the sidebar
echo $before_widget;
echo $before_title.$title.$after_title;
echo $out;
echo $after_widget;
}
}
$post->post_excerpt does not get work the way you think it does. Most people think this is the same as the template tag the_excerpt(), and it is not
the_excerpt() is generated by truncating get_the_content(). $post->post_excerpt is not generated at all as this is user defined. This excerpt is the excerpt text manually added by the user in the post edit screen in the excerpt meta box. (This meta box is hidden by default, but can be enabled in the "Screen Options" tab on the top part of the screen). If the user did not specify a manual excerpt, $post->post_excerpt will return nothing, that is why you see this behavior
You have already set up your postdata, so you can just simply use the template tags directly, so in place of $post->post_excerpt, you can use the_excerpt()
EDIT
Thanks to the comment below, I did not take into account that the excerpt should not be echo'd straight away. In this case, you would make use of get_the_excerpt() which does not echo the text, but simply retrieves it.
Here is what I'd like to accomplish
Goal A: I want to hyperlink each thumbnail in index.php to their post.
Goal B: I want to define a hyperlink for each thumbnail in single.php to an external website.
You may ask why am I using thumbnails for single.php? The reason is because I want this layout:
And so far I understand that there are 3 methods to display images:
Insert image into the editor area along with the text, but the problem is I cannot float the image and text differently because all items within a post are assigned a p tag - am I wrong?
Custom fields should get the job done but it doesn't seem the most efficient way - or am I wrong?
Post Thumbnails should be the easiest way but see my problem below
I have the code to accomplish Goal A and B but they only work separately.
In other words, "Code 1" does not work if "Code 2" is present.
How can I resolve this issue? Or is there a better method accomplish my goal?
Code 1: Link thumbnails to external websites using custom field (single.php)
<?php $name = get_post_meta($post->ID, 'externalurl', true);
if( $name ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {
the_post_thumbnail();
} ?>
Code 2: Link thumbnails to the post (functions.php)
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html( $html, $post_id, $post_image_id ) {
$html = '' . $html . '';
return $html;
}
is_single() function will help you achieve what you need. Try below code in functions.php and remove the additional code from single.php
function my_post_image_html( $html, $post_id, $post_image_id ) {
if ( is_single()) {
$name = get_post_meta($post_id, 'externalurl', true);
if( $name ) {
$html = '' . $html . '';
}
return $html;
}
else
{
$html = '' . $html . '';
return $html;
}
}
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
I am trying to create a shortcode for 2 of my theme's options. Basically I want to grabe my theme options to my shortcode so that I can display them anywhere on my posts something like this:
[ads_1]
[ads_2]
However when I tried to get the options on my shortcode it wont work at all and its giving me an error
Parse error: syntax error, unexpected 'theme_settings' (T_STRING) in C:\xampp\htdocs\themewp\wp-content\themes\bots_final\shortcodes.php on line 86
Here's my snippet for my shortcode where in I am trying to grab my theme option data:
add_shortcode('ads_1', function($atts){
return '
<div>
<?php $options = get_option( 'theme_settings' ); ?>
<?php echo $options['banner1']; ?>
</div>';
});
I am trying to grab the options from my Theme options page. Check my theme options code:
<div>
<?php $options = get_option( 'theme_settings' ); ?>
<?php echo $options['banner1']; ?>
</div>
Any idea what's the problem why I can't grab it?
You opened the <?php inside an open php tag:
add_shortcode('ads_1', function($atts){
$html = '<div>';
$options = get_option( 'theme_settings' );
$html .= $options['banner1'];
$html .= '</div>';
return $html;
});
What do you get from get_option( 'theme_settings' );? It seems like your suggesting an array..
To avoid conflicts, I'd store your settings as 'mytheme_theme_settings'.