WordPress Code Call Method and Skills

Time:2019-9-10

WordPress template basic file

Copy code

The code is as follows:

Style.css style sheet file
Index.php Home Page File
Single.php log single page file
Page.php Page File
Archvie.php Classification and Date Archive Page File
Searchform.php Searches Form Files
Search. PHP Search Page Files
Comments. PHP Message Area File (including Message List and Message Box)
404. PHP 404 error page
Header.php Page Header File
Side bar. PHP page sidebar file
Footer.php page bottom file
WordPress Header Header Header PHP Code

Note: This is the PHP code between <head> and </head>.

Copy code

The code is as follows:

<? PHP bloginfo (‘name’); > website title
<? PHP wp_title ();?> log or page title
<? PHP bloginfo (‘stylesheet_url’); WordPress Theme Style Sheet File Style. CSS Relative Address
<? PHP bloginfo (‘pingback_url’); WordPress Blog’s Pingback address
<? PHP bloginfo (‘template_url’);> Relative address of WordPress theme file
<? PHP bloginfo (‘version’); > WordPress version of Blog
<? PHP bloginfo (‘atom_url’);> Atom address of WordPress blog
<? PHP bloginfo (‘rss2_url’);> RS2 address of WordPress blog
<? PHP bloginfo (‘url’); > absolute address of WordPress blog
<? PHP bloginfo (‘name’); WordPress blog name
<? PHP bloginfo (‘html_type’); > HTML version of the website
<? PHP bloginfo (‘charset’); > Character encoding format of website

WordPress Body Template PHP Code

Copy code

The code is as follows:

<? PHP the_content ();?> log content
<? PHP if (have_posts():?> Confirm if there is a log
<? PHP while (have_posts ()): the_post (); > If so, display all logs
<? PHP endwhile;?> End the PHP function “while”
<? PHP endif;?> End the PHP function “if”
<? PHP get_header();?> content of header.php file
<? PHP get_sidebar();?> content of sidebar.php file
<? PHP get_footer();?> content of footer.php file
<? PHP the_time (‘m-d-y’)?> Date in the format “02-19-08”
<? PHP comments_popup_link();?> Displays a message link to a log
<? PHP the_title ();?> Displays the title of a log or page
<? PHP the_permalink()?> Displays a permanent link/URL address for a log or page
<? PHP the_category (‘,’)?> Displays the classification of a log or page
<? PHP the_author ();?> Displays the author of a log or page
<? PHP the_ID ();?> Displays the ID of a log or page
<? PHP edit_post_link();?> Displays an edit link for a log or page.
<? PHP get_links_list();?> Displays links in Blogroll
<? PHP comments_template(); > comments.php file content
<? PHP wp_list_pages();?> Displays a list of blog pages
<? PHP wp_list_cats();?> Displays a list of blog categories
<? PHP next_post_link (‘% link’)?> URL address for the next log
<? PHP previous_post_link(‘% link’)?> URL address of previous log
<? PHP get_calendar();?> Call calendar
<? PHP wp_get_archives()?> Displays a list of date archives for Blogs
<? PHP posts_nav_link();?> Displays newer log links (previous page) and older log links (next page)
<? PHP bloginfo (‘description’);?> Displays the description information of the blog

Some other WordPress template code

Copy code

The code is as follows:

/% Postname%/Display custom permanent links to blogs
<? PHP the_search_query();?> the value of the search form
<? Php_e(‘Message’);?> Print Output Information
<? PHP wp_register ();?> Displays registration links
<? PHP wp_loginout();?> Displays login/login links
<! — next page –> Insert pagination in log or page
<! – more -> Truncate logs
<? PHP wp_meta();?> Displays the administrator’s related control information
<? PHP timer_stop (1);?> Displays the time when the page was loaded
<? PHP echo get_num_queries();?> Display load page queries

1. WordPress calls to the latest WordPress articles can be implemented using a simple template tag wp_get_archvies. The code is as follows:

Copy code

The code is as follows:

<?php get_archives(‘postbypost’, 10); ?>
(Displays 10 latest updates) or</p>

The following code shows the latest 20 articles in your blog, format = custom, which is mainly used to customize the display style of this list of articles. Specific parameters and methods of use can be referred to the official instructions – wp_get_archvies. (fromat = custom can also be avoided, default to UL list to display the title of the article. )

Supplement: The latest article list can also be invoked through the query_posts() function of WP. Although the code will be a little more, it can better control the display of Loop, for example, you can set whether to display summaries or not. Specific methods of use can also be seen in the official instructions.

2. WordPress calls random articles

Copy code

The code is as follows:

<?php
$rand_posts = get_posts(‘numberposts=10&orderby=rand’);
foreach( $rand_posts as $post ) :
?>
<! – Here’s the Loop you want to customize
<li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
<?php endforeach; ?>

3. WordPress calls the latest message. Below is the latest message code I used to represent in a WordPress topic. I can’t remember which topic. This code directly calls the database to display a latest message. LIMIT 10 limits the number of messages displayed. The green part is the output style of each message.

Copy code

The code is as follows:

<?php
global $wpdb;
$sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,30) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = ‘1’ AND comment_type = ” AND
post_password = ”
ORDER BY comment_date_gmt DESC
LIMIT 10″;
$comments = $wpdb->get_results($sql);
$output = $pre_HTML; foreach ($comments as $comment) {
$output .= “n<li>”.strip_tags($comment->comment_author)
.”:” . ” <a href=”” . get_permalink($comment->ID) .
“#comment-” . $comment->comment_ID . “” title=”on ” .
$comment->post_title . “”>” . strip_tags($comment->com_excerpt)
.”</a></li>”;
} $output .= $post_HTML;
echo $output;?>

