John Davidson

php - Add post category to latest post block in WordPress

0 comments
Message:


I am editing the latest-blog.php file for WordPress and I have two goals.



  1. I would like the post category to be displayed and have a class of category assigned to it.

  2. I would like to assign a class to the thumbnail, the title and the excerpt so I can style them.


The code below is where I believe the changes need to made


foreach ( $recent_posts as $post ) {
$post_id = $post['ID'];

$title = get_the_title( $post_id );
if ( ! $title ) {
$title = __( '(Untitled)' );
}

$post_url = get_permalink( $post_id );

$text = get_post( $post_id );
$text = $text->post_content;
$text = wp_trim_words( $text, 25, '... <a class="subtitle-red" style="display:block;" href="'.$post_url.'">read&nbsp;full&nbsp;article</a>' );
$excerpt = $text;

$list_items_markup .= sprintf(
'<li>
<a href="%2$s">%1$s%3$s</a><p>%4$s</p>',
get_the_post_thumbnail( $post_id ),
esc_url( get_permalink( $post_id ) ),
esc_html( $title ),
$excerpt
);

if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
$list_items_markup .= sprintf(
'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
esc_attr( get_the_date( 'c', $post_id ) ),
esc_html( get_the_date( '', $post_id ) )
);
}
$list_items_markup .= "</li>\n";
}

As this is a core WP file editing it really isn't the best idea so an extra question... could this be done via functions.php instead?


Thanks


TJ



My Answer:
>post_content;_ _ $thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );_ _ $output .= '
';_ $output .= '' . $thumbnail . '';_ $output .= '

' . $title . '

';_ $output .= '

' . $text . '

';_ $output .= '
';_}_echo $output;

Rate this post

5 of 5 based on 2944 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us