wordpress插件开发
It’s easy to show lists of posts in WordPress. Your theme’s index.php file is probably handling all your categories, authors and search requests. However, sometimes you need a little more control. Perhaps you have multiple product pages but only want to feature two or three on your home page without manually re-entering the details.
在WordPress中显示帖子列表很容易。 您主题的index.php文件可能正在处理您的所有类别,作者和搜索请求。 但是,有时您需要更多控制权。 也许您有多个产品页面,但只想在主页上显示两个或三个,而无需手动重新输入详细信息。
In this tutorial, we’ll develop a small WordPress plugin which replaces a [shortcode] with a link to a specific page or post. The link will show the title, thumbnail and excerpt, but you can configure it to show which ever details you require.
在本教程中,我们将开发一个小的WordPress插件,用指向特定页面或帖子的链接替换[shortcode]。 该链接将显示标题,缩略图和摘录,但您可以对其进行配置以显示所需的详细信息。
Create a new file named showexcerptlink.php in your plugins folder (wp-content/plugins) and add a header so it can be identified by WordPress:
在您的plugins文件夹(wp-content / plugins)中创建一个名为showexcerptlink.php的新文件,并添加标题,以便WordPress可以识别它:
<?php /* Plugin Name: Show Page/Post Excerpt Plugin URI: https://www.sitepoint.com/ Description: Replaces a shortcode with a link to a specific page or post Version: 1.0 Author: Craig Buckler Author URI: http://optimalworks.net/ License: Use this how you like! */This is followed by our primary function, ShowExcerptLink(). We require one parameter: the slug/permalink name. While we could pass the ID, the slug is a better option because it’s less likely to change while you’re editing pages. You could add further parameters to control the HTML output.
接下来是我们的主要功能ShowExcerptLink()。 我们需要一个参数:段/永久链接名称。 虽然我们可以传递ID,但最好使用Slug,因为在编辑页面时它不太可能更改。 您可以添加其他参数来控制HTML输出。
function ShowExcerptLink($params = array()) { extract(shortcode_atts(array( 'slug' => '' ), $params)); $html = ''; if ($slug == '') return $html;We can now lookup the page or post using the WordPress WP_Query object. The following code attempts to find the page slug but, if that doesn’t exist, it looks for a post slug instead:
现在,我们可以使用WordPress WP_Query对象查找页面或发布。 以下代码尝试查找页面提示,但如果不存在,则会查找帖子提示:
$q = new WP_Query("pagename=$slug"); if (!$q->have_posts()) { $q = new WP_Query("name=$slug"); }We can now start a WordPress loop — although it’ll only have zero or one post:
现在,我们可以开始WordPress循环-尽管只有零个或一个帖子:
// the loop while ($q->have_posts()) { $q->the_post();The HTML output is now generated in the $html string. You can use any standard function that you’d normally find in a WordPress loop. In this example, we’ll output the title (H2), thumbnail, and excerpt:
HTML输出现在在$ html字符串中生成。 您可以使用通常在WordPress循环中找到的任何标准功能。 在此示例中,我们将输出标题(H2),缩略图和摘录:
// generate HTML $link = '<a href="' . get_permalink() . '>'; $html .= '<h2>' . $link . the_title('','',false) . "</a></h2>n" . (has_post_thumbnail() ? $link . get_the_post_thumbnail() . '</a>' : '' ) . get_the_excerpt();(This code is valid in any version of HTML although HTML5 would permit the anchor around the whole block.)
(此代码在任何版本HTML中均有效,尽管HTML5允许在整个块周围定位。)
We can now end the loop, return the HTML string and complete the function:
现在,我们可以结束循环,返回HTML字符串并完成功能:
} return $html; }Finally, we’ll register our function as a shortcode handler:
最后,我们将函数注册为简码处理程序:
// register shortcode add_shortcode('showexcerptlink', 'ShowExcerptLink');Save the file and activate the plugin in your WordPress control panel.
保存文件并在WordPress控制面板中激活插件。
The following shortcode can now be added to the content of any page or post:
现在可以将以下短代码添加到任何页面或帖子的内容中:
[showexcerptlink slug=page-or-post-slug-name]If you’re referencing a page which has one or more parent pages, the slugs must be separated by a forward slash, e.g.
如果引用的页面包含一个或多个父页面,则这些段必须用正斜杠分隔,例如
[showexcerptlink slug=grand-parent-slug/parent-slug/page-slug]Please feel free to use and modify the code as you wish.
请随意使用和修改代码。
翻译自: https://www.sitepoint.com/develop-wordpress-custom-page-excerpt-plugin/
wordpress插件开发