Month: June 2020

How to change loop times in WordPress

GOAL

To stop the loop at the number of times specified in WordPress.

Environment

WordPress 5.5.1

Loop in WordPress

The Loop is PHP code used by WordPress to display posts. The following is an example of the loop.

<?php 
if ( have_posts() ) {
	while ( have_posts() ) {
		the_post(); 
		//
		// Post Content here
		//
	} // end while
} // end if
?>

Check “The Loop” in wordpress.org for details about the loop.

Method

Use get_posts function

<?php
	$recentposts = get_posts( array( 'posts_per_page' => 3) );
	if ( $lastposts ) {
    		foreach ( $lastposts as $post ) : setup_postdata( $post );
			the_title();
        		//
			// Post Content here
			//
		endforeach; 
    		wp_reset_postdata();
	} //end if
?>

Appendix

Though <?php query_posts(‘posts_per_page=NUMBER’); ?> can be also used to limit the number of posts, it is not recommended.

Create Original Theme in WordPress 2

At the last time, in “Create Original Theme in WordPress 1“, I created the simplest minimum theme “techblog”. I’ll create practical WordPress theme from now on.

GOAL

To design the configuration of the WordPress site.

the configuration of pages

This is the page transition diagram. The top page is home. Pages “Home”, “About”, “Links” and “Posts(Post List)” can be accessed directly with main menu.

This is the construction of each page and links among them.

Detail design of each page

Home page

This page is used for “Home”.

Posts page

This page is used for “Posts (all posts)”, the result searched by categories and the result searched by words.

Single Page

This page is used for “About”, “Links” and the page for each single posts.

Side bar

Some of these items can be added easily by using Plugins.

Create Original Theme in WordPress 1

ULTIMATE GOAL

To create the original WordPress theme

GOAL

Today’s goal is to make the smallest WordPress theme as below.

File construction for WordPress theme

The following is an example of file construction.

  • index.php
  • header.php
  • footer.php
  • archive.php
  • comments.php
  • 404.php
  • page.php
    • page-fullwidth.php
  • sidebar.php
    • sidebar-home.php
    • sidebar-footer.php
  • single.php
  • category.php
  • tag.php
  • functions.php
  • style.css
  • templaete-parts
    • content.php
    • content-frontpage.php
    • content-none.php
    • content-single.php

You can see the role of each file in wordpress.org “Template Files”. Today, I create index.php, functions.php and style.css

index.php

This is the main PHP file displayed as a top page.

<?php
/**
*This is main file for "techblog" theme
*@package Techblog
*@since Techblog 1.0
 */
get_header(); ?>
<main id="main" class="site-main" role="main">
	<header>
		<h1 class="page-title"><?php bloginfo('name'); ?></h1>
		<h2 class="description"> <?php bloginfo('description'); ?></h2>
	</header>
	<?php if ( have_posts() ) : ?>
		<ul id="posts">
			<?php while ( have_posts() ) : the_post(); ?>
                <li><a href="<?php the_permalink(); ?>">
                	<h2 class = "post_item"><?php the_title(); ?></h2>
                </a></li>
			<?php endwhile; ?>
		</ul>
	<?php endif; ?>
</main>
<?php get_footer(); ?>

PHP Doc Tags

Some PHP doc tags are used in WordPress. You can see the list of PHP doc tags in “PHPDoc Tags” in wordpress.org.

*@package Techblog
*@since Techblog 1.0

php functions

You can see the functions that can be used in WordPress in “Code Reference“.

  • get_header()
    • Includes the header template for a theme
    • If you have no header.php, it includes the default header.
  • bloginfo()
    • Display information about the site you defined.
    • You can get information such as ‘name’, ‘description’ and so on.
  • have_posts()
    • It can be used to determines whether current WordPress query has posts to loop over
  • the_post()
    • Iterate the post index in the loop
    • It can be used when have_posts() is True
  • the_permalink()
    • Displays the permalink for the current post
    • It displays the url to the_post()
  • the_title()
    • Display or retrieve the current post title
    • It displays the titel of the_post()
  • get_footer()
    • Includes the footer template for a theme

style.css

style.css is the style sheet for the site. style.css should have infromation about the theme. Please check “Main Stylesheet (style.css)” in wordpress.org for details.

/*
Theme Name: Techblog
Theme URI: #
Author: Nako
Author URI: https://s-nako.work
Description: This is the simplest theme for engineer's technical blog
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: techblog
Domain Path: /languages/
Tags: blog, news,

This theme, like WordPress, is licensed under the GPL.
*/

body {
    color: #6B6B6B;
    background-color:#DADADA;
    font-family: 'Open Sans', sans-serif;
    line-height: 1.7;
    font-size: 14px;
}

The information in style.css is displayed in Appearance >Themes.

functions.php

functions.php is function set for the site. I don’t use any original function at this time.

<?php
/**
 * techblog functions and definitions
 * @package Techblog
 * @since Techblog 1.0
 */
if ( ! defined( 'TECHBLOG_VERSION' ) ) {
	// Replace the version number of the theme on each release.
	define( 'TECHBLOG_VERSION', '1.0.0' );
}

Install the created theme

Put the directory where index.php, style.css and functions.php is into the themes directory “xampp\htdocs\wordpress\wp-content\themes”.

You can activate the theme you created in the Appearance >Themes.

[Error]’unicodeescape’ codec can’t decode bytes

Problem

When I run the python code below, the error “SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3” occurred.

import glob
import os

word = "single_post_title"
path = "C:\Users\USERNAME\Downloads\document"
files = glob.glob(path +'\**')
for f in files:
    print(f)

Environment

Windows10
Python 3.8.6

Cause of the error

The unicodeescape ‘\’ is used in path = “C:\Users\USERNAME\Downloads\document”. “\” is used as a part of escape sequences in Windows system.

Solution

Solution 1. Use “\\” to express “\” in string data

path = "C:\\Users\\USERNAME\\Downloads\\document"
files = glob.glob(path +'\\**')

Solution 2. Use “/” instead of “\”

path = "C:/Users/USERNAME/Downloads/document"
files = glob.glob(path +'/**')

How To Develop WordPress in XAMPP

I’d like to create my original theme for WordPress. This is the preparation for development of WordPress in XAMPP.

GOAL

To create develop environment for WordPress development in XAMPP.

Environment

Windows10
XAMPP 7.4.10

If you don’t have XAMPP, please install it first according to “Installation of XAMPP“. And If you’ll use MariaDB in a production environment, make it secure by setting password according to “How To Set MySQL Password in XAMPP“.

Method

1. Install WordPress

Download WordPress from wordpress.org.

2. Put the WordPress into the root doc

The default root doc is xampp\htdocs.

3. Create a new database for WordPress

Reference: Using phpMyAdmin in Support page of wordpress.org

Open XAMPP Control Panel and run Apache and MySQL. Then access phpMyAdmin, localhost/phpmyadmin.
Create a new database for WordPress as below.

4. Connecting the database to WordPress

Access localhost/wordpress. In my case, the page is redirected to localhost/wordpress/wp-admin/setup-config.php.

Select language and go next page.

Input the data of your database and press Submit. Database Name is the database name you defined.

You can see Username, Password and Database Host in phpMyAdmin.

The connection to the database will be established. Press “Run the installation”.

5. Input information of your WordPress.

Input information of your new WordPress and press “Install WordPress”.

6. Confirm that you can login WordPress

You can login localhost/wordpress/wp-admin/ with Username and Password you defined at the last page.

Appendix

*You can import post, pages, comments, custom fields, categories, and tags from a WordPress export .xml file with Import tab.