1

ワードプレスの「ブログ」テンプレートを操作して、ページタイトルを参照としてカテゴリ固有の投稿を表示しました。そうすれば、「ニュース」ページを作成でき、ページにすべての「ニュース」カテゴリの投稿が自動的に入力されます。

このコードは、単一の単語のタイトルで記述されたとおりに完全に機能しますが、複数の単語のタイトルで投稿を呼び出すことはありません。すなわち:「ニュースで」。ページは壊れません。「申し訳ありませんが、条件に一致する投稿はありません」と表示されます。

何かご意見は?

<?php
/**
 * Template Name: Category Posts Ultimate Template
 *
 * This template is the default page template. It is used to display content when someone is viewing a
 * singular view of a page ('page' post_type) unless another page template overrules this one.
 * @link http://codex.wordpress.org/Pages
 *
 * @package WooFramework
 * @subpackage Template
 */
    get_header();
    global $woo_options;
    /**
     * The Variables
     *
     * Setup default variables, overriding them if the "Theme Options" have been saved.
     */

        $settings = array(
                        'thumb_w' => 505, 
                        'thumb_h' => 284, 
                        'thumb_align' => 'alignleft',
                        'post_content' => 'excerpt'
                        );

        $settings = woo_get_dynamic_values( $settings );
    ?>
    <!-- #content Starts -->
    <div id="content" class="page col-full">

        <?php woo_main_before(); ?>

        <section id="main" class="col-left"> 

            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <h1><?php the_title(); ?></h1>
                <?php the_content(); ?>
            <?php endwhile; else: endif; ?>

        <?php woo_loop_before(); ?> 

        <!-- Does the blog need pagination? -->
        <?php
            if ( get_query_var( 'paged') ) { $paged = get_query_var( 'paged' ); } elseif ( get_query_var( 'page') ) { $paged = get_query_var( 'page' ); } else { $paged = 1; }

            $query_args = array(
                                'post_type' => 'post', 
                                'paged' => $paged
                            );

            remove_filter( 'pre_get_posts', 'woo_exclude_categories_homepage' );

            // The Query

            query_posts('category_name='.get_the_title().'&post_status=publish,future&paged=' . get_query_var('paged'));

            if (have_posts()) { 
                $count = 0;
                while (have_posts()) { the_post(); $count++;
        ?>      

                    <!-- Post Starts -->
                    <article <?php post_class(); ?>>

                        <header>
                            <h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                        </header>

                        <?php woo_post_meta(); ?>

                        <section class="entry fix">
                            <?php if ( isset( $settings['post_content'] ) && $settings['post_content'] != 'content' ) { woo_image( 'width=' . $settings['thumb_w'] . '&height=' . $settings['thumb_h'] . '&class=thumbnail ' . $settings['thumb_align'] ); } ?>
                            <?php global $more; $more = 0; ?>                                           
                            <?php if ( isset( $settings['post_content'] ) && $settings['post_content'] == 'content' ) { the_content(__( 'Read More...', 'woothemes' ) ); } else { the_excerpt(); } ?>
                        </section>

                        <footer class="post-more">      
                            <?php if ( isset( $settings['post_content'] ) && $settings['post_content'] == 'excerpt' ) { ?>
                            <span class="read-more"><a href="<?php the_permalink(); ?>" title="<?php esc_attr_e( 'Continue Reading &rarr;', 'woothemes' ); ?>" class="button"><?php _e( 'Continue Reading', 'woothemes' ); ?></a></span>
                            <?php } ?>
                        </footer>   

                    </article><!-- /.post -->

                <?php
                        } // End WHILE Loop

                    } else {
                ?>
                    <article <?php post_class(); ?>>
                        <p><?php _e( 'Sorry, no posts matched your criteria.', 'woothemes' ); ?></p>
                    </article><!-- /.post -->
                <?php } // End IF Statement ?> 


        <?php woo_loop_after(); ?>
        <?php woo_pagenav(); ?>
        <?php wp_reset_postdata(); ?>      
        <?php wp_reset_query(); ?>            


        </section><!-- /#main -->

        <?php woo_main_after(); ?>

        <?php get_sidebar(); ?>

    </div><!-- /#content -->

<?php get_footer(); ?>
4

1 に答える 1

2

クエリにスペースを含めることができないため、機能していません。カテゴリ名で投稿を取得する代わりに、カテゴリスラッグのようなものを使用する方が良いでしょう。これにはスペースがなく、「InTheNews」のようなものの場合はおそらく「in-the-news」になります。

これは間違いなくあなたのカテゴリーのスラッグがタイトルと比較してどのように命名されているかに依存しますが、それは私が過去に成功して使用したものです。したがって、クエリを実行する前に、タイトルを取得して、カテゴリに一致する新しい「スラッグ」に変換する必要があります。

// Replace any spaces in the title with dashes
$title_slug = strtolower(str_replace( " ", "-" , the_title_attribute('echo=0') ) );
// Get the category object by using our new title slug
$id_obj = get_category_by_slug($title_slug);
// Get the category id so that we can use that in the query
$cat_id = $id_obj->term_id;

次にquery_posts、名前の代わりにカテゴリIDを使用するように変更します。

query_posts( 'cat='.$cat_id.'&post_status=publish,future&paged='.get_query_var('paged') );

繰り返しになりますが、これにはタイトル名やカテゴリスラッグを少し調整する必要がある場合とない場合がありますが、うまくいくはずです。

于 2012-09-07T04:01:21.180 に答える