如何更改此代码以将每个标题链接到帖子?
具体而言,该行:
$dl .= \'<dt>\' . $query->post->post_title . \'</dt>\';
谢谢你,
<?php
$args = array(
\'post_type\' => \'my_glossary_term\',
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'posts_per_page\' => -1
);
$query = new WP_Query( $args );
$dl = \'\';
$glossary_letter = \'\';
$active_letters = array( );
while ( $query->have_posts() ) {
$query->next_post();
$term_letter = strtoupper( substr( $query->post->post_title, 0, 1 ) );
if ( $glossary_letter != $term_letter ) {
$dl .= (count( $active_letters ) ? \'</dl>\' : \'\') . \'<li id="\' . $term_letter . \'"><span class="subheading">\' . $term_letter . \'</span><dl>\';
$glossary_letter = $term_letter;
$active_letters[] = $term_letter;
}
$dl .= \'<dt>\' . $query->post->post_title . \'</dt>\';
$dl .= \'<dd>\' . $query->post->post_content . \'</dd>\';
}
$dl .= \'</dl></li>\';
$ul = \'<ul class="letters">\';
foreach ( array( \'A\', \'B\', \'C\', \'D\', \'E\', \'F\', \'G\', \'H\', \'I\', \'J\', \'K\', \'L\', \'M\', \'N\', \'O\', \'P\', \'Q\', \'R\', \'S\', \'T\', \'U\', \'V\', \'W\', \'X\', \'Y\', \'Z\' ) as $letter ) {
$ul .= \'<li>\' . (in_array( $letter, $active_letters ) ? \'<a href="#\' . $letter . \'">\' . $letter . \'</a>\' : $letter) . \'</li>\';
}
$ul .= \'</ul>\';
echo \'<div id="glossary">\' . $ul . \'<ul class="definitions">\' . $dl . \'</ul></div>\';
?>
最合适的回答,由SO网友:s_ha_dum 整理而成
严格遵守您的代码,您希望。。。
$dl .= \'<dt><a href="\'.get_permalink($wp_query->post->ID).\'">\' . $wp_query->post->post_title . \'</a></dt>\';
。。。但您正在使用
WP_Query
以非常非典型的方式创建对象。您在某种程度上强行通过循环,而不是使用内置在核心中的方法和模板函数。例如:
while ( $query->have_posts() ) {
$query->the_post();
$term_letter = strtoupper( substr( get_the_title(), 0, 1 ) );
if ( $glossary_letter != $term_letter ) {
$dl .= (count( $active_letters ) ? \'</dl>\' : \'\') . \'<li id="\' . $term_letter . \'"><span class="subheading">\' . $term_letter . \'</span><dl>\';
$glossary_letter = $term_letter;
$active_letters[] = $term_letter;
}
$dl .= \'<dt><a href="\'.get_permalink().\'">\' . get_the_title() . \'</a>></dt>\';
$dl .= \'<dd>\' . get_the_content() . \'</dd>\';
}
$dl .= \'</dl></li>\';
或者至少你可以替换
$query->post->
具有
$post->
自
the_post()
方法重置
$post
每次运行循环中的下一个帖子时,它都是全局的。