从wordpress中的模板帖子中获取图像

时间:2014-02-27 17:30:51

标签: php wordpress shortcode

我使用了一个名为个人募捐活动的wordpress的众筹插件,你可以看到here。它使用一个名为cause的post类型和另一个名为campaign的post。广告系列帖子使用原因帖子作为模板。我在这里问的原因是插件开发人员每两个月左右回答一次问题。

我想改变一个短代码的内容。我想要的第一件事就是获得活动形象。我可能需要一个代码来从相关原因(模板)中获取它。我想要的第二件事是在你看到的$list_content中回显短代码。我如何在那里添加短代码?如果我使用$list_content .= '<?php echo do_shortcode( $content ) ?>'我得到php错误。我想要更改的代码如下所示:

`function pfund_campaign_list() {
global $wp_query;
wp_enqueue_style( 'pfund-user', pfund_determine_file_location('user','css'),
array(), PFUND_VERSION );
$post_query = array(
'post_type' => 'pfund_campaign',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);

if ( isset( $wp_query->query_vars['pfund_cause_id'] ) ) {
$post_query['meta_query'] = array(
array(
'key' => '_pfund_cause_id',
'value' => $wp_query->query_vars['pfund_cause_id']
)
);
}
$campaigns = get_posts($post_query);
$list_content = '<ul class="pfund-list">';
foreach ($campaigns as $campaign) {
$list_content .= '<li>';
$list_content .= ' <h2>';
$list_content .= ' <a href="'.get_permalink($campaign->ID).'">'.$campaign->post_title.'</a></h2>';
$list_content .= '</li>';

}
$list_content .= '</ul>';
return $list_content;
}`

在列出原因的另一个函数中,此代码用于获取原因图像,但是当我在campaign_list中使用它时,没有任何反应:

`$cause_img = get_post_meta($cause->ID, '_pfund_cause_image', true);
if ( $cause_img ) {
$list_content .= '<img class="pfund-image" width="100%" src="'.wp_get_attachment_url( $cause_img ).'"/>';
}`

您可以看到整个.php文件here

任何帮助都是非常苛刻的。

1 个答案:

答案 0 :(得分:0)

要在主题中获取短代码,请执行此操作

$list_content .= do_shortcode($content);

然后你可以改变你的$ list_content,你不能回应预定义的变量。

取代

$list_content .= '<?php echo do_shortcode( $content ) ?>'