我创建了一个自定义快捷码,用于向所选词汇表术语添加弹出定义。短代码如下所示:
[glossary term="data breach"]data breaches[/glossary]
该函数在名为“词汇表术语”的自定义帖子类型中搜索帖子,以获取术语的定义,将其打印在div中,并将所附文本转换为指向该div的链接:
function glossary_shortcode( $atts = array(), $shortcode_content = null ) {
// parameters
extract(shortcode_atts(array(
\'term\' => null
), $atts));
$glossary_term_object = get_page_by_title($term, \'OBJECT\', \'glossary-term\');
// print definition in popup div
echo \'<div id="glossary-popup-\' . $glossary_term_object->post_name . \'" class="glossary_popup"><h4>\' . $glossary_term_object->post_title . \'</h4>\' . wpautop( $glossary_term_object->post_content ) . \'<div class="ctas"><a href="">Visit Glossary</a><a href="#">Close</a></div></div>\';
// return link to definition popup
return \'<a href="#glossary-popup-\' . $glossary_term_object->post_name . \'">\' . $shortcode_content . \'</a>\';
}
add_shortcode(\'glossary\', \'glossary_shortcode\');
我不想重复弹出的定义,而是希望将此短代码创建的所有定义(可能在一个页面上使用多次)收集到主页内容末尾各自的div中。
有没有办法做到这一点?
SO网友:Tim
我想,我只需在glossary_shortcode()
在a中执行global
变量,并在钩子状的末端为其提供服务the_content
.
function glossary_shortcode( $atts = array(), $shortcode_content = null ) {
// Your code can be kept identical, just store the $glossary_term_object somewhere.
global $glossaries;
$glossaries = $glossaries ?? [];
$glossaries[] = glossary_term_object;
}
add_filter( \'the_content\', function ( $content ) {
global $glossaries;
if ( !empty( $glossaries ) ) {
echo \'<ul class="glossary">\';
foreach ( $glossaries as $glossary ) {
printf( \'<li><a href="%s" target="_blank">%s : %s</a></li>\',
get_the_permalink( $glossary ),
$glossary->post_title,
$glossary->post_content
);
}
echo \'</ul>\';
}
return $content;
}
SO网友:BoyKenan
这是解决方案,h/t给Tim,他让我95%的时间都在那里。
// Shortcode function
function glossary_shortcode( $atts = array(), $shortcode_content = null ) {
// Parameters: determine what post we\'re looking for
extract(shortcode_atts(array(
\'term\' => null
), $atts));
// Get requested post
$glossary_term_object = get_page_by_title($term, \'OBJECT\', \'glossary-term\');
// Add post to global variable
global $glossaries;
$glossaries = $glossaries ?? [];
$glossaries[] = $glossary_term_object;
// Link to the list item we\'ll build below.
return \'<a class="glossary-link toggle" href="#glossary-popup-\' . $glossary_term_object->post_name . \'">\' . $shortcode_content . \'</a>\';
}
// Register shortcode
add_shortcode(\'glossary\', \'glossary_shortcode\');
// Build our list of definitions. Set priority at 11+ or global variable will be empty.
add_filter( \'the_content\', function ( $content ) {
// Retrieve posts assembled above
global $glossaries;
if ( !empty( $glossaries ) ) {
$page_glossary = \'\';
// Format our posts content as a list of definitions
foreach ($glossaries as $glossary) {
$page_glossary .= \'<li id="glossary-popup-\' . $glossary->post_name . \'" class="glossary_popup"><h4>\' . $glossary->post_title . \'</h4>\' . wpautop( $glossary->post_content ) . \'<div class="ctas"><a href="">Visit Glossary</a><a class="close" href="#glossary-popup-\' . $glossary->post_name . \'">Close</a></div></li>\';
}
// Add the list to the end of the current post\'s content
$content .= \'<section id="page-glossary"><ul>\' . $page_glossary . \'</ul></section>\';
}
return $content;
}, 11 );