Skip to content

Instantly share code, notes, and snippets.

@juanlopezdev
Last active March 11, 2022 16:27
Show Gist options
  • Save juanlopezdev/8732aa7dacf04d8a728a to your computer and use it in GitHub Desktop.
Save juanlopezdev/8732aa7dacf04d8a728a to your computer and use it in GitHub Desktop.
Useful functions for wordpress / Funciones Útiles Wordpress #php #wordpress
<?php
/**
* Funciones Útiles Wordpress / Useful functions for wordpress
*/
//Pintar MENU
//https://developer.wordpress.org/reference/functions/wp_nav_menu/
//---------------------------------------------------------------
wp_nav_menu(array(
'menu' => 'wp_menu_top',
'container' => ''
));
//---------------------------------------------------------------
//LISTAR todas las TAXONOMIAS / List All Taxonomies *
//http://codex.wordpress.org/Function_Reference/get_terms
//-------------------------------------------------------
$terms = get_terms('name_taxonomy');
//-------------------------------------------------------
//Listar POST de una TAXONOMIA/ Get post by taxonomy *
//Taxonomia: marca
//http://codex.wordpress.org/Class_Reference/WP_Query
//-------------------------------------------------------
$args_products = array(
'post_type' => 'post_name',
'tax_query' => array(
array(
'taxonomy' => 'marca',
'field' => 'term_id',
'terms' => 3
)
),
'posts_per_page' => 5
);
$loop_products = new WP_Query($args_products);
//-------------------------------------------------------
//Obtener valores de las cajas personalizadas "CUSTOM FIELD" de una TAXONOMIA / Get values ​​of custom boxes "Custom Field" of a taxonomy *
//http://www.advancedcustomfields.com/resources/how-to-get-values-from-a-taxonomy-term/
//-------------------------------------------------------
$variable = get_field('field_name', $term);
$variable = get_field('field_name', 'product-cat_23');
//-------------------------------------------------------
//Pintar una IMAGEN DESTACAD / Print Image *
//http://codex.wordpress.org/Function_Reference/the_post_thumbnail
//-------------------------------------------------------
$default_attr = array(
'alt' => get_the_title(),
'title' => get_the_title(),
'class' => 'product-img',
'style' => 'width: 131px;'
);
the_post_thumbnail('thumbnail-image', $default_attr);
//Print: <img src="../img.png" class="product-img" style="width: 131px;" alt="Alt" title="Title"/>
//-------------------------------------------------------
//Obtener SRC de una IMAGEN DESTACADA de un POST
//http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
//-------------------------------------------------------
$image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID) , 'my_thumbail');
//Return:
//[0] => url
//[1] => width
//[2] => height
//[3] => boolean: true if $url is a resized image, false if it is the original.
//-------------------------------------------------------
//Obtener TAXONOMIA de un POST *
//http://codex.wordpress.org/Function_Reference/wp_get_post_terms
//---------------------------------------------------------------
$terms = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "all"));
//---------------------------------------------------------------
//Obtener IMAGEN de ACF(Advanced Custom Field)
//http://www.advancedcustomfields.com/resources/image/
//---------------------------------------------------------------
//Type: Object
$image = get_field('the_field_image');
$thumbnail = $image['sizes']['my_image-thumbnail'];
$title = $brand_logo_image['title'];
$alt = $brand_logo_image['alt'];
/* HTML
<img src="<?php echo $thumbnail; ?>" alt="<?php echo $alt ?>" title="<?php echo $title ?>" />
*/
//---------------------------------------------------------------
//Obtener PARTE de un WEB
//https://codex.wordpress.org/Function_Reference/get_template_part
//---------------------------------------------------------------
get_template_part( 'partials/content', 'page' );
//---------------------------------------------------------------
//Obtener ID de un POST por SLUG
//https://wordpress.org/support/topic/how-can-i-get-the-page-id-from-a-page-slug
//---------------------------------------------------------------
$my_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$slug'"
//---------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment