wordpress插件
A WordPress Post Series plugin enables you to organize your posts serially to create a book or a course. It provides users a path for learning. Posts series plugins can also be used to split a long post into multiple parts.
WordPress Post Series插件使您可以按顺序组织帖子以创建一本书或一门课程。 它为用户提供了学习的途径。 帖子系列插件还可用于将长帖子分成多个部分。
In this tutorial, I’ll show you how to create a plugin for displaying a series of posts. You can also integrate the same code into a theme, as theme functionality.
在本教程中,我将向您展示如何创建一个用于显示一系列帖子的插件。 您还可以将相同的代码作为主题功能集成到主题中。
In WordPress, taxonomies are used to group or organize similar posts together. But WordPress doesn’t provide a way to display all the posts of a particular taxonomy in a customized, serial manner. WordPress taxonomies are displayed using an archive.php file, so we cannot create a post series as a single, indexable post.
在WordPress中,分类法用于将相似的帖子分组或组织在一起。 但是WordPress没有提供一种以自定义,串行方式显示特定分类法的所有帖子的方法。 WordPress分类法是使用archive.php文件显示的,因此我们不能将帖子系列创建为单个可索引的帖子。
So we need a post series, which is actually one post that contains other posts in a serial manner.
因此,我们需要一个帖子系列,它实际上是一个帖子,其中包含一系列按顺序排列的其他帖子。
There are many different ways to create a post series. Popular post series plugins found at WordPress.org use custom taxonomies on WordPress posts to create a post series, but in this tutorial I’ll use Custom Post Types instead.
创建帖子系列有很多不同的方法。 在WordPress.org上找到的流行的帖子系列插件使用WordPress帖子上的自定义分类法来创建帖子系列,但是在本教程中,我将改用“自定义帖子类型”。
Create a plugin directory named sitepoint-post-series and place two files in this, named sitepoint-post-series.php and sitepoint-post-series.css.
创建插件目录名为sitepoint-post-series和地方两个文件在此,故名sitepoint-post-series.php和sitepoint-post-series.css 。
In the sitepoint-post-series.php file, place the code below, so that WordPress recognizes the directory as a plugin and lets you install it.
在sitepoint-post-series.php文件中,将代码放在下面,以便WordPress将目录识别为插件并允许您安装它。
<?php /* Plugin Name: SitePoint Post Series Plugin URI: https://www.sitepoint.com/ Description: This used is used to create a post series. Version: 1.0 Author: Narayan Prusty */You can also add post series functionality to a theme. In this case, you will need to place all the code referred to in this tutorial, in the theme’s functions.php file.
您还可以将主题系列后继功能添加。 在这种情况下,您需要将本教程中引用的所有代码放在主题的functions.php文件中。
First, we need to create a custom post type, where each custom post type represents a post series.
首先,我们需要创建一个自定义帖子类型,其中每个自定义帖子类型都代表一个帖子系列。
Place the code below in a file called sitepoint-post-series.php:
将下面的代码放在一个名为sitepoint-post-series.php的文件中:
function sitepoint_post_series_custom_post_type() { register_post_type("sitepoint-postseries", array( "labels" => array("name" => __("Post Series"), "singular_name" => __("Post Series")), "public" => true, "has_archive" => true, "rewrite" => array("slug"=> "post-series"), "supports" => array("editor", "title", "excerpt", "thumbnail", "comments"), "capability_type" => "post", "publicly_queryable" => true, "taxonomies" => array("category", "post_tag"), ) ); } add_action("init", "sitepoint_post_series_custom_post_type", 2); /* Flush Rewrite Rules */ function sitepoint_post_series_activation() { sitepoint_post_series_custom_post_type(); flush_rewrite_rules(); } register_activation_hook( __FILE__, "sitepoint_post_series_activation"); register_deactivation_hook( __FILE__, "sitepoint_post_series_activation");Here, we created a custom post type with the same taxonomies that are used by WordPress posts. This is so that you can create a category post series too.
在这里,我们创建了一个自定义帖子类型,其类型与WordPress帖子使用的分类法相同。 这样一来,您也可以创建类别帖子系列。
We also added activation and deactivation hooks to flush rewrite rules. This is so that the post series can be viewed on the front end.
我们还添加了激活和停用钩子来刷新重写规则。 这样便可以在前端查看文章系列。
Here is what our custom post type looks on the admin screen:
这是我们的自定义帖子类型在管理屏幕上显示的内容:
Now we need to add meta boxes to the WordPress Posts admin interface. This is so that authors can attach a post to a post series, and provide a serial number to sort the posts inside a post series.
现在,我们需要向WordPress帖子管理界面中添加元框。 这样作者可以将帖子附加到帖子系列中,并提供序列号以对帖子系列中的帖子进行排序。
Here is the code to add a meta box to post series:
这是将元框添加到帖子系列的代码:
/* Add Custom Meta Boxes in WordPress Posts */ function sitepoint_post_series_meta_box_markup($object) { wp_nonce_field(basename(__FILE__), "sitepoint-postseries"); ?> <div> <label for="sitepoint-postseries-serial-number">Serial Number</label> <br> <input name="sitepoint-postseries-serial-number" type="text" value="<?php echo get_post_meta($object->ID, "sitepoint-postseries-serial-number", true); ?>"> <br> <label for="sitepoint-postseries-id">Name</label> <br> <select name="sitepoint-postseries-id"> <option value="">-</option> <?php $posts = get_posts("post_type=sitepoint-postseries"); $selected_series = get_post_meta($object->ID, "sitepoint-postseries-id", true); foreach($posts as $post) { $id_post = $post->ID; if($id_post == $selected_series) { ?> <option selected value="<?php echo $post->ID; ?>"><?php echo $post->post_title; ?></option> <?php } else { ?> <option value="<?php echo $post->ID; ?>"><?php echo $post->post_title; ?></option> <?php } } ?> </select> </div> <?php } function sitepoint_post_series_custom_meta_box() { add_meta_box("sitepoint-postseries", "Post Series", "sitepoint_post_series_meta_box_markup", "post", "side", "low", null); } add_action("add_meta_boxes", "sitepoint_post_series_custom_meta_box");Here we add two fields to the meta box. The text field is used by the author to enter the serial number, and the drop down is used to select the post series name to which the post belongs to. If you don’t want to add a post to a post series, then either one or both fields should be left blank.
在这里,我们向meta框添加两个字段。 作者使用文本字段输入序列号,下拉列表用于选择帖子所属的帖子系列名称。 如果您不想将帖子添加到帖子系列中,则一个或两个字段都应留空。
Here is how it looks on the admin post screen:
这是在管理员发布屏幕上的外观:
Now we need to save the meta box fields when the form is saved. Here is the code to do that:
现在,我们需要在保存表单时保存元框字段。 这是执行此操作的代码:
/* Callback to Save Meta Data */ function sitepoint_post_series_save_custom_meta_box($post_id, $post, $update) { if(!isset($_POST["sitepoint-postseries"]) || !wp_verify_nonce($_POST["sitepoint-postseries"], basename(__FILE__))) return $post_id; if(!current_user_can("edit_post", $post_id)) return $post_id; if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE) return $post_id; $slug = "post"; if($slug != $post->post_type) return; $serial_number = null; if(isset($_POST["sitepoint-postseries-serial-number"])) { $serial_number = $_POST["sitepoint-postseries-serial-number"]; } else { $serial_number = ""; } update_post_meta($post_id, "sitepoint-postseries-serial-number", $serial_number); $series_id = null; if(isset($_POST["sitepoint-postseries-id"])) { $series_id = $_POST["sitepoint-postseries-id"]; } else { $series_id = ""; } $previous_series_id = get_post_meta($post_id, "sitepoint-postseries-id", true); update_post_meta($post_id, "sitepoint-postseries-id", $series_id); //no series, removing series, adding new series or changing series if($previous_series_id == "" && $series_id == "") { sitepoint_post_series_save_settings($series_id, $serial_number, $post_id); } else if($previous_series_id != "" && $series_id == "") { sitepoint_post_series_save_settings($previous_series_id, "", $post_id); } else if($previous_series_id == "" && $series_id != "") { sitepoint_post_series_save_settings($series_id, $serial_number, $post_id); } else if($previous_series_id != "" && $series_id != "") { sitepoint_post_series_save_settings($previous_series_id, "", $post_id); sitepoint_post_series_save_settings($series_id, $serial_number, $post_id); } } add_action("save_post", "sitepoint_post_series_save_custom_meta_box", 10, 3);Here we are saving the meta box content and then calling the function sitepoint_post_series_save_settings with different argument values depending on whether the user is removing a series, adding a series or changing a series.
在这里,我们要保存元框的内容,然后根据用户是删除系列,添加系列还是更改系列,使用不同的参数值调用函数sitepoint_post_series_save_settings 。
Here is the code for the sitepoint_post_series_save_settings function
这是sitepoint_post_series_save_settings函数的代码
/* Store WordPress posts and Post Series CTY relations as WordPress Settings. */ function sitepoint_post_series_save_settings($series_id, $serial_number, $post_id) { if($series_id != "" && $serial_number != "") { $post_series_list = get_option("post_series_" . $series_id . "_ids", ""); if($post_series_list == "") { $post_series_list_array = array($post_id); $post_series_list = implode (", ", $post_series_list_array); update_option("post_series_" . $series_id . "_ids", $post_series_list); } else { $post_series_list_array = explode(',', $post_series_list); if(in_array($post_id, $post_series_list_array)) { //do nothing } else { $post_series_list_array[] = $post_id; $post_series_list = implode (", ", $post_series_list_array); update_option("post_series_" . $series_id . "_ids", $post_series_list); } } } else if($series_id == "" || $serial_number == "") { $post_series_list = get_option("post_series_" . $series_id . "_ids", ""); if($post_series_list == "") { } else { $post_series_list_array = explode(',', $post_series_list); if(in_array($post_id, $post_series_list_array)) { //here remove the post id from array. if(($key = array_search($post_id, $post_series_list_array)) !== false) { unset($post_series_list_array[$key]); } $post_series_list = implode (", ", $post_series_list_array); update_option("post_series_" . $series_id . "_ids", $post_series_list); } else { } } } }This function creates a string, which stores the WordPress post ID’s that belong to a particular series. And then it stores the strings as a WordPress setting.
此函数创建一个字符串,该字符串存储属于特定系列的WordPress帖子ID。 然后将字符串存储为WordPress设置。
Now we’re done with all the admin area code. You should now be able to create posts and assign them to a series. And also assign categories and tags to each series.
现在,我们完成了所有的管理区号。 现在,您应该能够创建帖子并将其分配给系列。 并为每个系列分配类别和标签。
Now let’s code the front end to display the post series.
现在,让我们对前端进行编码以显示帖子系列。
The custom post type is not yet visible in the index and archive pages. To make it visible on these pages as well, you just need to add the code below:
自定义帖子类型在索引和存档页面中尚不可见。 要使其在这些页面上也可见,您只需添加以下代码:
/* Displaying Custom Post Types on Index Page */ function sitepoint_post_series_pre_posts($q) { if(is_admin() || !$q->is_main_query() || is_page()) return; $q->set("post_type", array("post", "sitepoint-postseries")); } add_action("pre_get_posts", "sitepoint_post_series_pre_posts");Here we’re using pre_get_posts hook to add a post series to the $q variable, which is used by the main loop to displays posts.
在这里,我们使用pre_get_posts钩子将帖子系列添加到$q变量中,主循环使用它来显示帖子。
We need to filter the content of the post series type and add posts belonging to the series.
我们需要过滤帖子系列类型的内容,并添加属于该系列的帖子。
Here is the code to add posts of a post series in a post series page.
这是在帖子系列页面中添加帖子系列的帖子的代码。
function sitepoint_post_series_content_filter($content) { $slug = "sitepoint-postseries"; if($slug != get_post_type()) return $content; $post_series_list = get_option("post_series_" . get_the_ID() . "_ids", ""); $post_series_list_array = explode(',', $post_series_list); $post_series_serial_number = array(); foreach($post_series_list_array as $key => $value) { $serial_number = get_post_meta($value, "sitepoint-postseries-serial-number", true); $post_series_serial_number[$value] = $serial_number; } asort($post_series_serial_number); $html = "<ul class='sitepoint-post-series'>"; foreach($post_series_serial_number as $key => $value) { $post = get_post($key); $title = $post->post_title; $excerpt = $post->post_content; $shortcode_pattern = get_shortcode_regex(); $excerpt = preg_replace('/' . $shortcode_pattern . '/', '', $excerpt); $excerpt = strip_tags($excerpt); $excerpt = esc_attr(substr($excerpt, 0, 150)); $img = ""; if(has_post_thumbnail($key)) { $temp = wp_get_attachment_image_src(get_post_thumbnail_id($key), array(150, 150)); $img = $temp[0]; } else { $img = "https://lorempixel.com/150/150/abstract"; } $html = $html . "<li><h3><a href='" . get_permalink($key) . "'>" . $title . "</a></h3><div><div class='sitepoint-post-series-box1'><img src='" . $img . "' /></div><div class='sitepoint-post-series-box2'><p>" . $excerpt . " ...</p></div></div><div class='clear'></div></li>"; } $html = $html . "</ul>"; return $content . $html; } add_filter("the_content", "sitepoint_post_series_content_filter");This displays the posts using HTML unordered list tag. For posts without an image we are loading a image from Lorempixel cloud service to generate random texture images.
这将使用HTML无序列表标记显示帖子。 对于没有图像的帖子,我们正在从Lorempixel云服务加载图像以生成随机纹理图像。
We are retrieving the posts of a post series from the setting string, which we saved during the saving of meta data.
我们正在从设置字符串中检索系列文章,这些设置字符串是在保存元数据期间保存的。
We can also add a post series box on posts that belong to a post series to indicate to the user that the post belongs to a specific posts series. Here’s the code to do that:
我们还可以在属于某个帖子系列的帖子上添加一个帖子系列框,以向用户指示该帖子属于特定的帖子系列。 这是执行此操作的代码:
/* Adding Content to WordPress Posts which belong to a Series */ function sitepoint_post_series_post_content_filter($content) { $slug = "post"; if($slug != get_post_type()) return $content; $serial_number = get_post_meta(get_the_ID(), "sitepoint-postseries-serial-number", true); $series_id = get_post_meta(get_the_ID(), "sitepoint-postseries-id", true); if(get_post_status($series_id) == "publish") { $html = ""; if($series_id != "" || $serial_number != "") { $html = "<div class='sitepoint-post-series-post-content'><div>This post is a part " . $serial_number . " of <a href='" . get_permalink($series_id) . "'>" . get_the_title($series_id) . "</a> post series.</div></div>"; } $content = $html . $content; } return $content; }Here we are just displaying a post series name and which part of this post is from the series.
在这里,我们仅显示帖子系列的名称,而帖子的哪一部分来自该系列。
You can also add the next and previous post of the series by using the below implementation of the sitepoint_post_series_post_content_filter function:
您还可以使用sitepoint_post_series_post_content_filter函数的以下实现来添加该系列的下一篇和上一篇文章:
function sitepoint_post_series_post_content_filter($content) { $slug = "post"; if($slug != get_post_type()) return $content; $serial_number = get_post_meta(get_the_ID(), "sitepoint-postseries-serial-number", true); $series_id = get_post_meta(get_the_ID(), "sitepoint-postseries-id", true); if($serial_number != "" && $series_id != "") { //find next and previous post too. $post_series_list = get_option("post_series_" . $series_id . "_ids", ""); $post_series_list_array = explode(',', $post_series_list); $post_series_serial_number = array(); foreach($post_series_list_array as $key => $value) { $serial_number = get_post_meta($value, "sitepoint-postseries-serial-number", true); $post_series_serial_number[$value] = $serial_number; } asort($post_series_serial_number); $post_series_serial_number_reverse = array(); $iii = 1; foreach($post_series_serial_number as $key => $value) { $post_series_serial_number_reverse[$iii] = $key; $iii++; } $index = array_search(get_the_ID(), $post_series_serial_number_reverse); if($index == 1) { $html = "<div class='sitepoint-post-series-post-content'><div>This post is a part of <a href='" . get_permalink($series_id) . "'>" . get_the_title($series_id) . "</a> post series.</div><div>⎘ Next: <a href='" . get_permalink($post_series_serial_number_reverse[$index + 1]) . "'>" . get_the_title($post_series_serial_number_reverse[$index + 1]) . "</a></div></div>"; $content = $html . $content; } else if($index > 1 && $index < sizeof($post_series_serial_number_reverse)) { $html = "<div class='sitepoint-post-series-post-content'><div>This post is a part of <a href='" . get_permalink($series_id) . "'>" . get_the_title($series_id) . "</a> post series.</div><div>⎘ Next post in the series is <a href='" . get_permalink($post_series_serial_number_reverse[$index + 1]) . "'>" . get_the_title($post_series_serial_number_reverse[$index + 1]) . "</a></div><div>⎗ Previous post in the series is <a href='" . get_permalink($post_series_serial_number_reverse[$index - 1]) . "'>" . get_the_title($post_series_serial_number_reverse[$index - 1]) . "</a></div></div>"; $content = $html . $content; } else if($index == sizeof($post_series_serial_number_reverse)) { $html = "<div class='sitepoint-post-series-post-content'><div>This post is a part of <a href='" . get_permalink($series_id) . "'>" . get_the_title($series_id) . "</a> post series.</div><div>⎗ Previous: <a href='" . get_permalink($post_series_serial_number_reverse[$index - 1]) . "'>" . get_the_title($post_series_serial_number_reverse[$index - 1]) . "</a></div></div>"; $content = $html . $content; } } return $content; }The problem with this implementation is that the code hits the MySQL number of times equal to the number of posts in the series. There is a performance issue if you have a lot of posts for a particular post series, but I’ve included it for educational purposes.
这种实现方式的问题在于,代码到达MySQL的次数等于系列中的帖子数。 如果您对某个特定的帖子系列有很多帖子,则会出现性能问题,但是出于教育目的,我将其包括在内。
Here are some of the plugins on WordPress.org that enable you to create a post series. I have compared each with the plugin we’ve created above.
这是WordPress.org上的一些插件,可让您创建帖子系列。 我已经将它们与我们上面创建的插件进行了比较。
Organize Series: Organize Series adds a custom taxonomy to WordPress posts. When you view the custom taxonomy it adds a heading to the archive page, which seems like the title of the post series. And posts in a post series are displayed like taxonomies, which may not be great from a user perspective. In comparison, our plugin uses custom post types to display a single post series so that you can add featured images, text or markup content (and more) to a post series. The posts of a post series look different compared to the archive page in our plugin.
整理系列 :整理系列为WordPress帖子添加了自定义分类法。 当您查看自定义分类法时,它会在存档页面中添加一个标题,看起来像该系列文章的标题。 帖子系列中的帖子就像分类法一样显示,从用户的角度来看可能不太好。 相比之下,我们的插件使用自定义帖子类型显示单个帖子系列,因此您可以将特色图片,文本或标记内容(以及更多)添加到帖子系列中。 与我们插件中的存档页面相比,帖子系列的帖子看起来有所不同。
WP Post Series: This also behaves like the Organize Series plugin. One difference is that it doesn’t provide any customization to the post series page.
WP Post Series :这也类似于Organize Series插件。 一个区别是,它不对帖子系列页面提供任何自定义。
Our plugin is highly customizable and doesn’t include any WordPress hacks. As such, it is much more compatible.
我们的插件是高度可定制的,并且不包含任何WordPress hacks。 因此,它更加兼容。
If you own a development blog, then you could use this plugin to create your own post series, which can increase your engagement and conversion rates. You can even use it to split up your large posts into multiple posts.
如果您拥有开发博客,则可以使用此插件创建自己的帖子系列,以提高参与度和转化率。 您甚至可以使用它将大型帖子拆分为多个帖子。
Feel free to comment on your experiences with the plugin below.
请随意评论以下插件的使用经验。
翻译自: https://www.sitepoint.com/creating-a-post-series-plugin-for-wordpress/
wordpress插件