【问题标题】:Wordpress - If Statement in variableWordpress - 变量中的 If 语句
【发布时间】: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


    【解决方案1】:
    $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>';
    }
    

    【讨论】:

    • 在最后一个 $replace . = ... 中缺少 &lt;/a&gt; 并且,这是正确的 :)
    • 根据需要调整。很高兴听到它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2021-08-13
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多