为什么我的代码的输出包含额外的

代码中未指定时是否需要标记?

发布于 2024-11-04 04:08:17 字数 2720 浏览 1 评论 0原文

我是 PHP 编码新手。我对这个很困惑。我正在为我的 WordPress 网站创建一些短代码。基本上,我创建了许多工作正常的短代码,但是当我开始编码这个时,输出包含不必要的 << br>且< p>标签。

这是我的代码的一部分:

function searchbox ($atts) {
global $wpdb;
$link = get_bloginfo('url').'/search/';
$out .= '<form name="search" action="'.$link.'" method="get">';
$out .= '<label>Developer:</label>';
$out .= '<select name="dev">';
$developers = $wpdb->get_col($wpdb->prepare("
        SELECT DISTINCT meta_value 
        FROM $wpdb->postmeta 
        WHERE meta_key = %s 
        ORDER BY meta_value ASC", 'developer') );
 if ($developers) {
   foreach ($developers as $developer) {
     $out .= "<option value=\"" . $developer . "\">" . $developer . "</option>";
   }
 }
$out .= '</select>';
$out .= '<label>Location</label><select name="loc">';
$locs = $wpdb->get_col($wpdb->prepare("
        SELECT DISTINCT meta_value 
        FROM $wpdb->postmeta 
        WHERE meta_key = %s 
        ORDER BY meta_value ASC", 'loc') );
 if ($locs) {
   foreach ($locs as $loc) {
     $out .= "<option value=\"" . $loc . "\">" . $loc . "</option>";
   }
 }
$out .= '</select>';
$out .= 'Price Range: <input type="text" size="4" name="pmin"> '
        +'to <input type="text" size="4" name="pmax">';  
$out .=  '<input type="submit" value="search" />';
$out .= '</form>';
return $out;
}
add_shortcode ('searchbox', 'searchbox');

然后将短代码 [searchbox] 添加到 WP 编辑器中。输出 html 代码为:

<form name="search" action="http://www.mycondophilippines.com/search/" method="get">
    <label>Developer:</label><br /> 
    <select name="dev"> 
        <option value="Avida Land">Avida Land</option> 
        <option value="DMCI Homes">DMCI Homes</option> 
        <option value="SMDC">SMDC</option> 
    </select> 
    <p><label>Location</label><br /> 
    <select name="loc"> 
        <option value="Makati">Makati</option> 
        <option value="Mandaluyong">Mandaluyong</option> 
        <option value="Manila">Manila</option> 
    </select> 
    <p>Price Range:<br /> 
    <input type="text" size="4" name="pmin"> to<br /> 
    <input type="text" size="4" name="pmax"> 
    <input type="submit" value="search" />
</form> 

注意添加的 br 和 p 标签。我观察到,当在 html 代码上添加文本时,它会自动添加

标签。我对这个感到困惑,因为我制作的简码函数位于一个 php 文件中,而其他函数则可以很好地处理 html 代码上的文本。正是这个函数添加了标签。

我使用 Dreamweaver CS5 进行编码。但我尝试用notepad2编辑但结果相同。

I'm new to PHP coding. I'm confused about this one. I am creating some shortcodes for my wordpress site. Basically, I have created a number of shortcodes that work fine, but when I start coding this one, the output contains unnecessary < br> and < p> tags.

here's a portion of my code:

function searchbox ($atts) {
global $wpdb;
$link = get_bloginfo('url').'/search/';
$out .= '<form name="search" action="'.$link.'" method="get">';
$out .= '<label>Developer:</label>';
$out .= '<select name="dev">';
$developers = $wpdb->get_col($wpdb->prepare("
        SELECT DISTINCT meta_value 
        FROM $wpdb->postmeta 
        WHERE meta_key = %s 
        ORDER BY meta_value ASC", 'developer') );
 if ($developers) {
   foreach ($developers as $developer) {
     $out .= "<option value=\"" . $developer . "\">" . $developer . "</option>";
   }
 }
$out .= '</select>';
$out .= '<label>Location</label><select name="loc">';
$locs = $wpdb->get_col($wpdb->prepare("
        SELECT DISTINCT meta_value 
        FROM $wpdb->postmeta 
        WHERE meta_key = %s 
        ORDER BY meta_value ASC", 'loc') );
 if ($locs) {
   foreach ($locs as $loc) {
     $out .= "<option value=\"" . $loc . "\">" . $loc . "</option>";
   }
 }
$out .= '</select>';
$out .= 'Price Range: <input type="text" size="4" name="pmin"> '
        +'to <input type="text" size="4" name="pmax">';  
$out .=  '<input type="submit" value="search" />';
$out .= '</form>';
return $out;
}
add_shortcode ('searchbox', 'searchbox');

then the shortcode [searchbox] is added in the WP editor. the output html code is:

<form name="search" action="http://www.mycondophilippines.com/search/" method="get">
    <label>Developer:</label><br /> 
    <select name="dev"> 
        <option value="Avida Land">Avida Land</option> 
        <option value="DMCI Homes">DMCI Homes</option> 
        <option value="SMDC">SMDC</option> 
    </select> 
    <p><label>Location</label><br /> 
    <select name="loc"> 
        <option value="Makati">Makati</option> 
        <option value="Mandaluyong">Mandaluyong</option> 
        <option value="Manila">Manila</option> 
    </select> 
    <p>Price Range:<br /> 
    <input type="text" size="4" name="pmin"> to<br /> 
    <input type="text" size="4" name="pmax"> 
    <input type="submit" value="search" />
</form> 

Notice the added br and p tags. I observed that when a text is added on the html code, it automatically adds <br> or <p> tags. I'm confused with this one, because the shortcode functions that I make are in one php file, and the others work fine with text on html code. It is this function that adds the tags.

I use Dreamweaver CS5 for coding. But I tried to edit with notepad2 but with the same result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初熏 2024-11-11 04:08:17

WordPress 在页面/帖子编辑器中添加了换行


。我假设这就是您输入短代码的地方。

更新

OP找到了一个解决方案 - http://www .simonbattersby.com/blog/2009/08/stop-wordpress-adding-br-tags/

WordPress adds the wrapping <p> and <br /> in the page/post editor. I'm assuming that's where you're entering your shortcode.

UPDATE

The OP found a solution - http://www.simonbattersby.com/blog/2009/08/stop-wordpress-adding-br-tags/

王权女流氓 2024-11-11 04:08:17

您只需在主题文件 (function.php) 中添加一个函数即可。
这样,当您升级 WP 源代码时,您就不必考虑重新更改该功能。

通过使用 WP codex(http://codex.wordpress.org/Function_Reference/wpautop)

只需添加:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

You can simply add a function in your theme file (function.php).
That way when you upgrade WP source you won't have to think about re-changing that function.

by using the WP codex(http://codex.wordpress.org/Function_Reference/wpautop)

just add:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
无声无音无过去 2024-11-11 04:08:17

在将生成的 html 发送到客户端之前,您必须通过美化函数传递它(htmlpurifier?)。输出有换行符,而您的代码根本没有插入任何换行符,这意味着有一些东西修改了您的输出。

您可能还想查找 HEREDOC,它可以让您创建多行字符串,而无需任何重复字符串的麻烦冲突或引用转义。

You must be passing your generated html through a beautification function before sending it to the client (htmlpurifier?). The output has linebreaks, while your code does not insert any at all, which means there's something modifying your output.

You may also want to look up HEREDOCs, which let you create multi-line strings without any of the hassles of repeated string contatenations or quote escaping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文