Crear un tema WordPress personalitzat utilitzant Tailwind CSS 3
Created at: 2025-01-17 09:09:59 | Updated at: 2025-01-17 09:09:59
Instal·lar Node.js i npm
Assegura't de tenir Node.js i npm instal·lats al teu sistema.
cd wp-content/themes/
mkdir my-tailwind-theme cd my-tailwind-theme
touch index.php style.css functions.php
/* Theme Name: My Tailwind Theme Theme URI: http://example.com/my-tailwind-theme Author: Your Name Author URI: http://example.com Description: A custom WordPress theme using Tailwind CSS. Version: 1.0 */
Inicialitzar npm
npm init -y
Instal·lar Tailwind CSS
npm install -D tailwindcss@latest postcss@latest autoprefixer@latest npx tailwindcss init
Això crearà un fitxer tailwind.config.js.
Configurar Tailwind CSS
Obre tailwind.config.js i configura el camí dels teus fitxers de plantilla:
module.exports = { content: [ './*.php', './**/*.php', ], theme: { extend: {}, }, plugins: [], }
Public code references from 5 repositories
Crea un fitxer postcss.config.js amb el següent contingut:
module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, }
Crea un fitxer src/styles.css amb el següent contingut:
@tailwind base; @tailwind components; @tailwind utilities;
Afegeix els scripts de compilació al package.json:
"scripts": { "build": "tailwindcss -i ./src/styles.css -o ./style.css --minify", "watch": "tailwindcss -i ./src/styles.css -o ./style.css --watch" }
Compila Tailwind CSS:
npm run build
Obre functions.php i afegeix el següent codi per enquejar el CSS generat:
function my_tailwind_theme_enqueue_styles() { wp_enqueue_style('tailwindcss', get_template_directory_uri() . '/style.css', array(), '1.0'); } add_action('wp_enqueue_scripts', 'my_tailwind_theme_enqueue_styles');
Crea les plantilles necessàries com header.php, footer.php, etc. Per exemple, header.php podria ser:
<!DOCTYPE html> <html <?php language_attributes(); ?>> <head> <meta charset="<?php bloginfo('charset'); ?>"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> <header class="bg-blue-500 text-white p-4"> <h1><?php bloginfo('name'); ?></h1> <p><?php bloginfo('description'); ?></p> </header>
Comença a desenvolupar el teu tema utilitzant les utilitats de Tailwind CSS. Per exemple, en el teu index.php:
<?php get_header(); ?> <main class="container mx-auto p-4"> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <article class="mb-4"> <h2 class="text-xl font-bold"><?php the_title(); ?></h2> <div><?php the_content(); ?></div> </article> <?php endwhile; else : echo '<p>No posts found</p>'; endif; ?> </main> <?php get_footer(); ?>
Public code references from 5 repositories
Activa el teu tema des del tauler d'administració de WordPress i visualitza'l al navegador.
Aquest procés t'ajudarà a crear un tema de WordPress utilitzant Tailwind CSS 3, proporcionant-te una base sòlida per a personalitzacions addicionals.
No valid image directory found or the category is not valid for the gallery.