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 the main page in Wordpress I use multiple excerpts of different pages. The problem is though that it returns everytime the same excerpt link. It seems like the last link (of 3) on the page is used everytime.
The code (of course I changed the number of the id everytime ;-) :
<?php
$post_id = 35; // post id
$queried_post = get_post($post_id);
$my_excerpt = get_excerpt_by_id_long($queried_post); //$post_id is the post id of the desired post
echo '<a href="' . get_permalink( $queried_post ) . '" title="' . $queried_post->post_title . '">';
echo '<h3><strong>';
echo $queried_post->post_title;
echo '</strong></h3>';
echo '</a>';
echo $my_excerpt;
?>
This is in functions.php
function get_excerpt_by_id($post_id){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_length = 35; //Sets excerpt length by word count
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$words = explode(' ', $the_excerpt, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '…');
$the_excerpt = implode(' ', $words);
endif;
$the_excerpt = '<p>' . $the_excerpt . '<a class="leesmeer" href="'.get_permalink($post_id).'">lees verder...</a></p>';
return $the_excerpt;
}
never mind, I found the solution: forgot to use the $post_id in the .get_permalink()...Edited the code in the above functions.php
In single.php I use <?php the_category(', '); ?>. This function lists categories attached to post, but title attribute (on hover) is missing. How can I add this? I've tried with adding a filter in functions.php and making a new function like <?php the_better_category('%cat% - my text'); ?>, but the result is miserable.
From the WordPress Codex:
<?php single_cat_title( '', true ); ?>
The first part (in single quotes) will output whatever custom text you put there before the category title and true means it will display (false is use in PHP).
Have you tried:
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>
Hey guys I am making a Widget to display a posts title, and excerpt from that post, and its date. The code I have is here:
public function widget( $args, $instance ) {
extract( $args );
$headline = $instance['headline'];
$category = $instance['category'];
$numberposts = $instance['numberposts'];
$readmore = $instance['readmore'];
echo $before_widget;
echo $before_title;
echo "<p class=\"headline\">$headline</p>";
echo $after_title;
$args = array( 'numberposts' => $numberposts, 'category_name' => $category );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<a href="' . get_permalink($recent["ID"]) . '" title=" '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> ';
echo the_time('F j, Y');
echo the_excerpt();
}
As you can see I am trying to call the time and the excerpt from the post. It is working but it is only displaying the date and and excerpt from the very first post that is called welcome. I want it to display the Date and excerpt from each individual post. I will post a link to the site with the widget in the side bar. Sorry if I am not being clear enough or if more info is needed I am very new to this.
http://www.modmacro.us/wpsandbox/
First thing: the_excerpt(), is already an echo statement. Not a return statement. It's the same with the_date(). Prefix them with "get_", and they will return the information you need, or you can get rid of the echo command preceding it.
Second thing: In order for those functions to work, they need to be used in a Wordpress loop.
Since we're already in a loop, we just have to make sure it changes the Wordpress Globals appropriately to reflect the information you need..
foreach( $recent_posts as $recent ){
setup_postdata(get_post($recent['ID']));
echo '<a href="' . get_permalink() . '" title=" '.esc_attr(get_the_title()).'" >' . get_the_title().'</a> ';
echo get_the_time('F j, Y', $recent['ID']);
the_excerpt();
}
wp_reset_postdata();
I have been trying to echo the post thumbnail for worpress using the string and the function but it doesnt work.
Here is the template i am trying to show it.
if ($favorite_post_ids):
$favorite_post_ids = array_reverse($favorite_post_ids);
foreach ($favorite_post_ids as $post_id) {
$p = get_post($post_id);
echo "<li>";
echo "<a href='".get_permalink($post_id)."' title='". $p->post_title ."'>" . $p->post_title . "</a> ";
echo '<img src="'.get_the_post_thumbnail(array(55,55)).'" />';
wpfp_remove_favorite_link($post_id);
echo "</li>";
First parameter of get_the_post_thumbnail() is the post ID, not the thumbnail size