Pass PHP Variable into a shortcode - php

I'm trying to do something that seems really simple but I can't figure it out.
In my category template, I have this shortcode:
<?php echo do_shortcode( '[jobs categories="manufacturing"]' ); ?>
I also have this to show the title of the category:
<?php single_cat_title(); ?>
I would like to put them both together so I can pass the category into the shortcode so it fetches the correct job category listings. So I created this:
<?php $category = single_cat_title(); ?>
<?php echo do_shortcode('[jobs categories=' . $category . ']'); ?>
but it doesn't seem to work properly - Am I doing something wrong? Is this even possible?
Many thanks!!

If you look at the documentation for the single_cat_title() function, you'll see that it can either display or return the value, and it accepts two parameters $prefix and $display with default values of '' and true respectively.
What this means, is that just using single_cat_title(); will print Category Title to the document.
If you want to use it as a variable (without printing it to the document), you'll need to set the second parameter to false:
$category = single_cat_title( '', false );
This will define the $category variable for you, without printing anything, and you can then pass it to your shortcode (also note, that typically you'll want quotes around your attribute values in shortcodes):
echo do_shortcode( '[jobs categories="' . $category . '"]' );
You can make it a bit more succinct as well:
<?php
$category = single_cat_title( '', false );
echo do_shortcode( "[jobs categories='$category']" );
?>

Related

HTML5Blank Save excerpt into array

I am using HTML5Blank as a starting theme and it comes with this function which returns 40 chars excerpt:
<?php html5wp_excerpt('html5wp_custom_post'); ?>
My main blog page is more complex, so I am using array to store values into it and echo them where I need them:
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $post_titles[$counter] = get_the_title($post->ID); ?>
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post_id); ?>
<?php $post_permalinks[$counter] = get_the_permalink($post->ID); ?>
<?php $post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid')); ?>
<?php $counter++; ?>
<?php endwhile; ?>
All other fields work and I can echo them, but I don't have any idea how to make excerpt work because it isn't echoing anywthing with:
<?php echo $post_excerpts[0]; ?>
First of all I notice, that you are using a variable called $post_id, which is not defined as far as I can see. You need to add a $post_id = $post->ID; before or alternatively you can use instead:
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post->ID); ?>
Maybe this already solves your problem. But to make sure, I will take it further.
I took a look at the functions.php of the HTML5-Blank-Theme: https://github.com/html5blank/html5blank/blob/master/src/functions.php
// Create 40 Word Callback for Custom Post Excerpts, call using html5wp_excerpt('html5wp_custom_post');
function html5wp_custom_post($length)
{
return 40;
}
So the function only returns the value of 40. I guess you can simply use the html5wp_excerpt() like this:
html5wp_excerpt(40);
Maybe there is something wrong with the html5wp_custom_post, so you can get rid of it to test this. And I also think, why use an addtional function, if it only returns a number... you can easily set it in the function call.
I don't know if this functions accepts an post ID as a parameter. So maybe it can only be used inside of a single.php page. I can't find a documentation about this, maybe you can do some research and find it out.
Here is another way to achieve it:
You can use the get_the_title() function that will accept the post id. Unfortunately, the get_the_excerpt() does not accept it.
So we first need to get the post object and then apply a filter to get the excerpt of the post. Do this by putting this code inside your while loop:
<?php $current_post = get_post($post->ID); ?>
You now have the current post as an object. In the next line, we apply the filter and save the result to the array at the right index position:
<?php $post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt); ?>
I wonder why there are so many php opening and closing tags, so you could make your code more readable:
<?php while ($the_query->have_posts()) : $the_query->the_post();
$current_post = get_post($post->ID);
$post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_titles[$counter] = get_the_title($post->ID);
$post_permalinks[$counter] = get_the_permalink($post->ID);
$post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid'));
$counter++;
endwhile; ?>
Just as an additional info, you could also use only the filters, should work with your post object:
$post_titles[$counter] = apply_filters('the_title',$current_post->post_title);
EDIT:
You can trim your excerpt to a certain length with mb_strimwidth (read more: https://www.php.net/manual/en/function.mb-strimwidth.php) :
$current_post = get_post($post->ID);
$trim_excerpt = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_excerpts[$counter] = mb_strimwidth($trim_excerpt, 0, 40, '...');
EDIT 2:
Maybe you should check if you are getting your post object. The fact that you are always seeing the same excerpt, maybe means you get the excerpt of the current page (not the post in your query).
$current_id = get_the_id();
$current_post = get_post($current_id);

do_shortcode within loop only returns content for first post

I have a shortcode that is supposed to return the title and content for multiple custom posts on a single page. It correctly displays the title for each post, but when it comes to displaying each post's content, it only displays the content from the first post. What am I doing wrong?
Figuring out how to get the content from within a shortcode has been tricky enough, so if anyone has any suggestions, I'd appreciate it!
My code is:
if($customposts->have_posts() ) : while ($customposts->have_posts() ) : $customposts->the_post();
$post= get_the_ID();
$foo = get_the_title();
$bar=do_shortcode($content);
echo "<h2>".$foo."</h2><p>".$bar."</p>";
endwhile; endif;
It doesn't look like you need to us do_shortcode. If title is working correctly, you should be able to assign get_the_content() to your $bar variable.
$bar = get_the_content();
Since you're looping through posts, store the html from each iteration of the loop, and then return all of the html at once (remember, you can't echo or print from a shortcode callback... well, you shouldn't, or you'll get some unexpected results):
$html = '';
if($customposts->have_posts() ) :
while ($customposts->have_posts() ) : $customposts->the_post();
$post= get_the_ID();
$foo = get_the_title();
$bar = get_the_content();
$html .= "<h2>$foo</h2><p>$bar</p>";
endwhile; endif;
return $html;
Also, be careful about using $post as your own variable, you will undoubtedly come into conflict with other scripts, including core.