4. WordPress calls related articles

Display related articles on the article page

Copy code

The code is as follows:

<?php</p>
<p>$tags = wp_get_post_tags($post->ID);</p>
<p>if ($tags) {</p>
<p>$first_tag = $tags[0]->term_id;</p>
<p>$args=array(</p>
<p>’tag__in’ => array($first_tag),</p>
<p>’post__not_in’ => array($post->ID),</p>
<p>’showposts’=>10,</p>
<p>’caller_get_posts’=>1</p>
<p>);</p>
<p>$my_query = new WP_Query($args);</p>
<p>if( $my_query->have_posts() ) {</p>
<p>while ($my_query->have_posts()) : $my_query->the_post(); ?></p>
<p><li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title();?> <?php comments_number(‘ ‘,'(1)’,'(%)’); ?></a></li></p>
<p><?php</p>
<p>endwhile;</p>
<p>}</p>
<p>}</p>
<p>wp_reset_query();</p>
<p>?>

5. WordPress calls articles that specify categories

Copy code

The code is as follows:

<?php $posts = get_posts( “category=4&numberposts=10” ); ?>
<?php if( $posts ) : ?>
<ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li>
<a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

6. WordPress’s comment output linked to commenters

Copy code

The code is as follows:

<?php</p>
<p>global $wpdb;</p>
<p>$sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID,</p>
<p>comment_post_ID, comment_author, comment_date_gmt, comment_approved,</p>
<p>comment_type,comment_author_url,</p>
<p>SUBSTRING(comment_content,1,14) AS com_excerpt</p>
<p>FROM $wpdb->comments</p>
<p>LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =</p>
<p>$wpdb->posts.ID)</p>
<p>WHERE comment_approved = ‘1’ AND comment_type = ” AND</p>
<p>post_password = ”</p>
<p>ORDER BY comment_date_gmt DESC</p>
<p>LIMIT 10″;</p>
<p>$comments = $wpdb->get_results($sql);</p>
<p>$output = $pre_HTML;</p>
<p>foreach ($comments as $comment) {</p>
<p>$output .= “\n<li>”.strip_tags($comment->comment_author)</p>
<p>.”:” . ” <a href=\”” . get_permalink($comment->ID) .</p>
<p>”#comment-” . $comment->comment_ID . “\” title=\”on ” .</p>
<p>$comment->post_title . “\”>” . strip_tags($comment->com_excerpt)</p>
<p>.”</a></li>”;</p>
<p>}</p>
<p>$output .= $post_HTML;</p>
<p>echo $output;?>

7. WordPress calls comment output with gravatar Avatar

Copy code

The code is as follows:

<?Php</p>

The above code changes the comment_author value to your ID, 18 is the size of the avatar, and 10 is the number of comments.

8. WordPress Call Website Statistics

Copy code

The code is as follows:

1. Total number of logs:</p>
2. Number of drafts:</p>
3. Total number of comments:</p>
4. Establishment time:</p>
5. Total number of labels:</p>
6. Total number of pages:</p>
7. Total number of classifications:</p>
8. Total number of links:</p>
9. Total number of users:</p>
10. Last update:</p>

9. WordPress Judgment Statement

