PHP - adding function to string - php

I'm relatively new to PHP and don't even really know how to ask the question for which i need help with, so please excuse my lack of technical knowledge - or referring to terms correctly for that matter.
I cannot figure out a way to add the "if(function_exists("the_ratings"))" code below to a string as in the PHP i have below. I know that the way it is below is not correct, but i've placed it there to show how and where i need it to display - help is greatly appreciated.
function latestreleases() {
$args = array( 'posts_per_page' => 30, 'cat' => '-1, -2' );
$last_5_posts_query = new WP_Query( $args );
while($last_5_posts_query->have_posts()) :
$last_5_posts_query->the_post();
$link = get_permalink();
$title = get_the_title();
$description = excerpt(16);
$details = 'Watch ';
$readmore = 'Read more...';
$content .= '<div class="all-latest-movies">';
$content .= '<h3>'.$title.'</h3>';
$content .= '<div class="thumbnail"><a href=" '.$link. ' ">'
. get_the_post_thumbnail( null, "home-movie-thumbnail")
. '</a></div>';
$content .= '<div class="description">' .$description. ' <a href= '.$link.' >' .$readmore. '</div>';
$content .= '<div class="view-listing"><a href= '.$link.' target="_blank" >' .$details. $title. '</a></div>';
$content .= '<div class="ratings">' if(function_exists("the_ratings")) { the_ratings(); } '</div>';
$content .= '</div><!-- .fetured-movies -->';
endwhile;
return $content;
}
add_shortcode('LatestReleases', 'latestreleases' );

Use a ternary operator:
$content .= '<div class="ratings">'. ( function_exists("the_ratings") ? the_ratings() : '' ) .'</div>';

If you want to do it inline you can use the ternary operator
$content .= '<div class="ratings">'.(function_exists("the_ratings")?the_ratings():'').'</div>';
If you want to use the if syntax
$content .= '<div class="ratings">';
if(function_exists("the_ratings")){
$content.=the_ratings();
}
$content.='</div>';

Related

How to translate button english to spanish with php?

so I have this code below that is showing on my career page.
I have a title content on the left and on the right, a button, and when clicking on it, it shows the full content of the article.
function myWebsite_careers_function() {
$markup = '';
$args = array(
'post_type' => 'career',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'post_date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ($loop->found_posts > 0) {
$markup .= '<div class="container">';
while ( $loop->have_posts() ) : $loop->the_post();
$link = get_the_permalink();
$markup .= '<div class="row pt-3 pb-3">';
$markup .= '<div class="col-8">';
$markup .= '<h4>' . get_the_title() . ',<br>' . get_field( "location" ) . '</h4>';
$markup .= '</div>';
$markup .= '<div class="col-4">';
$markup .= '<h4 style="text-align: right;">' . __("learn more", "myWebsite") . '</h4>';
$markup .= '</div>';
$markup .= '</div>';
endwhile;
wp_reset_postdata();
$markup .= '</div>';
}
return $markup;
}
add_shortcode( 'myWebsite_careers', 'myWebsite_careers_function' );
I can translate all my content except the button "learn-more" (code upper).
I would like to know how to get "aprende más" instead of "learn more" when it is in the Spanish version.
I hope you can help me with that, thank you in advance. Cheers!
I found the solution, it was in my WPML plugin, so I clicked on "theme and plugins localization" and then scanned my theme => string translation => and search the string in the list. Translate and its work. thank you for your help cheers! –

How do I call post objects (products) custom fields using shortcodes on posts

I'm trying to show 'featured products' on blog posts. These featured products are to be selected with custom fields post objects on the back end for each post.
I have written up what I think the PHP should be - where am I going wrong? When I try to use the shortcode no code appears (but the shortcode text doesn't show so it's definitely added). Thanks :)
<?php
add_shortcode('featuredproducts' , 'printfeaturedprod');
function printfeaturedprod(){
$html = '';
$instruments = get_field('featuredprod');
if( $instruments ):
$html .= '<div class="featuredproducts">';
$html .= '<h2 style="font-size:18px; font-family:poppins;">Featured in this video</h2>';
foreach( $instruments as $instruments ):
$permalink = get_permalink( $instruments->ID );
$title = get_the_title( $instruments->ID );
$product = wc_get_product( $instruments->ID );
$price = $product->get_price();
$featured_img_url = get_the_post_thumbnail_url($instruments->ID, 'full');
$html .= '<div class="featuredproduct">';
$html .= '<img class="featuredproductimg" src="' . $featured_img_url . '">';
$html .= '<div class="proddetails">';
$html .= '<a class="producttitle" href="' . $permalink . '"><?php echo esc_html( $title ); ?></a>';
$html .= '<br><span class="productprice">£' . $price . '</span>';
$html .= '</div>';
$html .= '</div>';
endforeach;
$html .= '</div>';
endif;
}
You have built your HTML in the $html variable but then you don’t do anything with it. The shortcode doesn’t automatically know that you want to display the $html variable so you need to return ( or echo) it at the end before the function finishes:
add_shortcode('featuredproducts' , 'printfeaturedprod');
function printfeaturedprod(){
$html = '';
/* your code here... */
return $html;
}
Reference: See the add_shortcode WP documentation

