Create a custom archives page in WordPress. Creating a ZIP Archive in PHP Consideration details archive php
It is clear that it is easier for template creators to display standard views of all pages of the site using standard functions and WordPress template tags, but this creates a consistent look and feel and the feeling of being navigated to the same pages on the site.
I’ll show right away what we get as a result.
Wordpress archives view: category archive before change
Archive of headings with removed thumbnails and a link in more detail.
Important! Since this task is solved by changing the template code, we do (database + site files) before work. In addition, we make two copies of the working template, one for editing, the other for restoring the wrong editing.
Changing the look of WordPress archives
To change the look of WordPress archives, you need to find, or rather define, which file in your working template displays the archives. In most templates, all archives are output as a single file, it is called (archive.php).
I repeat, for the safety of losing the site, we do not use the editor in the administrative panel of the site, but edit the previously made backup copies of the template files.
In a text editor (such as Notepad ++), open the archive.php file and start editing. In the archive.php file (at the end of the file), we are looking for a function that displays the archive's blog:
The name is the name of the file that is used to display the blog archives.
The first idea behind the task is simple: we need to change the code of the file outputting archives (content.php), namely, remove several functions in it, and thereby change the appearance of all site archives (categories, authors, dates, etc.).
But the question arises, if we change the code of the template file, it will return to its previous state after the first update of the template, we do not need this. Therefore, we will not edit the content.php file, but copy it and create our own file with a different name, for example, content-cat.php and edit it.
We are looking for a function that displays thumbnails in the file. The thumbnail output function will be at the top. We remove the display of the thumbnail.
orand remove the line with ‘Read More’, ‘template name’.
We save the created and edited content-cat.php file and upload it to the site directory in the working template folder. This file will appear in the administrative panel of the site on the Appearance → Editor tab.
Let's move on to the second step. In the file that displays the archives (archive.php), change the name of the content file to content-cat.
We save and see the result. If those are wrong, the system will show an error, an error file and an error line. To fix the error, we return the saved template backup files to their place and repeat it all over again.
Advice. If you want to read more about template tags and standard WordPress functions, check out this site: https://wp-kama.ru. This is not an advertisement or even a link, this site is clearer than the official WordPress site in the template and feature tags section.
In development of the topic
In my opinion, the WordPress site announcement theme needs to be continued. In the next posts, I'll talk about the topics: and.
WordPress Codex
Hidden text
The_post_thumbnail Function
Function
the_post_thumbnail
Appointment
The_post_thumbnail function displays the html code of the post thumbnail image, empty if there is no picture.
Application
This template tag, the_post_thumbnail function, must be used internally
Usage
the_post_thumbnail (string | array $ size = "post-thumbnail", string | array $ attr = "")A source
File: wp-includes / post-thumbnail-template.php
Function the_post_thumbnail ($ size = "post-thumbnail", $ attr = "") (echo get_the_post_thumbnail (null, $ size, $ attr);)
Parameters
$ size (string / array)
The size of the thumbnail to get. Can be a string with conditional dimensions: thumbnail, medium, large, full or an array of two elements (image width and height): array (60, 60).
Default: ‘Post-thumbnail’, that is, the size that is set for the current theme with the function set_post_thumbnail_size ()
$ attr (string / array)
An array of attributes to add to the resulting html img tag (alt is an alternative name).
Default:
Example
"title =" (! LANG:_("permalink"), the_title_attribute("echo=0")); ?>"> !}get ("layout", "imgwidth"), $ SMTheme-> get ("layout", "imgheight")), array ("class" => $ SMTheme-> get ("layout", "imgpos"). " featured_image ")); if (! is_single ()) (?>If I ask you which default page type in WordPress you use the least, then most likely your answer will be the archive template. Or, more likely, you haven't even heard of the archive template at all - it's so unpopular. The reason is simple. The standard way of using archives is far from being "user friendly".
Let's fix it today! Let's create a WordPress archives page that's really useful. The best part is that you will be able to use these archives in any modern WordPress theme installed on your site. However, let's first understand what we mean by "archives page"?
History of WordPress archives
In WordPress, you work with tons of different templates and building blocks that come with the default configuration. If we look at the list of Twenty Fifteen default theme directories, we can see the following:
- 404 error page
- Archives page (our guest today)
- Attached Images Page
- Index page (main page)
- Page template (for standard pages)
- Search results page
- Individual post and attachment pages
Despite their different purposes, these pages are all very similar in structure, and they often differ in only a few sections or a few lines of code. The only visible difference between an index page and an archive page is the extra heading at the top, which changes depending on the page you are viewing.
The idea behind the archive structure is to offer the blog administrator a convenient and easy way to display archives based on various criteria. After all, all of these various archive pages are just versions of the index page; they display content published in a specific time frame, a specific author, with specific tags and categories.
Sounds good when viewed from a programmer’s perspective, but it’s not the most user-friendly approach. In this case, one important level is missing - the level that lies between the user's intention to find content and individual elements of the archives.
This is what I mean. Currently, the only built-in way to display archived links on a WordPress site is via a widget. Thus, if you want to allow users to delve into archives, and you want to do this in a way that people can understand, you would have to dedicate the entire sidebar to one archives (just to reveal different types of structuring: archives by date, archives of categories, archives tags, copyright archives, etc.).
We need some kind of intermediary, some kind of page that will greet the visitor, explain to him that he is in the archive, and point out the content that he is interested in, or offer popular content.
It is for this reason that we decided to create a custom archives page.
How to create a custom archives page in WordPress
Here's what we're going to do. Our custom archive page will be based on a custom page template. This template will allow us to do the following:
- Add custom post (can contain text, images, registration form, etc. - standard WordPress content).
- Display the last 15 entries (configurable)
- Display links to copyright archives
- Display links to archives by month
- Have additional widget areas (to display things like popular content, categories, tags).
Finally, the page will be responsive and usable on any site.
However, we really need to take some topic as a basis. I used the Zerif Lite theme. I confess that this is one of our own themes. However, it is one of the 10 most popular themes published last year in the WordPress theme directory. So I hope you will take advantage of it.
And yes, if you don’t like the topic, you don’t need to swear. You can use the approach presented in this article with any other topic.
Starting with the main file
The best model for creating an archive page is your current theme's page.php file, for a couple of reasons:
- Its structure is already optimized for displaying arbitrary content within the main content block.
- This is perhaps one of the simplest page templates in your theme structure.
Therefore, we will use the page.php file of the Zerif Lite theme. I will create a copy of it and name it tmpl_archives.php.
(Make sure you don't name the page page-archives.php. All filenames starting with "page-" will be treated as new page templates within the main WordPress theme file hierarchy. This is why we used the tmpl_ prefix) ...
Let's change it to the following:
All this line does is pass the appropriate content file for our archives page.
You can remove other elements that appear to be redundant from your archive page (like comments, for example), but make sure to remove all elements that are relevant to the HTML structure. And overall, don't be afraid to experiment. After all, if something suddenly stops working, you can always go back to the previous code and easily debug it.
In addition, do not forget about the standard comment - the description of the template - which should be located at the very beginning of your file (in our case, in the tmpl_archives.php file):
In the end, we will be left with the following structure (some elements have been removed for convenience):
In this file, we will need to remove all unnecessary things, leaving only structural elements, as well as calls to basic WordPress functions:
The placeholder comment in the very middle of the code is where we will then insert our arbitrary elements.
Adding a custom greeting
WordPress itself has already taken care of this. We just need to insert the following line:
Adding new widget areas
Let's set up new widget areas in WordPress using a standard process. We'll do this with an additional function file to keep things reusable from theme to theme. We'll create a new file, archives-page-functions.php, placing it in the main theme directory, and also register two new widget scopes:
If (! Function_exists ("archives_page_widgets_init")): function archives_page_widgets_init () (/ * First archive page widget, displayed to the LEFT. * / Register_sidebar (array ("name" => __ ("Archives page widget LEFT", "zerif -lite ")," description "=> __ (" This widget will be shown on the left side of your archive page. "," zerif-lite ")," id "=>" archives-left "," before_widget " => "
"," before_title "=>""," after_title "=>"
",)); / * Second archive page widget, displayed to the RIGHT. * / Register_sidebar (array (" name "=> __ (" Archives page widget RIGHT "," zerif-lite ")," description "=> __ ("This widget will be shown on the right side of your archive page.", "Zerif-lite"), "id" => "archives-right", "before_widget" => " "," before_title "=>""," after_title "=>"
",));) endif; add_action (" widgets_init "," archives_page_widgets_init ");If (! Function_exists ("archives_page_styles")): function archives_page_styles () (if (is_page_template ("tmpl_archives.php")) (wp_enqueue_style ("archives-page-style", get_template_directory_uri (). "/ Archives-page-style. css "); // standard way of adding style sheets in WP.)) endif; add_action ("wp_enqueue_scripts", "archives_page_styles");
This is a conditional queuing operation. It will only work if the visitor is viewing the archived page.
Remember to include our new archives-page-functions.php file by adding the following line to the very end of the current theme's functions.php file:
Require get_template_directory (). "/archives-page-functions.php";
Finally, the new block we'll be using in our main content-tmpl_archives.php file is pretty simple. Place the following code below the the_content (); call:
All we now have to do is take care of the only missing file, archives-page-style.css. However, let's leave it for later, as we will use it as a repository for all the styles of our custom archive page, including for widgets.
Displaying the last 15 records
For this we need to do some PHP manual coding. Even though the output of posts can be implemented using a variety of widgets, let's avoid the monotony and dive into the coding to demonstrate more of the different possibilities.
You are probably asking why exactly 15 entries? I don't know, I just took this number from the ceiling. Let's make it customizable with custom fields.
Here's what we'll do:
- Set the number of posts using the custom field archived-posts-no.
- If the number is not correct, the template will use the default value of the last 15 records.
Below is the code that does this. Place it below the previous section in the content-tmpl_archives.php file, which handles the new widget areas.
ID, "archived-posts-no", true)); / * Here, we "re making sure that the number fetched is reasonable. In case it" s higher than 200 or lower than 2, we "re just resetting it to the default value of 15. * / if ($ how_many_last_posts> 200 || $ how_many_last_posts< 2) $how_many_last_posts = 15; $my_query = new WP_Query("post_type=post&nopaging=1"); if($my_query->have_posts ()) (echo "
"; echo"
- "; $ counter = 1; while ($ my_query-> have_posts () && $ counter<= $how_many_last_posts) {
$my_query->the_post (); ?>
- ">
All this code does is get the value of a custom field, sets the number of records to display, and then fetches those records from the database using WP_Query () ;. I also use some Font Awesome icons to add some charm to this block.
Displaying links to copyright archives
This section is only useful if you are running a multi-author blog. Skip this if you have one author.
This functionality can be implemented with a simple block of code that needs to be placed in our content-tmpl_archives.php file (below the previous block):
Our Authors
We'll move on to styles in a couple of minutes. For now, notice that everything is done with a call to the wp_list_authors () function.
Displaying links to archives by month
I have added this element at the very end, as it is not the most useful component from the point of view of readers. However, it is important to have it on the archive page so that you don't have to use widgets to display monthly archives elsewhere on the site.
This is how it will look in the content-tmpl_archives.php file:
By Month
In this case, we will display it as a separate paragraph with entries separated by forward slashes (|).
Complete Archive Page Template
Let's take a look at the complete content-tmpl_archives.php file, which is our main file for outputting a custom archive:
Last ". $ How_many_last_posts." Posts
"; echo" "; $ counter = 1; while ($ my_query-> have_posts () && $ counter<= $how_many_last_posts) {
$my_query->the_post (); ?>
Our Authors
By Month
Style sheet
Finally, let's take a look at the stylesheet. This is what the archives-page-style.css file looks like:
Archives-widget-left (float: left; width: 50%;) .archives-widget-right (float: left; padding-left: 4%; width: 46%;) .archives-latest-section () .archives -latest-section ol (font-style: italic; font-size: 20px; padding: 10px 0;) .archives-latest-section ol li (padding-left: 8px;) .archives-authors-section () .archives -authors-section ul (list-style: none; text-align: center; border-top: 1px dotted # 888; border-bottom: 1px dotted # 888; padding: 10px 0; font-size: 20px; margin: 0 0 20px 0;) .archives-authors-section ul li (display: inline; padding: 0 10px;) .archives-authors-section ul li a (text-decoration: none;) .archives-by-month-section ( ext-align: center; word-spacing: 5px;) .archives-by-month-section p (border-top: 1px dotted # 888; border-bottom: 1px dotted # 888; padding: 15px 0;) .archives- by-month-section pa (text-decoration: none;) @media only screen and (max-width: 600px) (.archives-widget-left (width: 100%;) .archives-widget -right (width: 100%; ))
Most of the styles are about fonts and a few structural elements, with the exception of a couple of alignments and the responsive design block at the end.
Let's see how it will look in practice. Here's a site that has tons of content in the archive:
How to integrate this template into any theme
The custom archives page we created here is for the Zerif Lite theme, which is available in the official WordPress directory. However, as I said before, it can be used alongside any theme. Here's how to do it:
- We grab the archives-page-style.css and archives-page-functions.php files that we created in the tutorial and then place them in the main theme directory.
- Edit the theme's functions.php and add the following line at the very end: require get_template_directory (). '/Archives-page-functions.php';
- We take the theme page.php file, make a copy of it, rename it, change the call to the get_template_part () function to get_template_part ('content', 'tmpl_archives');, and then add a descriptive comment at the very beginning: / * Template Name: Archive Page Custom * /.
- We take the content-page.php file of your theme, make a copy of it, rename it to content-tmpl_archives.php, and add all the arbitrary blocks that we created in the tutorial to it, just below the call to the_content (); function.
- We test and enjoy.
This is how it would look in the default Twenty Fifteen theme:
When you need to quickly download site sources from the server, even a relatively fast SSH tunnel does not provide the required speed. And you have to wait a very, very long time. And many hosting providers do not provide this access, but force them to be content with FTP, which is many times slower.
For myself, I determined the way out. A small script is uploaded to the server and launched. After a while, we get an archive with all the sources. And one file, even through the ancient FTP, downloads much faster than a hundred small ones.
Earlier on the pages of this blog, the zipArchive. However, then it was about unpacking the archive.
First, we need to find out if the server has zipArchive support. This popular library is installed on the overwhelming majority of hosting sites.
The library is strictly limited by php and server parameters. You won't be able to archive huge databases and banks of photos. Even the bases of the good old 1C program for accounting. It would seem that they should contain only text data. But no.
I advise you to use the library only when archiving relatively small sites with a huge number of small files.
Let's check if working with the library is available
If (! Extension_loaded ("zip")) (return false;)
If all is well, the script will continue its execution further.
A small offtopic for such checks. Checks should be done this way, avoiding large nested parentheses. This will make the code more atomic and easier to debug. Compare
If (a == b) (if (c == d) (if (e == f) (echo "All conditions worked";) else echo "e<>f ";) else echo" c<>d ";) else echo" a<>b;
and a code like this
If (a! = B) exit ("a<>b); if (c! = d) exit ("c<>d); if (e! = f) exit ("e<>f); echo "All conditions met";
The code is nicer and doesn't grow into huge nested constructs.
Sorry for the offtopic, but I wanted to share this find.
Now let's create an object and an archive.
$ zip = new ZipArchive (); if (! $ zip-> open ($ destination, ZIPARCHIVE :: CREATE)) (return false;)
where $ destination is the full path to the archive. If the archive has already been created, the files will be added to it.
$ zip-> addEmptyDir (str_replace ($ source. "/", "", $ file. "/"));
where $ source is the full path to our category (which we originally archived), $ file is the full path to the current folder. This is done so that there are no full paths in the archive, but only relative ones.
Adding a file works in a similar way, but you must first read it into a string.
$ zip-> addFromString (str_replace ($ source. "/", "", $ file), file_get_contents ($ file));
At the end, you need to close the archive.
Return $ zip-> close ();
How to run through all the files and subdirectories in a folder, I think there is no need to explain. Google something like Recursive folder traversal in php
This option suited me
Function Zip ($ source, $ destination) (if (! Extension_loaded ("zip") ||! File_exists ($ source)) (return false;) $ zip = new ZipArchive (); if (! $ Zip-> open ( $ destination, ZIPARCHIVE :: CREATE)) (return false;) $ source = str_replace ("\\", "/", realpath ($ source)); if (is_dir ($ source) === true) ($ files = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($ source), RecursiveIteratorIterator :: SELF_FIRST); foreach ($ files as $ file) ($ file = str_replace ("\\", "/", $ file); // Ignore "." and ".." folders if (in_array (substr ($ file, strrpos ($ file, "/") + 1), array (".", ".."))) continue; $ file = realpath ($ file ); $ file = str_replace ("\\", "/", $ file); if (is_dir ($ file) === true) ($ zip-> addEmptyDir (str_replace ($ source. "/", "" , $ file. "/"));) else if (is_file ($ file) === true) ($ zip-> addFromString (str_replace ($ source. "/", "", $ file), file_get_contents ($ file));))) else if (is_file ($ source) === true) ($ zip-> addFromString (basename ($ source), file_get_contents ($ source));) return $ zip-> close (); )
Hello dear readers of the blog site. Not so long ago, I wrote about something that certainly deserves all sorts of flattering epithets, despite its inherent minor flaws and criticism of its articles from the scientific community.
The very fact that a non-commercial project has been working for the benefit of the entire Internet community for decades now deserves great respect. But there is also a similar large-scale project on the network, which, without receiving this income, plays a very important role - saves site archives, video, audio and printed products.
Interestingly, the last column of this list (you can open it in Excel) will display the number of archives created for each site in the Web Archive (however, you can check the presence of a domain in the web archive in a number of online services, for example, on this or on this).
The list of bourgeois domain names that are being released or have already been released can be downloaded from this link. Well, then we look through the content of the sites that Web Archive has saved and try to find something worthwhile. Then we check the uniqueness of these materials (the link was given just above) and, if successful, we publish them on our resource, or we sell them in some other.
Yes, the method is dreary and I have not personally tested it. But I think that with a certain degree of automation and thinking, it can give a good exhaust. Probably someone has already put it on stream. And what do you think?
Good luck to you! See you soon on the pages of the blog site
You may be interested
Comparison of sites in SEObuilding.RU for free analysis of potential donors when buying links
Free online service for the selection of beautiful and free domains for registration (Frishki.ru)
SEObuilding.RU - a complete free website analysis with the calculation of their trust, cost and much more
Photo stocks and photobanks - 30 free legal sources of photos, images and icons VirusTotal - online virus check of your computer, phone and website files with all antiviruses at the same time
Site analysis in free online services Pr-cy, Cy-pr, Be1, Xseo and others
Website check (analysis) - 85 online services and programs
How did I increase my website traffic to 300 people a day? SE Ranking is the best position monitoring service for newbies and SEO professionals
Acceleration and protection of your site in the cloud service Airi.rf
The Internet Archive offers over 15,000,000 freely downloadable books and texts. There is also a collection of that may be borrowed by anyone with a free site account.
Alternatively, our portable Table Top Scanner can also be purchased and used on-site within libraries and archives. To read more about our TT Scribe, please visit.
Since 2005, the Internet Archive has collaborated and built digital collections with over 1,100 Library Institutions and other content providers. Partnerships include:, the and the. These collections are digitized from various mediatypes including:, and a wide variety of. Significant contributions have come from partners in North America (and Libraries), and, representing more than 184 languages.
The Internet Archive encourages our global community to contribute physical items, as well as uploading digital materials directly to the Internet Archive. If you have digital items that you would like to add to the Internet Archive, please a new item using the uploader interface. Click here to apply the specific creative commons license Creative Commons license to communicate how the material can be used.
For donation of physical books or items, please contact [email protected] site
Free to read, download, print, and enjoy. Some have restrictions on bulk re-use and commercial use, please see the collection or the sponsor of a book. By near-unrestricted access to these texts, we hope to encourage widespread use of texts in new contexts by people who might not have used them before.