繁体   English   中英

WordPress:动态更改类别

[英]WordPress: Change category dynamically

我有以下代码:

<?php wp_dropdown_categories(); ?>

我从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;

第一个功能采用所有类别,第二个功能在选择时显示它们。 当我选择类别之一时,我将被重定向到所选类别的URL。

我的问题是,该循环不显示所选类别的帖子。 在寻找解决方案时,我遇到了这样的事情:

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

但是它仅适用于“ XYZ类别页面”之类的内容。 我的页面允许最终用户创建新类别,因此我需要更多动态内容。

也许是这样的吗?

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

然后在循环中使用它?

编辑:这是我在循环中使用的代码(category.php和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 }}  ?>
$categories = get_query_var('cat');
if (!empty($categories)) 
{
    $category       =  get_category($cat);
    $categoryName   =  $category->name;   
    $categorySlug   =  $category->slug;   
    $catId          =  $category->cat_ID;
}

$args = array(
                                    'posts_per_page'    => 12, 
                                    'post_type'         => 'post',    
                                    'order'             =>  'ASC',                                  
                                    'paged'             => $paged,        
                                            'tax_query' => array(
                                                array (
                                                    'taxonomy'  => 'category',
                                                    'field'     => 'slug',
                                                    'terms'     => $categorySlug
                                                )
                                ));

$query = NEW WP_Query($args);

您实际上不需要在category.php中定义自定义查询。 WordPress足够聪明,可以为您做到这一点。 只需更换

<?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();
            ?>

<?php

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM