Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1290955
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 questionI 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 questionI 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 Answer
Reset to default 1In 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
版权声明:本文标题:woocommerce offtopic - Unable to pull in custom product attribute in shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741502449a2382126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wc_get_product()
. If you’re using it on a page, which product? – Jacob Peattie Commented Jun 15, 2021 at 16:13