【问题标题】:style a html table inside functions.php wordpress在functions.php wordpress中设置html表格的样式
【发布时间】:2017-06-20 01:32:03
【问题描述】:

我在functions.php 文件上创建了一个函数,该函数从我们数据库中的表中检索数据并将其放在HTML 中。

我的问题是我需要设置这个表格的样式,它的列和行以设置边距、填充等。

这是我目前所拥有的:

add_shortcode( 'persona-table', 'persona_table_shortcode' );

// this function generates the shortcode output
function persona_table_shortcode( $args ) {
    global $wpdb;
    // Shortcodes RETURN content, so store in a variable to return
    $content = '<table class="tabledatos">';
        $content .= '<tr><th>Firstname</th><th>Lastname</th><th>Points</th></tr>';
        $results = $wpdb->get_results( 'SELECT * FROM persona' );
        foreach ( $results AS $row ) {
            $content = '<tr>';
                // Modify these to match the database structure
                $content .= '<td class=rowtable>' . $row->ID_per . '</td>';
                $content .= '<td class=rowtable>' . $row->nombre . '</td>';
                $content .= '<td class=rowtable>' . $row->apellido . '</td>';
                $content .= '<td class=rowtable>' . $row->documento . '</td>';
                $content .= '<td class=rowtable>' . $row->tel. '</td>';
            $content .= '<tr>';
        }
    $content .= '</table>';
    // return the table
    return $content;
} 

正如您在每个&lt;td&gt; 上看到的那样,我添加了一个名为:rowtable 的类,但是当我刷新页面时,表格不会采用该类上定义的样式。另外,我尝试使用引号,正如您在整个表的 tabledatos 类中看到的那样,但这不起作用。

如何向此代码添加类,以便在 style.css 文件中自定义它们?

在此致谢

【问题讨论】:

  • 你确定你有rowtable类吗?开发者控制台中有任何线索吗?

标签: php html css mysql wordpress


【解决方案1】:

首先,带有内容的 HTML 属性总是需要引号,因此您的行应该如下所示:$content .= '&lt;td class="rowtable"&gt;' . $row-&gt;ID_per . '&lt;/td&gt;';

您应该验证您的样式表是否已按预期加载,并在您的浏览器的开发工具中检查该类是否已添加到td

在这里$content = '&lt;tr&gt;'; 你重新分配你的变量,这意味着你以前的内容会被刷新,并且对于你的 foreach 中的每个项目

【讨论】:

    【解决方案2】:

    我没有对你的问题给出完整的答案(很遗憾),我对PHP 也不是很好,但也许我可以帮你找到这个答案。

    首先,您是否检查过(使用浏览器的工具)您的页面中是否包含CSS 文件以及您的表格是否包含该类?如果你还没有,你现在应该检查一下。如果表格没有此类,请尝试手动添加(使用浏览器工具)并查看表格是否已更新。

    如果这第一步没有奏效,还有另一种方法可以为您的对象添加类。您可以使用成员函数.addClass("tabledatos"); 为您的对象添加一个类JavaScript

    希望你能在我的评论中找到有趣的东西

    【讨论】:

      猜你喜欢
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多