【发布时间】:2015-03-20 01:37:28
【问题描述】:
我正在尝试在我的帖子中显示 3 个随机类别。
我使用Advanced Custom Fieds 插件来显示类别图像。 if 语句是:
if ( get_field( 'portada', 'category_' . $cats->term_id ) ) {
echo '<a class="cover margin-ultimos" href="' . get_category_link( $cats->term_id ) . '">';
echo '<div id="cover-home" class="gray-shadow">';
echo '<img src="'. get_field( 'portada', 'category_'.$cats->term_id ). '" alt="Portada'. get_cat_name ( $cats->term_id ) . '" />';
echo '</div>';
而且,我正在尝试使用 wp_list_categories 来展示这一点,所以我在 functions.php 文件中添加了以下几行:
add_filter ( 'wp_list_categories', 'img_before_link_list_categories' );
function img_before_link_list_categories( $list ) {
$cats = get_categories();
foreach($cats as $cat) {
$find = $cat->name.'</a>';
$replace = '//Here the If Statement to show the image';
$list = str_replace( $find, $replace, $list );
$list = preg_replace('%<li class=".*">|</?ul>%U', '<h2>', $list);
$list = str_replace('</li>', '</h2>', $list);
}
return $list;
}
$replace变量中的if语句如何保存?
【问题讨论】:
标签: php wordpress variables if-statement