簡體   English   中英

如何獲取自定義帖子類型的標題作為其相關帖子的鏈接?

[英]how get the title of a custom post type as link to its related post?

我想獲取自定義帖子類型的帖子標題並將其鏈接到其相關帖子。 所以我有以下代碼:



query_posts( 'post_type=custom_post_type&posts_per_page=1&order=DESC' );

while (have_posts()) : the_post();
       echo "' >"; the_title(); "";

endwhile;

帖子標題的輸出工作正常,但鏈接不正確。 我有語法錯誤嗎? 還是這種方法完全錯誤?

感謝幫助!

使用get_permalink()獲取當前$post的永久鏈接。

while (have_posts()) : the_post();
?>
  <a href="<?= get_permalink() ?>"><?= sanitize_title(get_the_title()) ?></a>
<?php     
endwhile;

如果您使用的是非常過時的 PHP 版本,請將<?=替換為<?php echo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM