Forked from juanlopezdev/WORDPRESS_functions_utils.php
Last active
August 5, 2022 10:08
-
-
Save delennerd/cb3b09192f7eeead890b1e9af1b74821 to your computer and use it in GitHub Desktop.
Useful functions for wordpress #php #wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Funciones Útiles Wordpress / Useful functions for wordpress | |
*/ | |
// Print Nav Menu | |
// https://developer.wordpress.org/reference/functions/wp_nav_menu/ | |
//--------------------------------------------------------------- | |
wp_nav_menu(array( | |
'menu' => 'wp_menu_top', | |
'container' => '' | |
)); | |
//--------------------------------------------------------------- | |
// List All Taxonomies * | |
// http://codex.wordpress.org/Function_Reference/get_terms | |
//------------------------------------------------------- | |
$terms = get_terms('name_taxonomy'); | |
//------------------------------------------------------- | |
// 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 ); | |
//------------------------------------------------------- | |
// 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 ); | |
// Product category with ID 42 | |
$variable = get_field( 'field_name', 'product-cat_42' ); | |
// User with ID 24 | |
$variable = get_field( 'field_name', 'user_24' ); | |
//------------------------------------------------------- | |
// 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"/> | |
//------------------------------------------------------- | |
// Print Image manual | |
//------------------------------------------------------- | |
$image_id = 12; | |
// or with custom field | |
$image_id = get_field( 'featured_image', get_the_ID() ); | |
$image_attr = array( | |
'src' => wp_get_attachment_image_url( $image_id, 'full' ), | |
'srcset' => wp_get_attachment_image_srcset( $image_id ), | |
'alt' => get_post_meta( $image_id, '_wp_attachment_image_alt', true ), | |
'title' => the_title_attribute( [ | |
'post' => $image_id, | |
'echo' => 0, | |
] ), | |
); | |
// Print image: | |
// <img src="<?php echo $image_attr['src'] ?>" srcset="<?php echo $image_attr['srcset'] ?>" alt="<?php echo $image_attr['alt'] ?>" title="<?php echo $image_attr['title'] ?>" /> | |
// Todo: Close the php closing tag. Not closed here because gist | |
//------------------------------------------------------- | |
//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