Displaying a Single WordPress Category
In my case, the output of a separate category was needed to display news on the main page, because the page was not standard, then the usual way of output did not suit me, and besides the news, there was also a section of projects.
To display a separate category in WordPress, I found an interesting code. I use and recommend. Very comfortably.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<h2 class="pages_search">Новости</h2> <ul class="is_kat"> <?php $posts_new = get_posts(‘category=1&numberposts=3′); foreach ($posts_new as $post_new) : echo ‘<li class="news_item"><a href="’; echo get_permalink($post_new->ID); echo ‘">’; echo mysql2date(‘j.m.У’, $post_new->post_date); echo ‘ – ‘; echo $post_new->post_title; echo ‘</a></li>’; endforeach; ?> </ul> |
That is, we get all records with the condition we need get_posts(‘category=1&numberposts=3’) into an object $posts_new. Further, we just go around it in a loop and display the records we need.
More about the function get_posts() read here
If this information is not enough, read about the function $wpdb->get_results().
You can also read about the output of a separate category in WordPress here.:
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.
What means “j.m.�”
http://clip2net.com/s/5IJ3qj
article is very good
thanks for the comment, corrected in the article. I had an error Y in English does not perceive, put in Russian, so it's better to retype the code, so that no errors occur.
The line changes the display format of the date on the output.
HM. still not working on my site. Somewhere the syntax is wrong, something else. It's a pity, much needed piece – the situation is banal – in three places you need three different headings. You will have to install three different plugins.
Send your code to info[and]wp-admin.com.ua, I'll take a look
Все, Thanks a lot, figured out, the whole problem was in the copied quote “’”, when he took it over – everything worked great
I'm glad that my code for displaying a separate category was useful to you.