【问题标题】:Need Help in Some Custom Modification to single.php in a WordPress theme在 WordPress 主题中对 single.php 进行一些自定义修改时需要帮助
【发布时间】:2020-06-14 16:19:00
【问题描述】:

我正在使用自定义字段插件专业版。我创建了 3 个字段和 4 个子字段。现在,当我尝试在 single.php WordPress 主题编辑器中添加代码时,它会破坏帖子页面的 CSS。

在弄清楚事情之后,我发现:当我发布只有 1 个字段代码的 PHP 代码时,网站不会中断。但是把 2/3 打破了网站。此外,我发现当我在每个字段中输入 1-1 个条目时,网站在所有 3 个字段代码中都可以正常工作。我不知道为什么会这样。

这是我在 single.php 中使用的代码

<!-- Show Custom Fiels---------------->
<div id="Custom_Fields" class="sbox fixidtab">
<style>
table, th, td {
border: 1px solid white;
border-collapse: collapse;
background:black;
}
th, td {
padding: 5px;
text-align: center;
}
</style>
<?php
// Field 1 -----
if( have_rows('Field1') ):?>
<div class="custom_fields" style="overflow-x:auto;">
<h2><?php _d('Type : Name'); ?></h2>

<table style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Type 1</th>
            <th>Type 2</th>
            <th>Type 3</th>
            <th>Type 4</th>
        </tr>
    </thead>
<tbody>
<?php while ( have_rows('F1') ) : the_row();
        $name_F1 = get_sub_field('name');
        $type_F1 = get_sub_field('type');
        $link_F1 = get_sub_field('link');
        $link_F1_full = "<a href=".$link_F1." target='_blank'>Visit Now</a>";
        $language_F1 = get_sub_field('language');
?>
    <tr>
        <td><?php echo $name_F1; ?></td>
        <td><?php echo $type_F1; ?></td>
        <td><?php echo $link_F1_full; ?></td>
        <td><?php echo $language_F1; ?></td>
    </tr>
<?php
    endwhile;
?>
</tbody>
</table>
<?php
    //endwhile;

else :

    // no rows found

endif;

?>
</div>
<?php
// Field 2 -----
if( have_rows('Field 2') ):?>
<div class="custom_fields" style="overflow-x:auto;">
<h2><?php _d('Type : Name'); ?></h2>

<table style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Type 1</th>
            <th>Type 2</th>
            <th>Type 3</th>
            <th>Type 4</th>
        </tr>
    </thead>
<tbody>
<?php while ( have_rows('F2') ) : the_row();
        $name_F2 = get_sub_field('name');
        $type_F2 = get_sub_field('type');
        $link_F2 = get_sub_field('link');
        $link_F2_full = "<a href=".$link_F2." target='_blank'>Visit Now</a>";
        $language_F2 = get_sub_field('language');
?>
    <tr>
        <td><?php echo $name_F2; ?></td>
        <td><?php echo $type_F2; ?></td>
        <td><?php echo $link_F2_full; ?></td>
        <td><?php echo $language_F2; ?></td>
    </tr>
<?php
    endwhile;
?>
</tbody>
</table>
<?php
    //endwhile;

else :

    // no rows found

endif;

?>
</div>
<?php
// Field 3 -----
if( have_rows('Field 3') ):?>
<div class="custom_fields" style="overflow-x:auto;">
<h2><?php _d('Type : Name'); ?></h2>

<table style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Type 1</th>
            <th>Type 2</th>
            <th>Type 3</th>
            <th>Type 4</th>
        </tr>
    </thead>
<tbody>
<?php while ( have_rows('F3') ) : the_row();
        $name_F3 = get_sub_field('name');
        $type_F3 = get_sub_field('type');
        $link_F3 = get_sub_field('link');
        $link_F3_full = "<a href=".$link_F3." target='_blank'>Visit Now</a>";
        $language_F3 = get_sub_field('language');
?>
    <tr>
        <td><?php echo $name_F3; ?></td>
        <td><?php echo $type_F3; ?></td>
        <td><?php echo $link_F3_full; ?></td>
        <td><?php echo $language_F3; ?></td>
    </tr>
<?php
    endwhile;
?>
</tbody>
</table>
<?php
    //endwhile;

else :

    // no rows found

endif;

?>
</div>
</div>

如果我只为字段 1 输入代码,那么网站绝对没问题:

<!-- Show Custom Fiels---------------->
<div id="Custom_Fields" class="sbox fixidtab">
<style>
table, th, td {
border: 1px solid white;
border-collapse: collapse;
background:black;
}
th, td {
padding: 5px;
text-align: center;
}
</style>
<?php
// Field 1 -----
if( have_rows('Field1') ):?>
<div class="custom_fields" style="overflow-x:auto;">
<h2><?php _d('Type : Name'); ?></h2>

<table style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Type 1</th>
            <th>Type 2</th>
            <th>Type 3</th>
            <th>Type 4</th>
        </tr>
    </thead>
<tbody>
<?php while ( have_rows('F1') ) : the_row();
        $name_F1 = get_sub_field('name');
        $type_F1 = get_sub_field('type');
        $link_F1 = get_sub_field('link');
        $link_F1_full = "<a href=".$link_F1." target='_blank'>Visit Now</a>";
        $language_F1 = get_sub_field('language');
?>
    <tr>
        <td><?php echo $name_F1; ?></td>
        <td><?php echo $type_F1; ?></td>
        <td><?php echo $link_F1_full; ?></td>
        <td><?php echo $language_F1; ?></td>
    </tr>
<?php
    endwhile;
?>
</tbody>
</table>
<?php
    //endwhile;

else :

    // no rows found

endif;

?>
</div>
</div>

【问题讨论】:

    标签: php html css wordpress


    【解决方案1】:

    请注意字段名称。我认为你的名字是错误的。然后您的div 在您的if statement 内打开,但在您的if statementdestroys your html structure 之后关闭,如果该语句为假。顺便说一下,在这里你可以阅读关于转发器字段的使用:https://www.advancedcustomfields.com/resources/repeater/

    看这个例子:

    <?php
    // Field 1 -----
    if( have_rows('F1') ): // i think it has to be F1 instead of Field1 here? 
    ?>
    <div class="custom_fields" style="overflow-x:auto;">
    <h2><?php _d('Type : Name'); ?></h2>
    
    <table style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Type 1</th>
                <th>Type 2</th>
                <th>Type 3</th>
                <th>Type 4</th>
            </tr>
        </thead>
    <tbody>
    <?php while ( have_rows('F1') ) : the_row();
            $name_F1 = get_sub_field('name');
            $type_F1 = get_sub_field('type');
            $link_F1 = get_sub_field('link');
            $link_F1_full = "<a href=".$link_F1." target='_blank'>Visit Now</a>";
            $language_F1 = get_sub_field('language');
    ?>
        <tr>
            <td><?php echo $name_F1; ?></td>
            <td><?php echo $type_F1; ?></td>
            <td><?php echo $link_F1_full; ?></td>
            <td><?php echo $language_F1; ?></td>
        </tr>
    <?php
        endwhile;
    ?>
    </tbody>
    </table>
    </div><!-- RIGHT HERE -->
    <?php
        //endwhile;
    else:
    
        // no rows found
    endif;
    
    ?>
    <!--</div> WRONG HERE -->
    

    【讨论】:

    • 你是个救命恩人。非常感谢。我没有注意到这种方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多