ACF get_field() is not returning a value in WordPress

I am using ACF and CPT cooperatively. I created a shortcode to be placed in a text module in my theme. It works well. Yet, when I call ACF get_field(), it's not returning any value. I tried looking into this question and also this one, but neither works.
I double-checked ACF fields name. Also tried to change the input type from text to number but still no hope.
Development Environment
WordPress Version: 5.2.2 (Latest at the moment)
Theme/Child Theme Version: Divi 3.25.3
The shortcode I created:
<?php
add_shortcode('RESTAURANT_MENU', 'fetch_menu_products');
function fetch_menu_products($atts)
{
$atts = shortcode_atts(array(
'category_name' => ''
), $atts);
$category_name = $atts['category_name'];
$args = array(
'category_name' => $category_name,
'post_type' => 'menu',
'numberposts' => -1,
'post_status' => 'publish'
);
$output = '';
$menu_products = get_posts($args);
foreach ($menu_products as $menu_product) {
setup_postdata($menu_product);
$output .= '<section class="menu-item-wrapper">';
$output .= '<h3 class="menu-item__title">' . $menu_product->post_title . '</h3>';
$output .= '<div class="menu-item">';
$output .= '<div class="menu-item-description">';
$output .= '<p class="menu-item-description__text">' . $menu_product->post_content . '</p>';
$output .= '</div>';
$output .= '<ul class="menu-prices-list">';
if (get_field("regular_size_price") || get_field("large_size_price")) {
$output .= '<li class="menu-prices-list--item">R ' . get_field("regular_size_price", $menu_product->ID) . ' Currency</li>';
$output .= '<li class="menu-prices-list--item">L ' . get_field("large_size_price", $menu_product->ID) . ' Currency</li>';
}
if (get_field("price")) {
$output .= '<li class="menu-prices-list--item">' . get_field("price", $menu_product->ID) . ' Currency</li>';
}
$output .= '</ul>';
$output .= '</div>';
$output .= '</section>';
}
wp_reset_postdata();
return $output;
}
Can any help me find out why isn't it returning any value, please? Thank you.
Update: The ACF Location Rules
Try to use get_post_meta() instead of get_field(). As you are already using ACF then use this code to get value from post meta
get_post_meta($menu_product->ID, 'regular_size_price')[0]
For more help on get_post_meta() you can check this link
If my assumption is correct, your li elements are not being printed when you return the $output variable. That should be the case since you are using get_field function outside of theloop without passing post id in if statement.
Below is the corrected code:
if (get_field("regular_size_price", $menu_product->ID) || get_field("large_size_price", $menu_product->ID)) {
$output .= '<li class="menu-prices-list--item">R ' . get_field("regular_size_price", $menu_product->ID) . ' Currency</li>';
$output .= '<li class="menu-prices-list--item">L ' . get_field("large_size_price", $menu_product->ID) . ' Currency</li>';
}
I hope this helps.

Wordpress shortcode issue

i have this code for shortcode
function custom_query_shortcode( $atts ) {
// EXAMPLE USAGE:
// [postcat name="Lorem"]
// Defaults
$atts = shortcode_atts(array(
'name' => ''
), $atts, 'postcat');
// Reset and setup variables
$output = '';
$title = '';
$link = '';
$featImg = '';
// the loop
$argsSC = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page'=> -1,
'category_name' => $atts['name']
);
$querySC = new WP_Query( $argsSC );
if ( $querySC->have_posts() ) {
$output .= '<div class="row" style="margin-top: 30px;">';
while ( $querySC->have_posts() ) {
$querySC->the_post();
$title = get_the_title(get_the_ID());
$link = get_permalink(get_the_ID());
$featImg = ( has_post_thumbnail() ? get_the_post_thumbnail_url(get_the_ID(), 'large') : get_template_directory_uri() . '/imgs/No_Image_Available.gif' );
$id = get_the_ID(get_the_ID());
$chartL = get_field( 'chart' );
$storyL = get_field( 'story' );
// output all findings - CUSTOMIZE TO YOUR LIKING
//$output .= "<div><img src='$featImg'><a href='$link'>$title</a></div>";
$output .= '<article id="post-'.$id.'" class="csseco-tssf-t col-12 col-md-6">';
$output .= ' <div class="standard-featured bg-img-el" style="background-image: url('.$featImg.')">';
$output .= ' <a class="standard-featured-link" href="'.$link.'"></a>';
$output .= ' <div class="cat-tssf">';
if ( !empty($chartL['url']) ) {
$output .= ' Charts';
}
if ( !empty($storyL['url']) ) {
$output .= ' The Story';
}
$output .= ' </div>';
$output .= ' <h3 class="ttl-tssf">';
$output .= ' <span class="dtable">';
$output .= ' <span class="dtable-cell">';
$output .= ' <a href="'.$link.'" rel="bookmark">';
$output .= $title;
$output .= ' </a>';
$output .= ' </span>';
$output .= ' </span>';
$output .= ' </h3>';
$output .= ' </div>';
$output .= '</article>';
}
$output .= '</div>';
} else {
$output .= "nothing found.";
}
return $output;
wp_reset_query();
wp_reset_postdata();
}
add_shortcode("postcat", "custom_query_shortcode");
In the backend, when i press "Add new category" to make a new category(duh!) does not work. The category its added but the page does not refresh. I'm pretty sure it's something with this code, but i really dont know what... if i dont include this file in functions.php categories and tags works perfectly...
Please help and thanks in advance!
Can't comment, so I'll post it as an answer.
Please look at:
return $output;
wp_reset_query();
wp_reset_postdata();
The code after the return will not be executed I believe. So change it to:
wp_reset_query();
wp_reset_postdata();
return $output;
Maybe this will solve your issue?

