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\"']"); ?>```
Related
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 am trying to get one of my WordPress post info. my code is
$xa = get_post( get_the_ID());
echo '<pre>';
var_dump($xa);
echo '</pre>';
This code returns like this... http://postimg.org/image/p7pnvsnb3/
How can I get data form that indicated target which is in the link.
Here's the post title
echo $xa->post_title;
Here you can use the_title() or you can echo get_the_title() .
echo get_the_title();
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 am using the shortcode in the loop inside template file
and also using the lightbox form plugin.
<?php query_posts('showposts=9&post_type=packages') ?>
<?php while (have_posts()) :the_post(); ?>
<?php echo the_post_thumbnail(); ?>
...
...
<?php echo do_shortcode("[formlightbox text='Book Now' title=the_title][contact-form-7 id='383' title='Booking Form'][/formlightbox]"); ?>
<?php endwhile; ?>
Please note that in the shorcode there is title=the_title and it is not appened to the anchor tag.
But when I use title='hello'or something else it gets appended to the anchor tag.
I want current post's title should get appened to the rendered anchor tag via shortcode.
please help me
Break the string and use the string concatenation operator to combine the function into your string.
<?php echo do_shortcode("[formlightbox text='Book Now' title='" . get_the_title() . "'][contact-form-7 id='383' title='Booking Form'][/formlightbox]"); ?>
Update
This should use the get_the_title() as opposed to the_title() which echos the title.
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; ?>