dongxiansi0158 2019-01-28 23:21
浏览 89
已采纳

WordPress:动态更改类别

I've got this code:

<?php wp_dropdown_categories(); ?>

And I've got this code from Codex:

var dropdown = document.getElementById("cat");
        function onCatChange() {
            if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
                location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
            }
        }
        dropdown.onchange = onCatChange;

First function takes all categories, and second function displays them on select. When I select one of the categories, then I'm redirected to the URL of the chosen category.

My problem is, that loop doesn't show posts from chosen category. Searching for a solution, I came across something like this:

$query = new WP_Query( array( 'category_name' => 'staff' ) );

But it works only for things like "XYZ Category page". My page allows creating new categories by end user, so I need something more dynamically.

Maybe something like this?

$cat = get_the_category();
$query = new WP_Query( array( 'category_name' => $cat ) );

and then use it in loop?

EDIT: This is my code that I used in loop (both category.php and archive.php

            <?php
            query_posts(array('posts_per_page' => 2, 'paged' => $paged));

            $queryObject = new  Wp_Query( array(
                'posts_per_page' => 2,
                'post_type' => array('post'),
                'paged' => $paged,
                'orderby' => 1,
                ));

            if ( $queryObject->have_posts() ) {

                while ( $queryObject->have_posts() ) {
                    $queryObject->the_post();
            ?>

                <div class="shorts">

                    <div class="shorts1">

                        <a class="Text3" href="<?php the_permalink(); ?>"><strong><?php the_title(); ?></strong></a>

                        <br><br>

                        <a class="Text2"><?php the_excerpt() ?></a>

                        <br><br>

                        <div class="more-wrapper"> 
                            <div class="more">
                                <a href="<?php the_permalink(); ?>">Dowiedź się więcej</a>
                            </div>
                        </div>

                    </div>
                </div>

            </article>

        <?php }}  ?>
  • 写回答

2条回答 默认 最新

  • dsaxw4201 2019-01-29 18:20
    关注

    You don't really need to define a custom query in category.php . WordPress is clever enough to do that for you. Just replace

    <?php
                query_posts(array('posts_per_page' => 2, 'paged' => $paged));
    
                $queryObject = new  Wp_Query( array(
                    'posts_per_page' => 2,
                    'post_type' => array('post'),
                    'paged' => $paged,
                    'orderby' => 1,
                    ));
    
                if ( $queryObject->have_posts() ) {
    
                    while ( $queryObject->have_posts() ) {
                        $queryObject->the_post();
                ?>
    

    With

    <?php
    
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?