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\"']"); ?>```
On Wordpress, I’m trying to add a link to single product page using Code Snippets with Advanced Custom Fields. Instead of a link, my code displays as plaintext.
I have tried this code:
function product_datasheet_below_summary() { ?>
$link = get_field('datasheet');
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class="button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php
};
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );
This doesn’t work. I was hoping for a link to the Datasheet, but it simply prints, in plaintext:
$link = get_field(‘datasheet’); if( $link ): $link_url =
$link[‘url’]; $link_title = $link[‘title’]; $link_target =
$link[‘target’] ? $link[‘target’] : ‘_self’; ?>
followed by a generic square button link.
What am I doing wrong here? Thanks very much for your help.
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:
`
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php endif; ?>`
You're getting plaintext after the first ?> because that's a php ending tag, and the Code Snippets plugin doesn't allow for multiple php statements and is simply crashing and dumping plain text rather than executing code.
You need to rewrite the whole function as one php statement and echo all the button html, along with the php variables delimited in the html with .'s. A simple example:
<?php
$var = "Hello World";
echo "<p>The value of the variable is : " . $var . "</p>";
?>
And you may need to use the more standard ACF get field construct, too:
$value = get_field( "text_field" );
Search SE for more examples of echoing html in php.
Your function is a bit all over the place, I have cleaned it up to work in the output you want it to using an object instead of echoing out multiple parts of the button code. This in my opinion is easier to manage and looks nicer as it keeps the HTML and PHP code as separate as possible:
function product_datasheet_below_summary() {
$link = get_field('datasheet');
if( $link ){
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
} ob_start();?>
<?php if($link):?>
<a class="button" href="<?php echo $link_url;?>" target="<?php echo $link_target;?>"><?php echo $link_title;?></a>
<?php endif;
return ob_get_clean();
} add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );?>
I inserted a post by the ID of it into the a template (footer.php) but I only get the raw text from the post. The html tags and everything else is missing. I tried it with this
<?php
$post_id = 32;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $title;
echo $queried_post->post_content;
?>
and also a with a shortcode and plugin. Same result.
I need the html tags and everything because the site is build with a site builder (elementor) and it has some progressbars inside.
Any Ideas? Is this even possible?
You need to apply the filters, here's how your updated code looks:
<?php
$post_id = 32;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $title;
echo apply_filters('the_content', $queried_post->post_content);
?>
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>';
}
?>
I'm struggling with wordpress templates. In one of my theme templates I have the following lines
<?php
echo get_the_ID(); // displays nothing
echo get_the_title(); // displays "Hello World! this is title"
?>
Why does the get_the_ID() return nothing ? I want the page id of the current page I'm on.
This works within the loop:
<?php $this_page_id = get_the_ID(); echo $this_page_id ; ?>
Outside of the loop:
<?php $this_page_id = $wp_query->post->ID; echo $this_page_id ;?>
if you are using wp_query just add this
<?php echo $wp_query->post->ID; ?>