douyi3833 2017-07-15 01:43
浏览 51
已采纳

在wordpress中向模板添加值

I have 22 genres in my movie page that I am designing in Wordpress and with some PHP. Basically rather than me typing out 22 different taxonomy pages e.g. taxonomy-genre-action.php to taxonomy-genre-western.php I am going to use a template genre-template. But what I don't understand is how do I get the value/destination of a hyperlink to change two values on the genre-template page e.g.

<?php

    $args = array(
        'post_type'         => 'movies',
        'orderby'           => 'name',
        'order'             => 'ASC',
        'posts_per_page'    => -1,
        'tax_query'         => array(
            array(
                'taxonomy'      => 'genre',
                'field'         => 'slug',
                'terms'         => 'action' // [CHANGE HERE]
            )
        )
    );

?>

<h2 style="color: white">action movies</h2> <!-- [CHANGE HERE] -->

Change the 'action' value to whatever hyperlink to the other 21 genres are.

<a href="/wordpress/genre/<?php echo $args[$termCount]; ?>"><?php echo $args[$termCount]; ?></a>

How would I add the $args[$termcount] value to the genre-template.php

  • 写回答

1条回答 默认 最新

  • doushenyi9104 2017-07-15 03:44
    关注

    So, you want to create a general archive page for your custom taxonomy (genre) terms, right?

    I'll suggest you read, understand and practice WordPress Template Hierarchy in depth.

    If WordPress can't find taxonomy-{taxonomy}-{term}.php then it'll fallback to taxonomy-{taxonomy}.php. So, in your case, you can just create one archive file named taxonomy-genre.php and you'll be good to go.

    Also, you don't even need to construct a query in the archive file. WordPress will know that it's an archive call and will prepare query for that term automatically.

    Now, to get the current term's data, use get_queried_object(); to your advantage. For example, to get the term name,

    get_queried_object()->name;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?