在WordPress主题中对single.php的一些自定义修改中需要帮助 [英] Need Help in Some Custom Modification to single.php in a WordPress theme

查看:47
本文介绍了在WordPress主题中对single.php的一些自定义修改中需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Custom Field Plugin Pro. I created 3 Fields and 4 Sub Fields of that. Now when I am trying to add the code in single.php WordPress theme editor, then it breaking posts page CSS.

弄清问题后,我发现::当我发布只包含1个域代码的PHP代码时,网站不会中断.但是2/3破坏了网站.另外,我发现,当我在每个字段中输入1-1条目时,网站对所有3个字段的代码都可以正常工作.我不知道为什么会这样.

After figuring things I found that: when I post the PHP code of just 1 field code then the website doesn't break. But putting 2/3 breaking the website. Also, I found when I put 1-1 entry in each fields then the website works fine with all 3 fields code. I don't know why this is happening.

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

This is the code I am using in 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,则网站绝对正确:

If I put just code for field 1 then website is absolutely fine:

<!-- 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>

推荐答案

请注意字段名称.我觉得你的名字错了.然后,您的 div if语句中打开,但是在您的 if语句之后关闭,并且如果该语句破坏了html结构是错误的.顺便说一句,您可以在这里阅读有关中继器字段的用法: https://www.advancedcustomfields.com/resources/repeater/

Be careful with the names of your fields. I think your names are wrong. Then your div opens within your if statement but closes after your if statement and destroys your html structure if the statement is false. By the way, here you can read about the usage of repeater fields: 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 -->

这篇关于在WordPress主题中对single.php的一些自定义修改中需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