我在插件中编写了一个简短的代码,从一个分类术语、一个分类术语下的文章以及两个术语元中提取出一个自定义文章特征图像。
我希望该分类法的术语仅限于一定数量的字符,例如12个字符,但我下面的代码不会像我在下面尝试的那样截断该术语的长名称并添加省略号。
ob_start();
$post_type = \'esitykset\';
$taxonomy = \'tapahtumat\';
$post_ids = get_unique_term_recent_posts( $post_type, $taxonomy );
if ( $post_ids ) {
$args = [
\'post__in\' => $post_ids,
\'post_type\' => $post_type,
\'posts_per_page\' => 8
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
?>
<div class="home-poster-column">
<?php
$thumb = \'\';
$width = (int) apply_filters( \'et_pb_index_blog_image_width\', 724 );
$height = (int) apply_filters( \'et_pb_index_blog_image_height\', 1024 );
$classtext = \'et_pb_post_main_image\';
$titletext = get_the_title();
$thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, \'Blogimage\' );
$thumb = $thumbnail["thumb"];
?>
<?php
global $term_link;
if ( $terms = get_the_terms( $term_link->ID, \'tapahtumat\' ) ) {
// $term = $terms[0]; // WRONG! $terms is indexed by term ID!
$term = array_shift( $terms ); // RIGHT! Will get first term, and remove it from $terms array
}
?>
<a href="<?php echo get_term_link( $term ); ?>">
<span class="esitys-poster">
<?php print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height ); ?>
</span><br/>
<strong>
<?php
$event = the_terms( $post->ID, \'tapahtumat\');
$len = 10; // <-- Adjust to your needs!
echo mb_strimwidth($event, 0, $len, \'UTF8\' ) . \'…\';
?>
</strong> <br/>
<?php
/*
Get the date range meta of tapahtumat taxonomy
http://wordpress.stackexchange.com/questions/11820/echo-custom-taxonomy-field-values-outside-the-loop
*/
global $date_range;
if ( $terms = get_the_terms( $date_range->ID, \'tapahtumat\' ) ) {
// $term = $terms[0]; // WRONG! $terms is indexed by term ID!
$term = array_shift( $terms ); // RIGHT! Will get first term, and remove it from $terms array
echo get_term_meta( $term->term_id, \'date-range\', true ) . \'<br/>\';
}
/* Get the start price meta of tapahtumat taxonomy */
global $start_price;
if ( $terms = get_the_terms( $start_price->ID, \'tapahtumat\' ) ) {
// $term = $terms[0]; // WRONG! $terms is indexed by term ID!
$term = array_shift( $terms ); // RIGHT! Will get first term, and remove it from $terms array
echo get_term_meta( $term->term_id, \'start-price\', true );
}
?>
</a>
</div>
<?php
wp_reset_postdata(); } // endif have_posts()
} // endif $post_ids
$myvariable = ob_get_clean();
return $myvariable;
我知道上面的代码有点扭曲。如果您觉得更好的方法,请随时提出更好的方法。
谢谢
SO网友:birgire
这不起作用,因为the_terms()
正在回显输出。
减少冗长的术语名称,而不是直接在get_the_term_list()
用于get_the_terms()
. 这可能会产生无效的HTML,这可能会破坏网站的布局。
下面是相应主题文件的示例:
// Add a filter
add_filter( \'get_the_terms\', \'wpse248787_get_the_terms\' );
// Display terms
the_terms( get_the_ID(), \'tapahtumat\' );
// Remove the filter again
remove_filter( \'get_the_terms\', \'wpse248787_get_the_terms\' );
您定义了:
function wpse248787_get_the_terms( $terms )
{
$len = 10; // <-- Adjust to your needs!
// Limit term names if needed
foreach( $terms as $term )
{
if( $len > 0 && $len < mb_strlen( $term->name ) )
$term->name = mb_substr( $term->name, 0, $len - 1, \'UTF8\' ) . \'…\';
}
return $terms;
}
在
functions.php
当前主题目录中的文件。您可能还需要只添加编码参数或使用例如。
get_option( \'blog_charset\' )
.