admin管理员组

文章数量:1290955

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I have this code working directly on a page. But when I try to add it to a shortcode in the function.php it is not working. It will not pull in the author (or the date).

<?php 
//LIST ALL DOCUMENTS
add_shortcode('listDocs','listDocs');
function listDocs(){
    $params = array(
        'paged' => get_query_var('paged', 1),
        'posts_per_page' => 25, 
        'post_type' => 'product',
        'product_cat' => 'documents'
      );
      $product = wc_get_product();
      $author = $product->get_attribute('document-type');
      $date = get_the_date('F Y', $product->get_id());
  $wc_query = new WP_Query($params);
      while ($wc_query->have_posts()) {
           $wc_query->the_post(); 
           $output .= '<h3 style="padding: 10px 0px 0px 0px;margin: 0px;"> <a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3>
           <p>' . get_field('document_type') . ' / ' .  $author . ' / ' . $date . '</p><hr>';
        }

        return $output; 


}
    ?>
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I have this code working directly on a page. But when I try to add it to a shortcode in the function.php it is not working. It will not pull in the author (or the date).

<?php 
//LIST ALL DOCUMENTS
add_shortcode('listDocs','listDocs');
function listDocs(){
    $params = array(
        'paged' => get_query_var('paged', 1),
        'posts_per_page' => 25, 
        'post_type' => 'product',
        'product_cat' => 'documents'
      );
      $product = wc_get_product();
      $author = $product->get_attribute('document-type');
      $date = get_the_date('F Y', $product->get_id());
  $wc_query = new WP_Query($params);
      while ($wc_query->have_posts()) {
           $wc_query->the_post(); 
           $output .= '<h3 style="padding: 10px 0px 0px 0px;margin: 0px;"> <a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3>
           <p>' . get_field('document_type') . ' / ' .  $author . ' / ' . $date . '</p><hr>';
        }

        return $output; 


}
    ?>
Share Improve this question edited Jun 15, 2021 at 18:08 vancoder 7,92428 silver badges35 bronze badges asked Jun 15, 2021 at 15:48 KarenKaren 112 bronze badges 4
  • 1 Where are you using the shortcode? – Jacob Peattie Commented Jun 15, 2021 at 15:58
  • I am putting it on a page that will list specific products - in my case documents. – Karen Commented Jun 15, 2021 at 16:00
  • You code uses wc_get_product(). If you’re using it on a page, which product? – Jacob Peattie Commented Jun 15, 2021 at 16:13
  • Thank you! I got it working by adding: $product_id = get_the_ID(); $product = wc_get_product($product_id); – Karen Commented Jun 15, 2021 at 17:15
Add a comment  | 

1 Answer 1

Reset to default 1

In case anyone needs it, here was my final code:

<?php 
//LIST ALL DOCUMENTS
add_shortcode('listDocs','listDocs');
function listDocs(){
    $params = array(
        'paged' => get_query_var('paged', 1),
        'posts_per_page' => 25, 
        'post_type' => 'product',
        'product_cat' => 'documents'
      );
  $wc_query = new WP_Query($params);
      while ($wc_query->have_posts()) {
           $wc_query->the_post(); 
           $product_id = get_the_ID();
           $product = wc_get_product($product_id);
           $author = $product->get_attribute('written-by');
           $date = get_the_date('F Y', $product_id);
           $output .= '<h3 style="padding: 10px 0px 0px 0px;margin: 0px;"> <a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3>
           <p>' . get_field('document_type') . ' / ' .  $author . ' / ' . $date . '</p><hr>';
        }

        return $output; 


}
    ?>

本文标签: woocommerce offtopicUnable to pull in custom product attribute in shortcode