7

I created a page template to list all products of a specific product line. Now I want to list all posts from this custom post type (products) based on the taxonomy described in the shortcode for each page.

Example:

Page "List of all Prime products"

[products line="prime"]

I tried this code:

function shortcode_mostra_produtos ( $atts ) {
  $atts = shortcode_atts( array(
    'default' => ''
  ), $atts );
    $terms = get_terms('linhas');
    wp_reset_query();
    $args = array('post_type' => 'produtos',
      'tax_query' => array(
        array(
          'taxonomy' => 'linhas',
          'field' => 'slug',
          'terms' => $atts,
        ),
      ),
     );
     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        while($loop->have_posts()) : $loop->the_post();
            echo ' "'.get_the_title().'" ';
        endwhile;
     }
}
add_shortcode( 'produtos','shortcode_mostra_produtos' );
3
  • 2
    Have you attempted to do this or have any code to show?
    – Howdy_McGee
    Commented Jul 21, 2016 at 19:07
  • I tried this: gist.github.com/luanpiegas/51560598cbee58e6881531ee003feab0
    – Luan
    Commented Jul 21, 2016 at 19:09
  • 1
    That's a good start - people here are somtimes more inclined to answer questions when they know that the asker has attempted a solution ( and are not just asking for code ). It may be beneficial to edit your question and add that code snippet :)
    – Howdy_McGee
    Commented Jul 21, 2016 at 19:18

3 Answers 3

9

First off, it's always good to register shortcode during init versus just in your general functions.php file. At the very least add_shortcode() should be in init. Anyway, let's begin!

Whenever you use add_shortcode() the first parameter is going to be the name of the shortcode and the 2nd will be the callback function. This means that:

[products line="prime"]

Should be instead:

[produtos line="prime"]

So far we have this:

/**
 * Register all shortcodes
 *
 * @return null
 */
function register_shortcodes() {
    add_shortcode( 'produtos', 'shortcode_mostra_produtos' );
}
add_action( 'init', 'register_shortcodes' );

/**
 * Produtos Shortcode Callback
 * - [produtos]
 * 
 * @param Array $atts
 *
 * @return string
 */
function shortcode_mostra_produtos( $atts ) {
    /** Our outline will go here
}

Let's take a look at processing attributes. The way shortcode_atts() works is that it will try to match attributes passed to the shortcode with attributes in the passed array, left side being the key and the right side being the defaults. So we need to change defaults to line instead - if we want to default to a category, this would be the place:

$atts = shortcode_atts( array(
    'line' => ''
), $atts );

IF the user adds a attribute to the shortcode line="test" then our array index line will hold test:

echo $atts['line']; // Prints 'test'

All other attributes will be ignored unless we add them to the shortcode_atts() array. Finally it's just the WP_Query and printing what you need:

/**
 * Register all shortcodes
 *
 * @return null
 */
function register_shortcodes() {
    add_shortcode( 'produtos', 'shortcode_mostra_produtos' );
}
add_action( 'init', 'register_shortcodes' );

/**
 * Produtos Shortcode Callback
 * 
 * @param Array $atts
 *
 * @return string
 */
function shortcode_mostra_produtos( $atts ) {
    global $wp_query,
        $post;

    $atts = shortcode_atts( array(
        'line' => ''
    ), $atts );

    $loop = new WP_Query( array(
        'posts_per_page'    => 200,
        'post_type'         => 'produtos',
        'orderby'           => 'menu_order title',
        'order'             => 'ASC',
        'tax_query'         => array( array(
            'taxonomy'  => 'linhas',
            'field'     => 'slug',
            'terms'     => array( sanitize_title( $atts['line'] ) )
        ) )
    ) );

    if( ! $loop->have_posts() ) {
        return false;
    }

    while( $loop->have_posts() ) {
        $loop->the_post();
        echo the_title();
    }

    wp_reset_postdata();
}
0
0
    add_shortcode( 'product-list','bpo_product_list' );
function bpo_product_list ( $atts ) {
  $atts = shortcode_atts( array(
    'category' => ''
  ), $atts );
    $terms = get_terms('product_category');
    wp_reset_query();
    $args = array('post_type' => 'product',
      'tax_query' => array(
        array(
          'taxonomy' => 'product_category',
          'field' => 'slug',
          'terms' => $atts,
        ),
      ),
     );
     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        while($loop->have_posts()) : $loop->the_post();
            echo ' "'.get_the_title().'" ';
        endwhile;
     }

     else {
            echo  'Sorry, no posts were found';
          }
}

In above code, I have created product CPT and product_category taxonomy for product CPT.

[product-list category="shirts"]

The above code is perfectly works!

-1

**Try this **

function shortcode_bws_quiz_maker($id)
{
  if($id!='')
  {
    $post_id=$id[0];
    $html='';
    global $wpdb;
   $args=array('post_type'=>'post_type','p'=>$post_id);
   $wp_posts=new WP_Query($args);
   $posts=$wp_posts->posts;
  $html.="What you to get write here";
    return $html;

  }
  else
  {
    return 'Please enter correct shortcode';    
  }

}
add_shortcode('bws_quiz_maker','shortcode_bws_quiz_maker');
2
  • Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Commented Jun 20, 2017 at 8:23
  • This answer does not answer the OP at all - it seems completely unrelated. It also lacks any explanation of what it is supposed to do or how to use it.
    – butlerblog
    Commented Nov 10, 2018 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.