I have created a custom shortcode and can get the information to output, however it does not show up where I have placed it in the content hierarchy - it always prints at the top of the post/page. Any clue as to why this may be happening?
in my functions.php:
function sc_pdf($atts, $content = null) {
$pdfname = the_field('pdf_title');
$pdfimage = the_field('pdf_file');
$pdflink = the_field('pdf_thumbnail');
return '<p>'.$pdfname.'</p><p>'.$pdfimage.'</p><p>'.$pdflink.'</p>';
}
add_shortcode("peedeef", "sc_pdf");
Since you are using the_field method, I assume you use ACF plugin.
You should use get_field instead of the_field since the_field will output the specified field.
function sc_pdf($atts, $content = null) {
$pdfname = get_field('pdf_title');
... etc
Always use "return" instead echo.
You'll able to get data in proper location.
To move your shortcode around, don't use echo.
if you place the shortcode in your doc in the first example it will always float to the top.
in the second if I place it at the bottom it will appear at the bottom.
My shortcode is [showpod]
CODE THAT YOU CANT PLACE ANYWHERE
function makepod($atts) {
echo "<div class='podmysqlarray2 showpodholder'><h3 class='widget-title newposts'>Latest Snippets</h3>";
$args = array( 'numberposts' => '6' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<div class="pod"><li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> </div>';
}
echo "</div>";
}
add_shortcode('showpod', 'makepod');
AND NOW REVISED CODE YOU CAN PLACE ANYWHERE: -
function makepod($atts) {
$cat = "<div class='podmysqlarray2 showpodholder'><h3 class='widget-title newposts'>Latest Snippets</h3>";
$args = array( 'numberposts' => '6' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$cat.= '<div class="pod"><li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> </div>';
}
$cat .= "</div>";
return $cat;
}
add_shortcode('showpod', 'makepod');
Related
I'm trying to retrieve a list of posts in a custom widget in wordpress and I'd like to assign each li a class name based on the category name (or slug) of each item. I think I'm close but alas my classnames are showing empty. I'm not the greatest with PHP I'm sure it's a syntax issue...here's where I'm at so far:
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
$categories = get_the_category($args);
foreach( $recent_posts as $recent ){
echo '<li class="' . $categories["Post_ID"]->slug . '"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"]) . $recent["post_title"].'</a> </li> ';
}
?>
</ul>
get_the_category is expecting a post_id, not an array of posts.
Try something like
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$categories = get_the_category($recent["ID"]);
echo '<li class="' . $categories[0]->slug . '"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"]) . $recent["post_title"].'</a> </li> ';
}
I'm not sure if the key name is actually "ID" or "Post_ID" (you seem to be using both). Please check that too.
I have a function here which will grab the two most recent post and grab the image posted within those two post. The issue is though, it keeps grabbing the first image of the first post and not also the second:
Code:
function getImageFeatured($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, '<img');
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, '<img', $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, '>');
$postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
$image[$i] = $postOutput;
$start=$imgEnd+1;
}
if(stristr($image[$num],'<img')) { echo ''.$image[$num].""; }
$more = 0;
}
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . getImageFeatured('1') .'</a></li> ';
}
Not to sure why It is only sending out the first image of the first post.
Suggestion and thoughts appreciated.
EDIT:
With Revision:
.....above function I posted....
global $post;
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $post ){
setup_postdata( $post );
echo '<li><a href="' . get_permalink() . '" title="Look '.get_the_title_attribute().'" >' . getImageFeatured('1') .'</a></li> ';
}
wp_reset_postdata();
This does not work either for me. It seems if I use the setup_postdat... it wont process but if I remove it it goes through but does not do what I ask, but does what I stated the problem to be.
get_the_content is getting the content of the post in the loop. You may run the function on both posts but it still gets the content of the post that's in the loop.
The function you've written needs some serious improvement but for the sake of getting it working in the quickest manner possible replace the existing foreach loop with:
global $post;
foreach( $recent_posts as $post ){
setup_postdata( $post );
echo '<li><a href="' . get_permalink() . '" title="Look '.get_the_title_attribute().'" >' . getImageFeatured('1') .'</a></li> ';
}
wp_reset_postdata();
this is my query to show my work in the portfolio
<?php
// The Query
$the_query = new WP_Query( array( 'post_type'=> 'portfolio' ) );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
$medium = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
$url_thumb = $thumb['0'];
$url_medium = $medium['0'];
$option = '<li>';
$option .= '<a data-value="' . get_the_terms($post->ID, 'portfolio' ) . '" data-largesrc="' . $url_medium .'" data-title="' . get_the_title() .'" data-description="' . get_the_content() .'">';
$option .= '<img src="' . $url_thumb . '" alt="img01" />';
$option .= '</a>';
$option .= '</li>';
echo $option;
}
} else {
}
/* Restore original Post Data */
wp_reset_postdata();
?>
The problem is here, in the data-value I need to extract the category of work
data-value="' . get_the_terms( 'portfolio', $post->ID ) . '"
I think the code that I use is wrong because if I put online I truncates the code and I do not show anything
Your code suggests that portfolio is a custom post type, not a custom taxonomy, but you are passing it as the taxonomy parameter for get_the_terms(). These are not the same things - post types are types of content (e.g. posts, pages) and taxonomies are ways to organize and group things (e.g. tags, categories).
You need to pass the slug of the custom taxonomy as the $taxonomy parameter, not the slug of the custom post type portfolio. I don't know what taxonomy you are querying but it is probably something like portfolio_categories or similar. For example if you were using the default category taxonomy with the post you would want get_the_terms($post->ID, 'category');
You are using wrong syntax, the right syntax is
<?php get_the_terms( $id, $taxonomy ); ?>
i am using below code to display the latest post from sub-domain to domain--Here is the code i am using ---
I need to show the limit characters of post_title like to 30
Here is the code..
function render_my_recent_posts( $numberposts = 5 ) { ?>
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<li><img src="/images/default-user-avatar-3.jpg"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul><?php
}
Any help ?
Thankyou
Would substr($recent["post_title"],0,30) do what you need?
So I'm trying to write a new plugin since I haven't been able to find one that does exactly what I want with the extensibility that I desire. The goal of the plugin is to be able to use a simple shortcode to display an image slider that automatically populates with your blog's latest posts.
I've got the basic plugin files ready and the shortcode implemented and tested. I had a snafu solved yesterday on SO but the solution highlighted a new problem. Here's the code:
function slyd( $category, $slydcount ) {
global $post;
$tmp_post = $post; // Create $tmp_post to empty $post once Slyd is done with it
$args = array(
'category' => $category,
'numberposts' => $slydcount
);
$slydposts = get_posts( $args );
foreach( $slydposts as $post ) : setup_postdata($post);
$post_title = get_the_title(); // Get the post's title
$post_content = get_the_content(); // Get the post's content - will write code to get excerpt later
$post_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' ); // Get the post's featured image's src
$post_permalink = get_permalink(); // Get the post's permalink
echo '<h2>' . $post_title . '</h2>'
. '<p>' . $post_content . '</p>'
. '<p>' . $post_thumb . '</p>';
endforeach;
$post = $tmp_post; // Empty $post once Slyd is done with it
}
// Create the shortcode
function slyd_shortcode( $atts ) {
// $atts ::= array of attributes
// examples: [slyd]
// [slyd category='slide']
// [slyd slydcount='5']
// [slyd theme='default']
/* Retrieve attributes set by the shortcode and set defaults for
unregistered attributes. */
extract( shortcode_atts( array(
'category' => 'slyd', // Which category(s) to display posts from
'slydcount' => '5', // How many Slyds to display
'theme' => 'default' // Which Slyd theme to use
), $atts ) );
return "<p>category = {$category}, count = {$slydcount}</p>"
. slyd( $category, $slydcount );
}
add_shortcode( 'slyd', 'slyd_shortcode' );
The issue is in the foreach loop in function slyd();. I was originally using a return where the echo is now to put the result on the screen. That worked to display the first post but it would, of course, escape the function. I need it to cycle through and display all of the posts.
From researching the PHP documentation I found that I could use print in place of echo or return but it's giving me the same result as echo. What's happening is the code seems to be executing twice. It places itself where it needs to be the first time, then it also echoes to the browser and places it just below the head of the page.
I suppose my question is whether there's an alternative to return, echo, or print that would solve my problem?
Thanks in advance.
Now I'm trying to get the plugin to pull in the blog's latest posts but I'm running into a bit of a snafu. When I use the_title() and the_permalink() they display outside of the code I'm trying to contain them in. Further, the_content() is displaying once with the_permalink() and the_title() and then a second time where it's supposed to.
You can see the behavior here.
return is what you want in this case. You want to return a value (i.e. html code) from the slyd function so you can use it (in this case append it) in the slyd_shortcode function. However, you need to first collect all output into an additional variable ($ret below), and then return that value:
function slyd( $category, $slydcount ) {
global $post;
$tmp_post = $post;
$args = array(
'category' => $category,
'numberposts' => $slydcount
);
$slydposts = get_posts( $args );
$ret = '';
foreach( $slydposts as $post ) {
setup_postdata($post);
$post_title = get_the_title();
$post_content = get_the_content();
$post_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
$post_permalink = get_permalink();
$ret .= '<h2>' . $post_title . '</h2>'
. '<p>' . $post_content . '</p>'
. '<p>' . $post_thumb . '</p>';
}
$post = $tmp_post;
return $ret;
}
As you see you should first initialize the $ret variable with an empty string, then append to it on each iteration of the foreach loop. return is used to return the whole string after the loop.