Post excerpt doesnt work - php

Hello stackoverflow people, I need help. I'm using wordpress and somehow I can't use this function:
$post_id = 12;
echo get_post($post_id)->post_excerpt;
Somehow it prints nothing. Here is the full div. Can you help me to solve this problem?
<div id="block1">
<div class="inblock1">
<h2>About boots</h2>
<p><?php $post_id = 12;
echo get_post($post_id)->post_excerpt; ?> </p>
apac
</div>
</div>

It sounds like you don't actually have an excerpt set for this post. You can always use a conditional to test it, and output a custom excerpt (from the post_content) if one doesn't exist:
$my_post = get_post($post_id);
// If the excerpt is empty, generate one from post_content, else display the saved excerpt
echo empty($my_post->post_excerpt) ? wp_trim_words($my_post->post_content, 55, '...') : $my_post->post_excerpt;
Read more about wp_trim_words() in the Codex.

You may need to use setup_postdata() because <?php $post_id = 12; echo get_post($post_id)->post_excerpt; ?> will return excerpt if you have excerpt data in excerpt field but below code will return data from content if you don't have data in excerpt field.
$post_id = 12;
$tempVar = $post;
$post = get_post($post_id);
setup_postdata($post);
the_excerpt();
wp_reset_postdata();
$post = $tempVar;

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);

Wordpress post Loop not working as expected

I'm using a theme that uses the blog page to display all of its content on the blog page rather than an excerpt and then when i click into the post i want to show the whole content.
I'm using the following code:
$postId = get_the_ID();
$ex = the_excerpt();
if($postId == 19){
echo $ex;
}
else{
echo $content;
}
The blog page is located at post =19
I would expect only the excerpt to show on the blog page and the content to show on the post page. However both show. Also it doesnt matter if i change the number 19 in my if statement as the same happens. Can anyone see where i am going wrong?
edit made changes, screen shots:
You need to use the_excerpt(); inside the condition means where you want to display the excerpt but in case you want to get value of excerpt but does not want to display it directly then you have to use get_the_excerpt(); so you need to change
$ex = the_excerpt();
to
$ex = get_the_excerpt();
And same is the case with the_content()
Hope it helps you.
functions such as the_excerpt() are only available inside the loop or after you call the function the_post()
You may want to display only the except in your index.php
while (has_posts()) {
the_post();
the_excerpt();
}
In your single.php you may want to display the whole content
if(has_posts()) {
the_post();
the_content();
}
Use $postId = get_queried_object_id(); instead of $postId = get_the_ID();

How to get the content by using get_next_post() and its Id

I am trying te get some cutted Text with substr() Function from the next post data.
This works great but i cant find a way to get just the content from the next post by its ID
<?php $next_post = get_next_post();
if (!empty( $next_post )){ ?>
<?php echo get_the_post_thumbnail($next_post->ID, 'medium'); ?>
<?php } ?>

how to pull custom field value

I have a custom field, "price" and I want to pull that custom field for each individual post.
I have the code:
<?php $price = get_post_meta($post->ID, 'Price', true); ?>
<?php echo $price; ?>
But this is pulling the value of the first post and applying it to all the posts. Can someone help me understand this code and understand why it's applying the same field value to all my posts? How can I make it pull the individual value for each post?
Thank you all, I'm really new at this.
try to put this line at the start of script:
global $post;
thn try with your code:
<?php $price = get_post_meta($post->ID, 'Price', true); ?>
<?php echo $price; ?>
thanks
I have already checked it.
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'yourcustomfield', true);
wp_reset_query();
?>

How to escape in foreach in php?

I have a Wordpress site and I am trying to fetch posts from a specific category. That works, but now I want to link to the specific posts with a permalink. I could not getting it to work with escaping in a foreach. Can someone tell me/show me what I am doing wrong here?
<?php
global $post; // required
$args = array('category' => 9); // include category 9
$custom_posts = get_posts($args);
foreach($custom_posts as $post) :
setup_postdata($post);
echo "<a href='.the_permalink().'> the_title() </a> ";
the_excerpt();
//and so on..
endforeach;
?>
You're not actually closing your quotes properly and aren't using the correct functions. Line 5 should read more like:
the_title("<a href='".get_permalink()."'>",'</a>',true);
BOOM.

Categories