background preloader

Wordpress

Facebook Twitter

Don’t Be Shy to Use sprintf with WordPress. Don’t be shy to use the printf and sprintf functions with WordPress.

Don’t Be Shy to Use sprintf with WordPress

It makes code much easier to read. Take a look at the following examples. echo '<a href="' . get_permalink() . '" class="link">' . get_the_title() . '</a>'; It looks quite dirty and it’s very easy to miss a quote or double-quote. Printf( '<a href="%s" class="link">%s</a>', get_permalink(), get_the_title() ); And here’s a slightly less clean, but more secure example: printf( '<a href="%s" class="link">%s</a>', esc_url( get_permalink() ), esc_html( get_the_title() ) ); You might think escaping the permalink and the post title is not necessary, and you’re right.

$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a>', esc_url( get_permalink() ), esc_attr( get_the_time() ), esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ) ); Thanks for reading and have a great day! Pages. Pages Languages: English • Español • Français • Italiano • 日本語 • 한국어 • Slovenčina • ไทย • 中文(简体) • 中文(繁體) • Русский • Português do Brasil • (Add your language) In WordPress, you can write either posts or Pages.

Pages

When you're writing a regular blog entry, you write a post. Posts, in a default setup, appear in reverse chronological order on your blog's home page. Pages are for content such as "About," "Contact," etc. In addition to the generally required "About" and "Contact" Pages, other examples of common pages include Copyright, Disclosure, Legal Information, Reprint Permissions, Company Information, and Accessibility Statement.

In general, Pages are very similar to Posts in that they both have Titles and Content and can use your WordPress Theme templates files to maintain a consistent look throughout your site. What Pages Are: Pages are for content that is less time-dependent than Posts. What Pages are Not: Pages are not Posts, nor are they excerpted from larger works of fiction. Support » How to get to Admin Panel? Function Reference/query posts. Languages: English • Italiano • 日本語 • 中文(简体) • Português do Brasil • (Add your language) Description Note: This function isn't meant to be used by plugins or themes.

Function Reference/query posts

As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose. query_posts() is a way to alter the main query that WordPress uses to display posts.

It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. For general post queries, use WP_Query or get_posts. Support » sql select posts matching multiple categories. Function Reference/is front page.