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();
Related
I am trying to create a a page in wordpress where I show 5 recent posts.
I was able to successfully do that.
However, I am not able to retrieve images that are present inside that post.
I am using this code to retrieve post url and post image. (Post URL works fine!)
$args = array('numberposts'=>'5');
$recentposts = wp_get_recent_posts($args);
foreach($recentposts as $post){
$v = $post['ID'];
$postlink = get_permalink($v);
$postimg = get_the_post_thumbnail($v);
}
try below code
$posts = get_posts( array( 'posts_per_page' => 5 ) );
foreach ( $posts as $_post ) {
if ( has_post_thumbnail( $_post->ID ) ) {
echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
echo get_the_post_thumbnail( $_post->ID, 'thumbnail' );
echo '</a>';
}
}
I was able to get the title and excerpt to display correctly, but I can't figure out which permalink call to use.
function display_excerpt_shortcode( $atts ) {
extract(shortcode_atts(array(
'excerptid' => ''
), $atts));
if ($excerptid) {
$args=array(
'p' => $excerptid,
'post_type' => 'post',
'post_status' => 'publish'
);
$my_query = new WP_Query($args);
if ($my_query) {
$title = apply_filters( 'the_title', $my_query->posts[0]->post_title );
$excerpt = apply_filters( 'the_excerpt', $my_query->posts[0]->post_excerpt );
$link = apply_filters( 'the_permalink', $my_query->posts[0]->post_permalink );
return '<h3>' . $title . '</h3> <p>' . $excerpt . '</p> <a class="button small primary" href="' . $link . '" title="' . $title . '" >Read More </a>';
}
}
return;
}
add_shortcode('display_excerpt', 'display_excerpt_shortcode');
I've tried all kinds of combinations. the_permalink, get_permalink, post_permalink... I just can't figure out if it's the wrong combination or if I'm just completely off the mark. Thanks in advance.
Have you tried:
$link = get_permalink( $my_query->posts[0]->post_ID )
I think your problem is that the query object has no 'permalink' property.
Following the guide in the codex's class reference page, you'll find the pattern of setting a new post object during each iteration of the loop with $the_query->the_post();:
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//Now reference WP post functions:
the_title();
the_permalink();
}
}
Sorry it's a totally different design, but this has never failed me.
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?
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');