Tabla de Contenidos

Wordpress

Basic

To install

Directory structure

How to develop

Core code files implement main Wordpress functions. /wp-includes/option.php, for example, contains functions for updating the site configuration, /wp-includes/pluggable.php and /wp-includes/ user.php contain function for updating users… Also exist /wp-includes/post.php, comments…

There is also the possibility to add more information to these elements (posts, users, comments…); to do that exists the concept of meta elements, which exists for other main elements in Wordpress.

Themes and plugins

Themes

wp-content\themes contains several folders, each one is an installed theme. You can develop your theme there.
WordPress themes only require two files: index.php and style.css. They usually are made up of many files and can become quickly disorganized. Still Wordpress does not need any folder you also can add a js and a css folders, in its root you'll place the template main files. There's also another especial folder, page-templates, where you will put the custom page template files.

So, to develop a theme you will create a folder with the name of the theme. There you will add an index.php file and a style.css.

Theme hierarchy

WordPress uses the query string (a sequence of codes in a Uniform Resource Identifier (URI) that a web page uses to determine what dynamic data to display) to decide which template or set of templates should be used to display the page.

  1. From the query string Wordpress decides which page is being requested (search, category…).
  2. Selects the template determined by the template hierarchy.
  3. Looks for template files with specific names in the current theme’s directory and uses the first matching template file as specified by the hierarchy.

With the exception of the basic index.php template file, you can choose whether you want to implement a particular template file or not. If WordPress cannot find a template file with a matching name, it will skip to the next file in the hierarchy. If WordPress cannot find any matching template file, the theme’s index.php file will be used.

functions.php

To add some logic to the template. The difference between functions.php and plugin.

A WordPress plugin:

A functions file:

In functions.php you can:

functions.php is loaded during theme setup, after any plugin files have loaded.

General uses for functions.php:

This is an example of functions.php

Attaching CSS and JS

You will need to register and enqueue the plugins and styles. To do it, you must add a callback function to the head or the wp_enqueue_scripts event. To add it:

add_action( 'wp_enqueue_scripts', 'register_styles' );

Then, to register and enqueue:

function register_styles() {
	wp_register_style( 'my-plugin',  get_template_directory_uri() . '/css/my_style.css' );
	wp_enqueue_style( 'my-plugin' );
}

All of this code goes into the functions.php.

You also can use plugins_url()(for Plugins) and get_template_directory_uri() (for Themes) to get a proper URL.

In the same way you will use wp_register_script and wp_enqueue_style.

Page templates

Special wordpress templates. They are only to change the look and feel of a page (not a post); they can be applied to a single page, a page section or a class of pages. page-about.php is a page template, more specific than page.php or index.php.

When a user requests a specific page, the core WordPress code decides which template to use for rendering that page. Template Hierarchy:

index.php — Or else WordPress uses the theme’s index file.

Name your template file so you can easily identify its template name, e.g., filename my-custom-page.php for the template name “My Custom Page.” Important! Do not use page- as a prefix, as WordPress will interpret the file as a specialized template, meant to apply to only one page on your site.

For example: Your About page has a slug of ‘about’ and an ID of 6. If your active theme’s folder has a file named page-about.php or page-6.php, then WordPress will automatically find and use that file to render the About page.

The post loop

It's used to display posts according to your instructions given by template tags. The basic loop is (while there are posts, display the posts):

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

The most simple index.php would be:

<?php
get_header();
if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_content();
   endwhile;
endif;
get_sidebar();
get_footer();
?>

Other example:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
 
    <?php the_post_thumbnail(); ?>
    <?php the_excerpt(); ?>
 
<?php endwhile; else: ?>
<?php _e('Sorry, no posts matched your criteria.'); ?>
<?php endif; ?>

Functions that cna be used

WP_Query

WP_Query is a class given by WordPress to be used in the loop. For example $wp_query→have_posts() is called to see if there are any posts to show. To obtain a post $wp_query→the_post() is called. Also can be used to filter posts:

<?php $my_query = new WP_Query( $args ); ?>
<?php while ($my_query->have_posts()) ? $my_query->the_post(); ?>
    // Do something
<?php endwhile; ?>

Conditional tags

They are used o control which templates are loaded on a certain page. Also which parts:

Useful functions to use on templates

How to...

Simple header

 <head>
        <meta charset="<?php bloginfo( 'charset' ); ?>">
        <title><?php wp_title( '|', true, 'right' ); ?></title>
        <link rel="stylesheet" href="<?php echo esc_url( get_stylesheet_uri() ); ?>" type="text/css" />
<?php wp_head(); ?>
    </head>

Create a front pate

You must indicate on the admin panel that the front page won't show the first post but content from a created page. Then…

<?php
/*
* Template Name: My Template
*/
get_header(); ?>
 
<div id="container">
    <div id="content" class="pageContent">
 
    <h1 class="entry-title"><?php the_title(); ?></h1> <!-- Page Title -->
    <?php
    // TO SHOW THE PAGE CONTENTS
    while ( have_posts() ) : the_post(); ?> <!--Because the_content() works only inside a WP Loop -->
        <div class="entry-content-page">
            <?php the_content(); ?> <!-- Page Content -->
        </div><!-- .entry-content-page -->
 
    <?php
    endwhile; //resetting the page loop
    wp_reset_query(); //resetting the page query
    ?>
 
    <?php
    // TO SHOW THE POST CONTENTS
    ?>                        
        <?php
        $my_query = new WP_Query( 'cat=1' ); // I used a category id 1 as an example
        ?>
        <?php if ( $my_query->have_posts() ) : ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
 
            <h1 class="entry-title"><?php the_title(); ?></h1> <!-- Queried Post Title -->
            <div class="entry-content">
                <?php the_excerpt(); ?> <!-- Queried Post Excerpts -->
            </div><!-- .entry-content -->
 
        <?php endwhile; //resetting the post loop ?>
 
        </div><!-- #post-<?php the_ID(); ?> -->
 
        <?php
        wp_reset_postdata(); //resetting the post query
        endif;
        ?>
 
    </div><!-- #content -->         
</div><!-- #container -->

Get all posts

Using the argument post_status=any and post_per_page=1:

$args = array(
              'post_type' => 'post',
              'orderby'   => 'title',
              'order'     => 'ASC',
              'post_status' => 'any',
              'posts_per_page' => -1,
            );
$my_query = new WP_Query($args);

Get a concrete page content

<?php $the_query = new WP_Query( 'pagename=contact' ); ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

Get child pages from front page

First you must obtain the ID of the front page, it's stored in the wp_options WP table, as option_name=page_on_front and option_value=ID of the page. So if you want to retrieve this value, just use get_option('page_on_front').

<?php $args = array(
	'child_of'     => get_option('page_on_front'),
	'post_type'    => 'page',
	'post_status'  => 'publish',
	'title_li'     => '', 
); ?>
<?php $children = wp_list_pages( $args ); ?>
<?php if ($children) : ?>
<ul>
<?php echo   $children; ?>
</ul>
<?php endif; ?>

Other functionalities

Plugins