Wordpress如何使用<a href=""> in wp_query?

时间:2016-03-22 16:52:57

标签: php html wordpress

I have a wp_query to list the posts title and excerpt for a subcategory. I want the title to link to its respective post, but unable to figure out how to get the a href working properly in the following code. The link that is being generated from the below code is, /category/sub-category/the_permalink(). Any suggestions please?

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title', 'category_name' => 'diet-tips', 'paged' => $paged,'post_type' => 'post' );
$postslist = new WP_Query( $args );

if ( $postslist->have_posts() ) :
    while ( $postslist->have_posts() ) : 
        $postslist->the_post(); 

        echo "<div style='border:1px groove gray; margin-bottom:5px;'><h3 class='btposth'><a href='the_permalink()'>";
        the_title();
        echo "</a></h3><div class='btpostdiv'>";
        the_excerpt();
        echo "</div></div>";

        "<br />";
    endwhile;  

    next_posts_link( 'Older Entries', $postslist->max_num_pages );
    previous_posts_link( 'Next Entries »' );
    wp_reset_postdata();
endif;

Thanks for your help everyone, the code that is producing the results I was seeking is this:

echo "<div style='border:1px groove gray; margin-bottom:5px;'><h3 class='btposth'><a href='" . get_the_permalink() . "'>";
             the_title(); 
echo "</a>

3 个答案:

答案 0 :(得分:1)

您正在尝试将单词the_permalink()打印为文本而不是函数。试试这个:

 echo "<div style='border:1px groove gray; margin-bottom:5px;'><h3 class='btposth'><a href='".the_permalink()."'>";
    the_title();
    echo "</a></h3><div class='btpostdiv'>";

更短的版本:

echo "<div style='border:1px groove gray; margin-bottom:5px;'><h3 class='btposth'><a href='".the_permalink()."'>".the_title()."</a></h3><div class='btpostdiv'>";

编辑:没有回显功能:

 <div style="border:1px groove gray; margin-bottom:5px;">
     <h3 class="btposth"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
 <div class='btpostdiv'>

答案 1 :(得分:0)

echo '<a href='".the_permalink()."'>';
Somelinks
echo '</a>';

答案 2 :(得分:0)

试试这个

echo "<div style='border:1px groove gray; margin-bottom:5px;'><h3 class='btposth'><a href=".the_permalink().">".the_title()."</a></h3><div class='btpostdiv'>".the_excerpt()."</div></div>";