以简码显示漂亮的链接

问题描述 投票:0回答:1

我正在制作一个简码以在首页上显示所有漂亮的链接。到目前为止,我已经成功显示了所有缺失链接以外的内容

// Display Pretty-Links
function custom_budurl() {

    $links = new WP_Query(array(
        'post_type' => 'pretty-link',
        'orderby' => 'meta_value',
        'order' => 'ASC',
    ));

    if ($links -> have_posts()) :
        // Start The Loop
        while ($links -> have_posts()) : $links -> the_post();
            echo '<li><a href="' . . '">' . get_the_title() . '</a></li>';
        endwhile;
    endif;

}
add_shortcode( 'budurl', 'custom_budurl' );

但是我无法弄清楚在href =“”选项中写什么来打印如图所示创建的链接。enter image description here

如果有人可以帮助,那就太好了。谢谢

wordpress hyperlink shortcode posts
1个回答
0
投票

漂亮链接使用自定义表格。在下面,我使用一个自定义选择来获取漂亮链接的标签,然后使用home_url()构建网址。在运行PHP 7.4的Pretty Links 3.1.0的WP 5.3.2上进行了测试。

// Display Pretty-Links
function custom_budurl()
{

  $links = new WP_Query(array(
    'post_type' => 'pretty-link',
    'orderby' => 'meta_value',
    'order' => 'ASC',
  ));

  if ($links->have_posts()) :
    // Start The Loop
    while ($links->have_posts()) : $links->the_post();
      global $wpdb;

      // get current post id
      $pid = get_the_ID();

      // custom select to get pretty link slug from custom table
      $sql = $wpdb->prepare("SELECT slug from {$wpdb->prefix}prli_links where link_cpt_id = %d", $pid);

      // run the query
      $results = $wpdb->get_row($sql);

      // build url
      $url = home_url($results->slug);

      // print html to browser
      echo '<li><a href="' . $url . '">' . get_the_title() . '</a></li>';

    endwhile;
  endif;
}
add_shortcode('budurl', 'custom_budurl');
© www.soinside.com 2019 - 2024. All rights reserved.