Wordpress - 变量中的 If 语句
Wordpress - If Statement in variable
我试图在我的帖子中显示 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;
}
如何将 if 语句保存在 $replace
变量中?
$replace = '';
if( get_field( 'portada', 'category_' . $cats->term_id ) )
{
$replace .= '<a class="cover margin-ultimos" href="' . get_category_link( $cats->term_id ) . '">';
$replace .= '<div id="cover-home" class="gray-shadow">';
$replace .= '<img src="'. get_field( 'portada', 'category_'.$cats->term_id ). '" alt="Portada'. get_cat_name ( $cats->term_id ) . '" />';
$replace .= '</div>';
$replace .= '</a>';
}
我试图在我的帖子中显示 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;
}
如何将 if 语句保存在 $replace
变量中?
$replace = '';
if( get_field( 'portada', 'category_' . $cats->term_id ) )
{
$replace .= '<a class="cover margin-ultimos" href="' . get_category_link( $cats->term_id ) . '">';
$replace .= '<div id="cover-home" class="gray-shadow">';
$replace .= '<img src="'. get_field( 'portada', 'category_'.$cats->term_id ). '" alt="Portada'. get_cat_name ( $cats->term_id ) . '" />';
$replace .= '</div>';
$replace .= '</a>';
}