I am just trying to generate a path with something like :
$PhotoName = the_title();
$DestinationFile = 'temp/watermarked/';
$DestinationFile .= $PhotoName;
$DestinationFile .= '.jpg';
the_title(); is a Wordpress function that gets the title of the post. If I echo just $PhotoName I see the name of the post as suspected. However if I echo $DestinationFile it will never print that part of the string so I would see something like temp/watermarked/.jpg, it never prints $PhotoName as part of it.
You need to pass false as the third parameter to get it to return the page title to you.
$PhotoName = the_title('', '', false);
See the WordPress reference for more details.
the_title() is a template tag that must be run within the loop. The title applies filters and prints the title to the screen.
Use get_the_title() to return the value in php.
When using a foreach loop with get_posts() use $post->post_title to get the value. If you want the filters applied: apply_filters( 'the_title', $post->post_title );
Related
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']" );
?>
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.
I would like to get the post thumbnail and link it to the post in WordPress. This seems logical to me but it's not producing the desired result.
<?php
if(is_category()){
echo '' . '<img src="' . the_post_thumbnail( 'full' ) . '"';
}
?>
Instead, it's producing this in the DOM:
What do I need to do to get this working?
This worked for me:
echo '<img src="' . get_the_post_thumbnail() . '';
Have you tried the 'get_the_post_thumbnail_url()' function? As described on this page https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
Also I'm not sure if the 'full' size is correct, maybe first try it without the optional size parameter. This only works while "in the loop", or you'll have to specify the post ID as the first parameter.
You might want to create an if statement to display the post tile if there isn't a post thumbnail (the function returns false if this is the case).
<?php
if(is_category()){
echo '' . '<img src="' . get_the_post_thumbnail_url( 'full' ) . '"/>';
}
?>
how to print the content in wordpress by passing the string php.I have written the following code but it print all the content including image.I want it to print only particular text.
<?php
$content = get_the_content('Read more');
print $content;
?>
This depends on whether or not you're in the Loop - if you are then your code should work - see more here: https://codex.wordpress.org/The_Loop
However, if you are outside the loop and want to pass in a post ID as a paramter, you can refer to this post as everything is explained very well in there: https://wordpress.stackexchange.com/questions/51741/get-post-content-from-outside-the-loop
$page_id = 302;
$page_object = get_page( $page_id );
echo $page_object->post_content;
Use the_excerpt for only showing small content with read more
<?php $short_desc= get_the_excerpt();
echo substr($short_desc,0,200);?>
Here's what I'd like to do. This function which grabs the content of a custom Wordpress post field:
get_post_meta($post->ID, 'society_twitter', true);
Needs to run in place of the string 'yourTwitterUsername' below,
[twitter-widget username="yourTwitterUsername"]
Looking something like:
[twitter-widget username="get_post_meta($post->ID, 'society_twitter', true);"]
Is this possible? If so, what is the syntax? A little more detail: I'm trying to modify a Twitter widget to display the username listed in a custom post field in my database rather than just one string value. I've tried editing the plugin php itself but this seems like an easier solution.
Use string concatenation?
$string = '[twitter-widget username="' . get_post_meta($post->ID, 'society_twitter', true) . '"]';
Edit: Updated based on the comments:
$string = '[twitter-widget username="' . get_post_meta($post->ID, 'society_twitter', true) . '"]';
echo do_shortcode( $string);