admin管理员组文章数量:1122846
I am working on a project where I need to display news articles in a timeline (Latest news). I want each news item to be structured as follows:
• The title of the news article • Category (e.g., “Asia,” “Europe”) & Posting time
I only want to show the 5 latest news articles in the timeline. Additionally, lower on the page, I want to display all the latest news of a given catergory (e.g. show all latest news for "Asia").
What would be the best way to achieve this layout? Are there any plugins or best practices for implementing a clean and efficient timeline with these elements?
Any tips would be be greatly appreciated! Thank you in advance!
I am working on a project where I need to display news articles in a timeline (Latest news). I want each news item to be structured as follows:
• The title of the news article • Category (e.g., “Asia,” “Europe”) & Posting time
I only want to show the 5 latest news articles in the timeline. Additionally, lower on the page, I want to display all the latest news of a given catergory (e.g. show all latest news for "Asia").
What would be the best way to achieve this layout? Are there any plugins or best practices for implementing a clean and efficient timeline with these elements?
Any tips would be be greatly appreciated! Thank you in advance!
Share Improve this question asked Sep 26, 2024 at 0:22 code2goatcode2goat 11 Answer
Reset to default 0You can achieve this by creating Shortcodes for showing the the latest news and another to display all latest news of a given category.
Here is the shortcodes you can use:
[latest_news_timeline]
=> This is a shortcode to show the latest news.[category_news]
=> This is a shortcode to display all latest news of a given category.
We need to add given code to functions.php file
of the Theme.
<?php
/**
* This is to create shortcode to display the latest news.
*/
function show_latest_news_timeline() {
$args = array(
'post_type' => 'post', // Here we can change post_type if you are using a custom post type.
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
$output = '<div class="news-timeline">';
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$category = get_the_category();
$category_name = $category[0]->cat_name;
$output .= '<div class="news-item">';
$output .= '<h2>' . get_the_title() . '</h2>';
$output .= '<span class="news-meta">' . $category_name . ' · ' . get_the_date() . '</span>';
$output .= '</div>';
}
} else {
$output .= 'No news found.';
}
$output .= '</div>';
wp_reset_postdata();
return $output;
}
add_shortcode( 'latest_news_timeline', 'show_latest_news_timeline' );
/**
* This is to create shortcode to display all latest news of a given category.
*/
function show_category_news_data( $atts ) {
$atts = shortcode_atts(
array(
'category' => ''
),
$atts,
'category_news'
);
$args = array(
'post_type' => 'post', // Here we can change post_type if you are using a custom post type.
'category_name' => $atts['category'],
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
$output = '<div class="category-news">';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$category = get_the_category();
$category_name = $category[0]->cat_name;
$output .= '<div class="news-item">';
$output .= '<h2>' . get_the_title() . '</h2>';
$output .= '<span class="news-meta">' . $category_name . ' · ' . get_the_date() . '</span>';
$output .= '</div>';
}
} else {
$output .= 'No news found.';
}
$output .= '</div>';
wp_reset_postdata();
return $output;
}
add_shortcode( 'category_news', 'show_category_news_data' );
Also then you need to add given code to style.css file of your theme.
.category-news,.news-timeline{margin:20px 0}.news-item{margin-bottom:20px;padding:10px;border-bottom:1px solid #ccc}.news-item h2{margin:0 0 5px;font-size:20px}.news-meta{font-size:14px;color:#777}
Hope this should help you out.
本文标签: pluginsHow to Display News in a Timeline with HeadlineCategoryand Time
版权声明:本文标题:plugins - How to Display News in a Timeline with Headline, Category, and Time? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288328a1928072.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论