【问题标题】:Printing links individually in Worppress在 Wordpress 中单独打印链接
【发布时间】:2017-04-12 23:07:59
【问题描述】:

我基本上想在 wordpress 的“链接”部分创建一个类别,并为该类别添加一些链接和标题。简单的东西。

然后,我希望能够在我的模板文件中,根据我的喜好单独回显链接和标题或有关链接的任何内容。最好在一个循环中,因为我在链接之前和之后要进行一些页面构建。

我知道 'category_before' 和 'category_after' 存在,但它们不会满足我的需要。

所以我尝试了,

 <?php $args = array(
'orderby'          => 'name',
'order'            => 'ASC',
'limit'            => -1,
'category'         => '3',
'hide_invisible'   => 1,
'show_updated'     => 0,
'echo'             => 1,
'categorize'       => 0,
'category_orderby' => 'name',
'category_order'   => 'ASC',
'class'            => 'linkcat',
'category_before'  => '<tr><td>',
'category_after'   => '</td></tr>' ); 
wp_list_bookmarks( $args );
?>

但这会做一些错误的事情。除了链接文本和目的地之外,我不需要类别标题或其他任何内容。

我希望有一个“for”循环来循环所有链接,我可以在其中构建我的代码部分和链接,但如果有更好的方法,请告诉我。

谢谢

编辑:更多信息

所以我尝试了:

<?php
$taxonomy = 'link_category'; // Taken from the DB table
$tax_terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
    echo $tax_term->name;
}
?></ul>

这是我得到的最接近的。这只会返回有关类别的信息,而不是类别中的信息。

数据库中的“wp_term_taxonomy”表中没有关于我创建的实际类别的任何内容。

再次感谢

编辑: 这是我指的区域:

I want to show these 2 links

【问题讨论】:

    标签: php html wordpress hyperlink


    【解决方案1】:

    如需更多控制,您可以使用函数get_terms

    <?php
    //list terms in a given taxonomy
    $taxonomy = 'category'; // Pass default category or any custom taxonomy name
    $tax_terms = get_terms($taxonomy);
    ?>
    <ul>
    <?php
    foreach ($tax_terms as $tax_term) {
    echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
    }
    ?>
    </ul>
    

    对于信息函数及其参数: https://developer.wordpress.org/reference/functions/get_terms/

    【讨论】:

    • 这似乎不起作用。您已使用“get_terms”但链接到“get_term”。既不返回任何东西。谢谢
    • 刚刚又解决了这个问题。你能提供信息需要什么参考吗?你期望的输出是什么?
    【解决方案2】:

    您可能想尝试get_bookmarks 函数。

    $bookmarks = get_bookmarks( array(
        'orderby'        => 'name',
        'order'          => 'ASC',
        'category_name'  => 'category-name'
    ));
    
    // Loop through each bookmark and print formatted output
    foreach ( $bookmarks as $bookmark ) { 
        printf( '<a class="relatedlink" href="%s">%s</a><br />', $bookmark->link_url, $bookmark->link_name );
    }
    

    https://codex.wordpress.org/Function_Reference/get_bookmarks

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 2013-10-07
      相关资源
      最近更新 更多