viernes, 19 de junio de 2015

Understanding Dynamic WordPress links

Understanding Dynamic WordPress links


With most CMS environments like WordPress, there is a learning curve to understand how the URL structures work. When I was first learning how to develop WordPress themes, I would look through other themes to help get an understanding of how to call different templates or directories and I would often get mixed up. When a developer is transitioning from basic HTML website development to using a framework like WordPress these are important principles to understand. Here is a list of my most commonly used URL functions with example usages. These are all based from the Codex in WordPress.org.

Blog Name

You can use the Blog Name parameter to display the name of your blog ( or Website ) anywhere! For example, maybe you want the title tag for your site to just display the name of your website. You would do the following:
<title><?php bloginfo( 'name' ); ?></title>
Note: This is not an SEO recommendation by no means so don’t take it as that. ha You will probably always rank for the name of your website ;)

Website URL

The URL parameter displays the URL of your website. This is very self explanatory, but many times I see themes using absolute URL’s ( http://www.wproots.com ) and not using the relative URL. Below is how you should display your relative URL. This is especially important if you plan on distributing the theme as the URL will continually change.
<?php bloginfo( 'url' ); ?>

Stylesheet Directory

For me, there isn’t another parameter that I use more than the stylesheet_directory parameter. What the stylesheet_directory does is reference the directory where the style.css file is in your theme. Here is an example of calling an image located in your images folder through your HTML.
<img src="<?php bloginfo( ‘stylesheet_directory’ ); ?>/images/myimage.jpg" width="…" height="…" alt="…" />
Note: Don't get confused when you see the term stylesheet in the parameter. All this is doing is referencing the directory or folder where the style.css file is located in the theme.

Template Directory

Template_Directory is a lot like the stylesheet_directory parameter except the usages are a bit different. The template_directory refers to the directory the theme templates are located in. From my experience, most basic WordPress themes do not use this parameter as it is not necessary to handle that basics of theme development. The template_directory parameter becomes very functional when building more advanced themes or applications. An example of this would be if you are using an MVC type structure or more complex theme options panels and you need to reference files deeper within the theme directory.
<?php bloginfo( 'template_directory' ); ?>/classes/myclass.php ?>
Again, I would highly recommend using these built in WordPress functions when developing themes and plugins. Let me know your thoughts and how you find them most useful!

No hay comentarios:

Publicar un comentario