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 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?