jeudi 19 mars 2015

Wordpress - If Statement in variable

I'm trying to show 3 random categories in my posts.


I use Advanced Custom Fieds plugin to show category image. The if statement is:



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>';


And, i'm trying to show this using wp_list_categories, so i added in functions.php file this lines:



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;
}


How can save the if statement in $replace variable?


Aucun commentaire:

Enregistrer un commentaire