Skip to content

Instantly share code, notes, and snippets.

@ville6000
ville6000 / index.php
Last active October 22, 2015 08:27
Variable demo 2
<?php
$post_idx = 0;
if ( have_posts()) {
while ( have_posts()) {
the_post();
// $post_idx is available in content-page.php
include( locate_template( 'content-page.php' ) )
$post_idx += 1;
}
@ville6000
ville6000 / functions.php
Last active April 18, 2016 07:12
Add siblings field to WP REST API data
<?php
add_action( 'rest_api_init', 'slug_register_siglings' );
function slug_register_siglings() {
register_api_field( 'post',
'siblings',
array(
'get_callback' => 'slug_get_siglings',
'update_callback' => null,
'schema' => null,
@ville6000
ville6000 / functions.php
Created October 29, 2015 07:56
Add localized JavaScript variables to WordPress theme
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_localize_script(
'script',
'i18n',
array(
'download' => __( 'Download file', 'mytheme' ),
)
);
function Counter () {
var count = 0;
function logNewValue() {
console.log("Count is " + count);
}
return {
add: function () {
count += 1;
@ville6000
ville6000 / index.php
Created November 10, 2015 08:02
Get select fields options from Types custom field
<?php
$field_config = wpcf_admin_fields_get_field( 'field-id' );
$field_options = array();
if ( isset( $field_config['data']['options'] ) ) {
foreach ( $field_config['data']['options'] as $option ) {
if ( isset( $option['value'] ) ) {
$field_options[ $option['title'] ] = $option['value'];
}
}
@ville6000
ville6000 / index.php
Last active November 13, 2015 13:20
Order WordPress posts by multiple keys
<?php
$the_query = new WP_Query( array(
'orderby' => array( 'menu_order' => 'ASC', 'title' => 'DESC' ),
) );
@ville6000
ville6000 / index.php
Last active October 28, 2016 20:29
Get translated permalink with WPML 3.2+
<?php $object_id = apply_filters( 'wpml_object_id', YOUR-PAGE-ID-HERE, 'page' ); ?>
<a href="<?php echo get_permalink( $object_id ); ?>">
<?php _e( 'Link text', 'text-domain' );
</a>
@ville6000
ville6000 / index.php
Created January 19, 2016 09:27
Get human readable time difference in WordPress
<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?>
<?xml version="1.0" encoding="UTF-8"?>
<modification>
<id>ExampleId</id>
<version>0.1</version>
<vqmver>2.5.1</vqmver>
<author>Ville6000</author>
<file name="catalog/view/theme/journal/template/product/product.tpl">
<operation>
<search position="replace"><![CDATA[<input type="button" value="<?php echo $button_cart; ?>" id="button-cart" class="button" />]]></search>
@ville6000
ville6000 / functions.php
Last active March 30, 2016 08:23
Override Yoast SEO breadcrumb trail
<?php
add_filter( 'wpseo_breadcrumb_links', 'override_yoast_breadcrumb_trail' );
function override_yoast_breadcrumb_trail( $links ) {
if ( get_post_type() === 'employee' ) {
$breadcrumb[] = array(
'url' => get_permalink( get_page_by_title( 'contact' )->ID ),
'text' => __( 'Contact', 'your-text-domain' ),
);