how to use the post ID in as a shortcode attribute?

I have created a custom post type in WordPress. I want use the post ID in a shortcode so I can pull specific posts to output the data, which is a Gallery using fields from Advanced Custom Fields.
Here's my function so far:
function mbgc_gallery_shortcode( $atts ) {
// $post_id = $atts['id'];
extract( shortcode_atts(
array(
'id' => '',
), $atts )
);
$html_out = '123';
// $post = get_post( $id );
// if ( $post ) :
if( have_rows('mbgc_gallery') ):
$html_out .= '<div class="mbgc-gallery owl-carousel owl-theme">';
while( have_rows('mbgc_gallery') ): the_row();
// vars
$image = get_sub_field('mbgc_image');
$caption = get_sub_field('mbgc_caption');
$title = get_sub_field('mbgc_title');
$sub_title = get_sub_field('mbgc_sub_title');
if ( $image ) :
$html_out .= '<div class="mbgc-gallery-item">';
if ( $caption ) :
$html_out .= '<div class="mbgc-slide-caption">';
$html_out .= '<h4>' . $caption . '</h4>';
$html_out .= '<div class="mbgc-slide-titles">';
$html_out .= '<h6>' . $title . '</h6>';
$html_out .= '<h6>' . $sub_title . '</h6>';
$html_out .= '</div>';
$html_out .= '</div>';
endif;
$html_out .= '<div class="mbgc-slide">';
$html_out .= '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';
$html_out .= '</div>';
$html_out .= '</div>';
endif;
endwhile;
$html_out .= '</div>';
endif;
// endif;
return $html_out;
}
add_shortcode('mbgc_gallery', 'mbgc_gallery_shortcode');
I'm hoping the shortcode can look like [mbgc_gallery id="401"], as an example. The main thing is I am not sure how to exactly write it in the function to pull the ID.
You need to pass the ID from the shortcode attributes to the ACF have_rows() function so that the correct data is retrieved.
<?php
function mbgc_gallery_shortcode( $atts ) {
$attributes = shortcode_atts(
array(
'id' => null
), $atts );
// $attributes['id'] is now the passed-in ID
$html_out = '';
if( have_rows('mbgc_gallery', $attributes['id']) ):
$html_out .= '<div class="mbgc-gallery owl-carousel owl-theme">';
while( have_rows('mbgc_gallery', $attributes['id']) ): the_row();
// vars
$image = get_sub_field('mbgc_image');
$caption = get_sub_field('mbgc_caption');
$title = get_sub_field('mbgc_title');
$sub_title = get_sub_field('mbgc_sub_title');
if ( $image ) :
$html_out .= '<div class="mbgc-gallery-item">';
if ( $caption ) :
$html_out .= '<div class="mbgc-slide-caption">';
$html_out .= '<h4>' . $caption . '</h4>';
$html_out .= '<div class="mbgc-slide-titles">';
$html_out .= '<h6>' . $title . '</h6>';
$html_out .= '<h6>' . $sub_title . '</h6>';
$html_out .= '</div>';
$html_out .= '</div>';
endif;
$html_out .= '<div class="mbgc-slide">';
$html_out .= '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';
$html_out .= '</div>';
$html_out .= '</div>';
endif;
endwhile;
$html_out .= '</div>';
endif;
return $html_out;
}
add_shortcode('mbgc_gallery', 'mbgc_gallery_shortcode');
Use the shortcode like this: [mbgc_gallery id="123"].
Personally I don't like using extract() - this turns an array into individual variables, named according to the array element key, and can make debugging difficult. Instead, just access your id like this: $attributes['id'] as shown above.
Good luck!

Categories