Problems creating a ShortCode
-
Hi,
I´m trying to develop a shortcode but I´m getting always the same problem, the shortcode is not calling to post, in this case is getting just the value of the actual page. I give you an example:
I want to display the title of two post passing the ids in the home, like this :[tsa_products id=”12,21″], but it just get the title of the home page, also in the case of display al post from a same category, like this: [list-posts category=”prueba”].
——Home inside WordPress admin—
[tsa_products id=”16,26″][list-posts category=”prueba”]
——home php:——–<?php /** * Template Name: Home */ get_header(); ?> <div class="container"> <div class="row"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php the_content(); ?> <?php endwhile;?> <?php endif; ?> </div> </div> <?php get_footer(); ?> ----My functions.php ----- add_shortcode('tsa_products', 'tsa_products_shortcode'); function tsa_products_shortcode( $atts ) { $atts = shortcode_atts( array( 'id' => '', ), $atts ); // $attributes['id'] is now the passed-in ID $html_out = ''; $html_out .= '<div class="col-md-4 col-sm-6 col-xs-6">'; $html_out .= '<div class="product-col-box">'; $html_out .= '<div class="product-image">'; $html_out .= '<img src="' . get_post_thumbnail_id() . '" />'; $html_out .= '</div>'; $html_out .= '<div class="title-product">'; $html_out .= '<h4>' . get_the_title($id) . '</h4>'; $html_out .= '</div>'; $html_out .= '</div>'; $html_out .= '</div>'; return $html_out; } add_shortcode( 'list-posts', 'rmcc_post_listing_parameters_shortcode' ); function rmcc_post_listing_parameters_shortcode( $atts ) { ob_start(); // define attributes and their defaults extract( shortcode_atts( array ( 'type' => 'post', 'orderby' => 'title', 'posts' => -1, 'category' => '', ), $atts ) ); // define query parameters based on attributes $options = array( 'post_type' => $type, 'orderby' => $orderby, 'posts_per_page' => $posts, 'category_name' => $category, ); $query = new WP_Query( $options ); // run the loop based on the query if ( $query->have_posts() ) { ?> <ul class="clothes-listing "> <ul class="clothes-listing "> <li id="post-<?php the_ID(); ?>"><?php the_title(); ?></li> </ul> </ul> <?php $myvariable = ob_get_clean(); return $myvariable; } }
[Moderated: code moderated – follow the forum guidelines for posting code – https://make.wordpress.org/support/handbook/forum-welcome/#post-code-safely%5D
Thank you
- This topic was modified 6 years, 5 months ago by .
- This topic was modified 6 years, 5 months ago by .
- This topic was modified 6 years, 5 months ago by .
The page I need help with: [log in to see the link]
- The topic ‘Problems creating a ShortCode’ is closed to new replies.