php - Creating a wordpress function with the category ID as a parameter but can't get the right data
Get the solution ↓↓↓I'm trying to create a function inside "functions.php" that everytime I call it I only have to tell which categories ID I want.
The final result should be something like this: Mockup - Posts inside a Caroussel
Without a function I can do this inside "front-page.php" file, but the code will become too messy and repetitive if I start to insert more categories.
Right now this is what I have (front-page.php):
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'orderby' => 'date',
'order' => 'ASC',
'category__in' => 1515
);
$firstCategory = get_posts( $args );
$i = true;
foreach (array_chunk($firstCategory, 3, true) as $firstCategory) : ?>
<div class=" carousel-item <?php echo $i ? ' active' : '' ?>">
<?php foreach( $firstCategory as $post ) : setup_postdata($post); ?>
<div class="card col-4">
<?php if( has_post_thumbnail() ) : ?>
<div class="card-header">
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail('thumbnail', array(
'alt' => trim(strip_tags( $post->post_title )),
'title' => trim(strip_tags( $post->post_title ))
)); ?>
</a>
</div>
<?php endif; ?>
<div class="card-body">
<h3 class="card-title"> <?php the_title(); ?> </h3>
<p class="card-text"> <?php
if(has_excerpt()) {
echo get_the_excerpt();
} else {
echo wp_trim_words(get_the_content(), 15);
}
?>
</p>
<a href=" <?php the_permalink(); ?>" class="card-link">Ver mais</a>
</div>
</div>
<?php
$i = false;
endforeach;
?>
</div>
<?php
wp_reset_postdata(); ?>
<?php endforeach; ?>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a></div>
Quickly explanation, this code gets posts from a category and insert 3 posts each time inside a caroussel.
When I do it inside "functions.php" I can't retrieve all the right data. Only links and Title are ok.
What I could do until now (functions.php):
function displayCatCaroussel($categoryId) {
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'orderby' => 'date',
'order' => 'ASC',
'category__in' => $categoryId
);
$firstCategory = get_posts( $args );
$i = true;
echo '
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">';
foreach (array_chunk($firstCategory, 3, true) as $firstCategory) :
if($i) {
echo '<div class="carousel-item active">';
} else {
echo 'div class="carousel-item">';
}
foreach( $firstCategory as $post ) : setup_postdata($post);
echo '<div class="card col-4">';
if( has_post_thumbnail($post->ID) ) {
echo '<div class="card-header"> <a href="', the_permalink( $post->ID ), '">';
the_post_thumbnail('thumbnail', array(
'alt' => trim(strip_tags( $post->ID )),
'title' => trim(strip_tags( $post->ID ))
));
echo '</a>
</div>';
}
echo '<div class="card-body">
<h3 class="card-title">'. $post->post_title . '</h3>
<p class="card-text">';
if(has_excerpt( $post->post_excerpt )) {
the_excerpt();
} else {
echo wp_trim_words( $post->post_content, 15);
}
echo '</p>
<a href="', the_permalink( $post->ID ), '" class="card-link">Ver mais</a>
</div>
</div>';
$i = false;
endforeach;
echo '</div>';
wp_reset_postdata();
endforeach;
echo '</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a></div>';}
(front-page.php):
<?php displayCatCaroussel(1515); ?>
So thumbnail and "the_excerpt()" aren't working.
What I need to do to get all data right? and is this the right way to do it?
This is the first time I make a question here so if you need more info let me know.
Thanks in advance
Answer
Solution:
There are a few things we can fix/improve upon, but your most pressing issues are
has_excerpt
accepts a post or post ID, changehas_excerpt($post->post_excerpt)
tohas_excerpt($post)
trim(strip_tags($post->ID))
just gives you the ID back, you want something else, post_title maybetrim(strip_tags($post->post_title))
?
Also, don't usesetup_postdata($post)
in your loop and
- use
get_the_post_thumbnail($post, 'thumbnail', $args)
instead ofthe_post_thumbnail('thumbnail', $args)
- and use
get_the_excerpt($post)
instead ofthe_excerpt()
NOTE: default attributes forget_the_post_thumbnail
are:
$default_attr = [
'src' => $src,
'class' => "attachment-$size_class size-$size_class",
'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
];
Is short, your primary issue is that you're mixing up the global $post and the locally scoped $post (and it's meta data).the_post_thumbnail
andthe_excerpt
checks the global $post, buthas_post_thumbnail
andhas_excerpt
is using the local $post. Plussetup_postdata($post)
is going to cause some cascading issues as well. The best solution is to ignore the global $post altogether.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function update() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.