我有这个代码直接在一个页面上工作。但当我尝试将其添加到函数中的一个短代码时。php它不工作。它不会吸引作者(或日期)。
<?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;
}
?>
SO网友:Karen
如果有人需要,下面是我的最终代码:
<?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;
}
?>