Skip to content

Instantly share code, notes, and snippets.

@mzo84
mzo84 / .htaccess
Created July 10, 2016 05:48
pretty url htaccess for yii 2 - put in /web folder
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?basic/(.*)$ /basic/index.php/$1 [L]
@mzo84
mzo84 / custom-taxonomies.php
Last active August 14, 2016 08:20
wordpress - add custom taxonomies example
// Add to functions.php within the theme folder.
function create_my_taxonomies() {
register_taxonomy('actors', 'post', array(
'hierarchical' => false, 'label' => 'Actors',
'query_var' => true, 'rewrite' => true));
register_taxonomy('producers', 'post', array(
'hierarchical' => false, 'label' => 'Producers',
'query_var' => true, 'rewrite' => true));
}
@mzo84
mzo84 / seo-friendly-title.php
Created August 15, 2016 08:23
<title> tag for bookmark friendly links
<title>
<?php if (function_exists('is_tag') && is_tag()) {
single_tag_title('Tag Archive for &quot;'); echo '&quot; - ';
} elseif (is_archive()) {
wp_title(''); echo ' Archive - ';
} elseif (is_search()) {
echo 'Search for &quot;'.wp_specialchars($s).'&quot; - ';
} elseif (!(is_404()) && (is_single()) || (is_page())) {
wp_title(''); echo ' - ';
} elseif (is_404()) {
@mzo84
mzo84 / global-custom-field.php
Created August 15, 2016 10:55
Add a global custom field
// Add this to your functions.php:
<?php add_action('admin_menu', 'add_gcf_interface');
function add_gcf_interface() {
add_options_page('Global Custom Fields', 'Global Custom Fields', '8', 'functions',
'editglobalcustom elds');
}
function editglobalcustom elds() { ?>
<div class="wrap">
<h2>Global Custom Fields</h2>
<form method="post" action="options.php">
@mzo84
mzo84 / the-loop.php
Last active August 16, 2016 06:12
wordpress loop
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php echo get_post_meta($post->ID, 'PostThumb', true); ?>
@mzo84
mzo84 / loop-query.php
Last active August 15, 2016 11:33
wordpress - To check what query string is being used by the Loop
// To check what query string is being used by the Loop
// insert before the loop.
<?php echo "<pre>"; print_r($wp_query->query_vars); echo "</pre>"; ?>
@mzo84
mzo84 / page-custom-fields.php
Created August 15, 2016 11:46
wordpress - custom field on page
// when adding a custom field to a page, you can check its value like this:
<?php // e.g conditional sidebar display
if (!get_post_meta($post->ID, "noSidebar", true)) {
get_sidebar();
} ?>
@mzo84
mzo84 / query-posts.php
Created August 16, 2016 06:28
wordpress - query posts
<?php // The WordPress Loop - customized with query_posts
global $query_string; // grab the global query information
$posts = query_posts($query_string.'&cat=-9'); // exclude Asides category
if (have_posts()) : while (have_posts()) : the_post();
...
endwhile; else:
...
endif;
wp_reset_query(); // reset the query
?>
@mzo84
mzo84 / WP-Query.php
Created August 16, 2016 06:33
Customizing the Loop with WP_Query
<?php // The WordPress Loop - customized with WP_Query
$custom_query = new WP_Query('cat=-9'); // exclude Asides category
while($custom_query->have_posts()) : $custom_query->the_post();
...
endwhile;
wp_reset_postdata(); // reset the query
?>
// more examples
<?php
@mzo84
mzo84 / WP_Query-multiple-loops.php
Created August 16, 2016 06:34
Using WP_Query to create and customize multiple loops.
<?php // Loop 1
$ rst_query = new WP_Query('cat=-1'); // exclude category
while($ rst_query->have_posts()) : $ rst_query->the_post();
...
endwhile;
wp_reset_postdata(); // reset the query
// Loop 2
$second_query = new WP_Query('cat=-2'); // exclude category
while($second_query->have_posts()) : $second_query->the_post();
...