Show Post Count For User Wordpress

I am trying to show the amount of posts each users on my WP site has posted.
Currently the code I have is:
<?php
$author_id = the_author_meta('ID');
echo count_user_posts('$author_id');
?>
As you can see I am storing the author ID in $author_id and running that in the count_user_posts() echo.
When I run the to strings separately it works however when, I combine them as above it doesn't.
Any ideas?
Regards,
You should use get_the_author_meta for this kind of purposes.
$author_id = get_the_author_meta('ID');
echo count_user_posts($author_id);
the_author_meta should be used only for echoing stuff.
Try to use it like this :
<?php
$author_id = the_author_meta('ID');
echo count_user_posts($author_id); // remove quotes
?>
Hope it works for you now.
I found you need to add your post, page or your_custom_post_type for it to work successfully.
<?php echo 'Posts made: ' .(count_user_posts(get_the_author_meta('ID'),'your_custom_post_type') ); ?>
// or multiple post types
<?php echo 'Posts made: ' . count_user_posts( get_the_author_meta('ID'),['job', 'featured_job', 'free_job'] ); ?>

Get_field value to show in shortcode

Im trying to use do_shortcode to display data from an advanced custom field which is in the admin cms.
I have an advanced customed field called 'meta_slider_shortcode'. I would like the do_shortcode to display the data that is in the meta_slider_shortcode' field.
Here is my php:
<?php
$meta = get_field( "meta_slider_shortcode" );
echo do_shortcode('$meta');
?>
Check the value of $meta. If it does not contain square brackets then add it in do_shortcode like do_shortcode("[$meta]");
The short code cant have ' ' wrapped around the variable.
Here is a working example:
<?php
$meta = get_field( "meta_slider_shortcode" );
echo do_shortcode($meta);
?>
if you have short code then use it some thing like this
$yourvar = do_shortcode( '[your-short-code]' );
echo $yourvar;
or
echo do_shortcode( '[your-short-code]' );
in your case it would
echo do_shortcode(get_field( "meta_slider_shortcode" ));

PHP echo variable within function arguments

I'm currently using a wordpress function in order to display posts from a specific category. A simplified example is shown below:
<?php query('cat_name=cat1&posts=1') ?>
Essentially this gets 1 post from the category cat1.
However I have a variable saved which gets the current category (this is on category pages):
<?php $thiscat = get_the_category(); ?>
Current Category: <?php echo $thiscat ?>
How can I now echo the variable $thiscat into the arguments of my query above so that the category name is filled in for me? This function is applied on different category pages so having it automatically passed to the arguments of my query saves a lot of hassle.
Thanks in advance for any help.
You only echo something when you want to output it to the browser, here we concatenate the query string with the variable:
<?php $thiscat = get_the_category(); ?>
<?php query('cat_name=' . $thiscat . '&posts=1') ?>
Not sure I understand the question, but it sounds like you want to use $thiscat in your query. This should do it:
<?php
$thiscat = get_the_category();
query("cat_name=$thiscat&posts=1")
?>
Note the double quotes, which are necessary. If you use single quotes, the variable will not get expanded.

Categories