WordPress obtains all category names and ID program instances

Source: Internet
Author: User
Tags wordpress blog

I have recently studied the CMS topic of WordPress. One of the reasons is that I have already said that I will not publish a Chinese WordPress blog topic easily, because I really think it is worthless, in a few days, the bottom of the copyright will disappear. For a CMS topic, the category of the home page is naturally the most frustrating. After a few tossing times, I finally learned the new PHP skills for getting the names and IDs of all WordPress categories. GET ~ The main body of the code is WPDAXUE, but I don't know if you find it. In fact, WPDAXUE cannot be used, and there are all fatal PHP syntax errors-I can't speak out, after several changes, the code posted below will definitely be able to withstand the test. I will also take a note to make everyone feel free to use it ~ The first step is to add a large part of code to the function. Of course you can do it without adding it. You can also paste it when you need to call it.

The code is as follows: Copy code

Function zzfly_show_category (){
Global $ wpdb;
$ Request = "SELECT $ wpdb-> terms. term_id, name FROM $ wpdb-> terms ";
$ Request. = "left join $ wpdb-> term_taxonomy ON $ wpdb-> term_taxonomy.term_id = $ wpdb-> terms. term_id ";
$ Request. = "WHERE $ wpdb-> term_taxonomy.taxonomy = 'Category '";
$ Request. = "order by term_id asc ";
$ Categorys = $ wpdb-> get_results ($ request );
Foreach ($ categorys as $ category) {// call the menu
$ Output = '<span> '. $ category-> name. "(<font style =" color: red; "> ". $ category-> term_id. '</font>) </span> ';
Echo $ output;

After adding the above code to a proper position, you can use the following code to display the code as needed.

The code is as follows: Copy code
<? Php zzfly_show_category ();?>


After adding the above code to a proper position, you can use the following code to display the code where it needs to be displayed. Yes, that's all about this large piece of code, I have been tossing for nearly an hour. Most of the code comes from the great WPDAXUE, but I fixed several bugs. Today I am here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.