使用WordPress PHP我试图将值传递给元素的类标记
<div class="element-item"></div>
就像
<div class="element-item comedy"></div>
$term = get_the_term_list( get_the_ID(), 'type' );
echo '<div class="element-item '.$term.'">';
该值正在从类标记中删除并显示在页面上!
我检查了源代码,似乎我将整个链接传递给了class标签!
<div class="element-item " .<a="" href="http://www.domain.ca/type/meat/" rel="tag" style="position: absolute; left: 0px; top: 0px;">Canadian</div>
你可以告诉我为什么会这样,以及如何解决这个问题?
答案 0 :(得分:0)
试试这种方式,可能会对你有所帮助
$html_output = '<div class="element-item ' . $term . '">';
echo $html_output;
答案 1 :(得分:0)
试试这个
echo "<div class="element-item $term>";
或
你可以借助像这样的javascript来做到这一点
$(function(){
$('.element-item').addClass('<?php echo $term ?>');
});
如果您遇到任何问题,请告诉我
答案 2 :(得分:0)
您已根据返回值正确识别问题。
问题在于:
$term = get_the_term_list( get_the_ID(), 'type' );
get_the_term_list()
将返回HTML字符串,如函数文档中所述:https://codex.wordpress.org/Function_Reference/get_the_term_list
我建议采用以下方法:
// We're going to place all of our classes into this array.
$classes = array( 'element-item' );
// Get terms assigned to post.
$terms = get_the_terms( get_the_ID(), 'type' );
// Check terms were found.
if ( $terms && ! is_wp_error( $terms ) ) {
// Loop through each found term and add it to the array of classes.
foreach ( $terms as $term ) {
$classes[] = $term->slug;
}
}
// Convert the array of classes into a string separated by spaces.
// Escape the value before outputting inside the attribute tag.
printf( '<div class="%s">', esc_attr( implode( ' ', $classes ) ) );
进一步阅读:https://codex.wordpress.org/Function_Reference/get_the_terms