我正在尝试为短代码创建一个函数来循环遍历“文章”自定义帖子类型。这本质上是用户上传的可下载文件,是一个自定义帖子类型,没有帖子编辑器中的内容字段/编辑器框。此自定义帖子类型还附加了一个“download_link”ACF字段,该字段是下载文件的位置。这是Feed中每个帖子的href属性。但是,该字段不会在下面的函数中生成任何输出。然而,标题正在填充。我知道所有文章帖子都为此特定字段提供了值。我错过了什么吗?谢谢。
// CREATE A FEED OF ARTICLES WITH LINKS
function articles_download_feed(){
$articles_query = new WP_Query(array('post_type' => 'article',
'post_status' => 'any'));
if( $articles_query->have_posts() ){
$output = '<ul>';
while( $articles_query->have_posts() ){
$articles_query->the_post();
$articleID = get_the_ID();
$title = get_the_title($articleID);
if(get_field('download_link', $articleID)){
$download = get_field('download_link', $articleID, true);
} else {
$download = 'ABC';
}
$output .= '<li><a target="_blank" href="'.$download.'">'.$title.'</a></li>';
}
$output .= '</ul>';
wp_reset_postdata();
return $output;
}
} add_shortcode('articles_feed', 'articles_download_feed');
答案 0 :(得分:0)
我自己设法解决了这个问题。
我通过add_post_meta()WP函数为'article'类型的帖子设置'dile_dl_link'自定义字段,而不是通过高级自定义字段(下面是创建新文章的代码段):
// CREATE NEW ARTICLE LINK FROM GRAVITY FORM SUBMISSION
function article_upload_createlink($entry, $form){
// Fields
$post_title = ucwords($entry[8]);
$post_status = 'publish';
$post_excerpt = $entry[9];
$post_type = 'article';
$dl_link = $entry[3];
// Post being created
$new_article = array(
'post_title' => $post_title,
'post_status' => $post_status,
'post_excerpt' => $post_excerpt,
'post_type' => $post_type,
);
// ID for post
$theId = wp_insert_post($new_article);
// Update download_link
add_post_meta($theId, 'file_dl_link', $dl_link);
} add_action( 'gform_after_submission_7', 'article_upload_createlink', 10, 2 );
问题中提出的相关功能已更改如下:
// CREATE A FEED OF ARTICLES WITH LINKS
function articles_download_feed(){
$articles_query = new WP_Query(array('post_type' => 'article', 'post_status' => 'any'));
if( $articles_query->have_posts() ){
$output = '';
while( $articles_query->have_posts() ){
$articles_query->the_post();
$articleID = get_the_ID();
$title = get_the_title();
$excerpt = get_the_excerpt();
$get_dllink = get_post_meta($articleID, 'file_dl_link', true);
$output .= '<div class="wpb_wrapper">
<div class="vc_empty_space" style="height: 24px">
<span class="vc_empty_space_inner"></span>
</div>
<div class="wpb_text_column ">
<div class="wpb_wrapper">
<h4>'.$title.'</h4>
<p>'.$excerpt.'</p></div>
</div>
<div class="w-btn-wrapper align_left">
<a target="_blank" class="w-btn style_raised color_primary icon_none" href="'.$get_dllink.'"><span class="w-btn-label">DOWNLOAD</span><span class="ripple-container"></span></a>
</div>
</div>
<div class="w-separator type_default size_medium thick_1 style_solid color_border cont_none">
<span class="w-separator-h"></span>
</div>';
}
wp_reset_postdata();
return $output;
}
}
add_shortcode('articles_feed', 'articles_download_feed');