is_single()
Determine whether it’s a page for a specific article
is_single(’2′)
Determine whether it’s a page for a specific article (id = 2)
is_single(’Beef Stew’)
Determine whether it’s a page for a specific article (headline judgement)
is_single(’beef-stew’)
Judging whether it’s a page for a specific article (slug judgment)
comments_open()
Whether to leave a message to open
pings_open()
Whether to open Ping or not
is_page()
Is it a page?
is_page(’42′)
ID judgment, that is, whether it is a page with ID 42
is_page(’About Me’)
Judging Title
is_page(’about-me’)
Slug judgment
is_category()
Is it a classification?
is_category(’6′)
ID judgment, i.e. classification with ID 6
is_category(’Cheeses’)
Classification Title Judgment
is_category(’cheeses’)
Classified slug judgment
in_category(’5′)
Judging whether the current article belongs to Category 5
is_author()
Display all authors’pages
is_author(’1337′)
Page showing author number 1337
is_author(’Elite Hacker’)
Display the current author’s page by nickname
is_author(’elite-hacker’)
Following is a different judgment to achieve the year, month, day, time and other ways to display the archive.
is_date()
is_year()
is_month()
is_day()
is_time()
Determine whether the current archive page is
is_archive()
Determine whether it’s a search
is_search()
Determine whether the page is 404
is_404()
Judging whether to turn pages or not, for example, when your current blog is http://domain.com showing http://domain.com? Paged=2, this judgment will return true. This function can be used with is_home to control some interfaces that can only be displayed on the home page.
For example:

Copy code

The code is as follows:

<? PHP if (is_single():?>.</p>
Or:
<? PHP if (is_home () & & is_paged ():?>.</p>

10. WordPress non-plug-in synchronous twitter

Copy code

The code is as follows:

<?Php</p>

The agting in the code is changed to your twitter user name, and Zheng Yong is changed to your name. Another method of calling requires your space to be a foreign host:

Copy code

The code is as follows:

<?php</p>
<p>// Your twitter username.</p>
<p>$username = “wange1228″;</p>
<p>// Prefix – some text you want displayed before your latest tweet.</p>
<p>// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\”link.html\”)</p>
<p>// Suffix – some text you want display after your latest tweet. (Same rules as the prefix.)</p>
<p>$suffix = “”;</p>
<p>$feed = “<a href=”http://search.twitter.com/search.atom?q=from” rel=”external nofollow” >http://search.twitter.com/search.atom?q=from</a>:” . $username . “&rpp=1”;</p>
<p>function parse_feed($feed) {</p>
<p>$stepOne = explode(“<content type=\”html\”>”, $feed);</p>
<p>$stepTwo = explode(“</content>”, $stepOne[1]);</p>
<p>$tweet = $stepTwo[0];</p>
<p>$tweet = str_replace(“<“, “<“, $tweet);</p>
<p>$tweet = str_replace(“>”, “>”, $tweet);</p>
<p>return $tweet;</p>
<p>}</p>
<p>$twitterFeed = file_get_contents($feed);</p>
<p>echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);</p>
<p>?>

ZeroZ summarizes the characteristics of this method:

1. Non-plug-in!

2. There is no need to verify the username and password, that is to say, you can specify to call anyone’s tweet!

3. You can customize the text displayed after tweet information, that is, $suffix=”; here!

4. Only the latest tweet can be invoked to meet my needs.

5. Probably only foreign space can be used! (I have verified that this is true.)

11. WordPress non-plug-in call comment expression

Copy code

The code is as follows:

<!–smilies–>
<?php</p>
<p>function wp_smilies() {</p>
<p>global $wpsmiliestrans;</p>
<p>if ( !get_option(‘use_smilies’) or (empty($wpsmiliestrans))) return;</p>
<p>$smilies = array_unique($wpsmiliestrans);</p>
<p>$link=”;</p>
<p>foreach ($smilies as $key => $smile) {</p>
<p>$file = get_bloginfo(‘wpurl’).’/wp-includes/images/smilies/’.$smile;</p>
<p>$value = ” “.$key.” “;</p>
<p>$img = “<img src=\”{$file}\” alt=\”{$smile}\” />”;</p>
<p>$imglink = htmlspecialchars($img);</p>
<p>$link .= “<a href=\”#commentform\” title=\”{$smile}\” onclick=\”document.getElementByIdx_x(‘comment’).value += ‘{$value}’\”>{$img}</a> “;</p>
<p>}</p>
<p>echo ‘<div>’.$link.'</div>’;</p>
<p>}</p>
<p>?></p>
<p><?php wp_smilies();?></p>
<p><!–smilies—>

Copy the above code to the appropriate location in comments. php.