Examples of wordpress getting category directories

Source: Internet
Author: User
Tags tag name

1. How do I obtain the Category ID based on the category name?
This function is required to obtain the Category ID based on the category name: get_cat_ID ()
The function is used as follows:

The code is as follows: Copy code

<? Php get_cat_ID ($ cat_name)?>

The following is a brief introduction to this function. The first is the parameter $ cat_name, which is the default value of the category name.

Is "General". Its type is generic. Optional. Value returned by the function: 0 is returned when an error occurs, and the category ID is returned if the error is successful. The type is integer.

Number.
Let's take a look at the example below:

The code is as follows: Copy code

<? Php
$ Category_id = get_cat_id ('Category name ');
Query_posts ('cat = '. $ category_id );
If (have_posts (): while (have_posts (): the_post ();
The_content ();
Endwhile; endif;
?>

2. How do I obtain the link URL of a category based on the Category ID?
To implement this function, you must use this function: get_category_link ()
The function is used as follows:

The code is as follows: Copy code

<? Php get_category_link ($ category_id)?>

The following are some introductions and descriptions about this function: parameter: $ categroy_id category idnumber default value: None, type is an integer,

Required; returned value: URL string type
Example:

The code is as follows: Copy code

<? Php
$ Category_id = get_cat_ID ('Category name ');
$ Category_link = get_category_link ($ category_id );
?>
<A href = "<? Php echo $ category_link;?> "Title =" Category Name "> your Category directory Name </a>

3. Display the link to the document category directory
This function is called by the_category ()
It must be emphasized that it must be used in the main LOOP! (Remember !)
The usage is as follows:

The code is as follows: Copy code

<? Php the_category ($ separator, $ parents);?>

This function must be described as follows:
Parameter: $ separator indicates the text or character displayed between category directory links. By default, category directory links are arranged in an unordered list. Word

String type
$ Parents how to display sub-category directory links, string type, values include:
'Multiple '-- display the link between the parent category and its subcategory to show the "parent-child" relationship
'Single '-- only displays links of subcategories, and marks the "parent-child" relationship with the link text
The default value is to display subcategory links with no logo text
Next, we will give two examples to illustrate the specific usage of this function:
Example 1: Use commas to separate classification directories
List the category directories (when there are more than one category directory) with commas as the separator.

The code is as follows: Copy code

<P> This post is in: <? Php the_category (',');?> </P>

Example 2: use arrows to separate classification directories
Lists the category directories. The Arrow (>) is used as the separator. (Please note that some visitors may classify> classify this format.

Read as a category with a parent-child relationship .)

The code is as follows: Copy code

<P> Categories: <? Php the_category ('& gt;') ;?> </P>

4. Display or return the category directory title of the current page
Here we need to use the function: single_cat_title ()
For pages (such as "/tag/carceer") that display WordPress tags rather than Classification Directory titles, this tag displays or returns the tag name

. This tag can only be used outside the WordPress main loop.
The basic usage is as follows:

The code is as follows: Copy code

<? Php single_cat_title ($ prefix, $ display);?>

The default usage is as follows:

The code is as follows: Copy code

<? Php single_cat_title ('prefix' =>, 'display' => true);?>

The following describes the parameters:
$ Prefix indicates the text before the title. Default value: None; Type: string; optional
$ Display displays the title (TRUE) or returns the title for use in PHP (FALSE ). Default value: TRUE. The type is Boolean. Optional.
The following example shows the title of the category directory with the words "current Category:
Example:

<P> <? Php single_cat_title ('Current category directory is ');?> </P>
// Output: the current category directory is wordpress.

In the following example, the title of the current category directory is assigned to the variable $ current_category for use in PHP.
The sample code is as follows:

<? Php $ current_category = single_cat_title ("", false);?>

5. Return the description entered by the user in the current category directory.
Here, we need to use the function: category_description ()
The basic usage of this function is as follows:

The code is as follows: Copy code

<? Php echo category_description ($ category);?>

The parameters are described as follows:
Parameter: the numeric ID of the category directory where $ category is returned. If no category directory is specified, the current category is used by default.

. Integer type
Example:

The code is as follows: Copy code

<Div> <? Php echo category_description (3);?> </Div>
// Output: WordPress is a favorite blogging tool of mine and I share tips and tricks

Using WordPress here.
<Div> <strong> <? Php single_cat_title ('Current category directory');?>
</Strong>: <? Php echo category_description ();?> </Div>
// Output:

Current category directory WordPress: WordPress is a favorite blogging tool of mine and I

Share tips and tricks for using WordPress here.


Usage of the classification directory function wp_list_categories

Purpose: obtain the category directory based on conditions and format the directory based on parameters.

Usage: <? Php wp_list_categories ($ args);?>

Default value:

The code is as follows: Copy code

$ Defaults = array (
'Show _ option_all '=> ", // do not list category links
'Orderby' => 'name', // Sort by category name
'Order' => 'asc ', // sort in ascending order
'Show _ last_update '=> 0, // do not display the latest timestamp of logs in the category
'Style' => 'list', // The category is displayed in the list.
'Show _ count' => 0, // The number of category logs is not displayed
'Hide _ empty' => 1, // no log category is displayed
'Use _ desc_for_title '=> 1, // display the category description
'Child _ of '=> 0, // subcategory is not restricted
'Feed' => ", // do not display the feed
'Feed _ image' => ", // do not display the feed image
'Clude' => ", // This Category is not displayed
'Hierarchical '=> true, // displays the parent/subcategory in different levels
'Title _ Li' => _ ('Categories '), // use "Categories" as the title of the current category list
'Echo '=> 1, // Display (echos) category
'Depth '=> 0 // unlimited list depth
);

The most common examples of a topic (get all classification directories, sort by name, get 3 depth, and format the title as None ):

This example is often used in the drop-down menu and requires relevant css and js support. If no css is available, the format problem will occur, such as all subdirectories.

Stack rather than drop-down, but the function is OK.

The code is as follows: Copy code

<Ul id = "nav">
<Li> <a href = "<? Php echo get_settings ('Home');?> /"Title =" <? Php

Bloginfo ('Description');?> "> Home </a> </li>
<? Php wp_list_categories ('sort _ column = name & title_li = & depth = 3 & prime;);?>
</Ul>

Sort by category name and display only the categories with IDs 1, 2, 3, and 4:

The code is as follows: Copy code

<? Php
Wp_list_categories ('orderby = name & include = 1, 2, 3, 4 & prime;);?>

Sort by category name, and display the number of logs for each category, but do not display the category with ID 10

The code is as follows: Copy code

<? Php
Wp_list_categories ('orderby = name & show_count = 1 & exclude = 10 & prime;);?>


Returns the root category id of the current category.

First: for the category page, the system has a default variable $ cat, which is the ID of the current category. This is more accurate.

Type 2: applicable to a single page

The code is as follows: Copy code

$ Cat = single_cat_title ('', false );
Echo get_cat_ID ($ cat );

Third:

The code is as follows: Copy code

If (! Is_page ()&&! Is_home () {$ catsy = get_the_category (); $ myCat = $ catsy [0]-> cat_ID;

$ Currentcategory = '& current_category ='. $ myCat;} wp_list_categories

('Hierarchical = 1 & use_desc_for_title = 0 & exclude = 12 & depth = 1 & orderby = id & title_li = '. $ currentcate

Gory );


Get category list

Directly obtain a clean category directory without using SQL statements

The code is as follows: Copy code
Echo "<span> <select name = 'list'> ";
Foreach (get_all_category_ids () as $ v)
{
$ Cat_info = get_category ($ v );
Echo "<option value = '". $ v. "'>". $ cat_info-> name. "</option> ";
}
Echo '</select> </span> ';

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.