【问题标题】:Wordpress Page of PostsWordPress 帖子页面
【发布时间】:2013-10-03 17:33:08
【问题描述】:

我正在建立一个个人网站来托管我的大学工作、个人项目和照片等。

菜单是由pageslinks 组成的层次结构。以我的大学网页为例。我想要实现的是显示与模块代码相关的帖子,即page'sslug

我使用了以下链接 http://codex.wordpress.org/Page_Templates#A_Page_of_Posts 并设法让它工作,但我已将模块代码硬编码到模板中,这意味着对于每个模块,我必须有一个单独的模板,唯一的东西就是从一个文件到下一个文件的不同之处在于 5 个字符,这不利于代码重用。

我要问的是,有没有办法从我正在查看的页面中获取slug 并将其用于WP_Query 参数。

如果您转到http://michaelnorris.co.uk/ 并查看菜单结构。导航到大学 -> 三年级 -> 个人项目,您会注意到 URL 是 http://michaelnorris.co.uk/uni/three/ci301,其中 ci301Individual Project 的模块代码。我想在每个模块页面上都有这个系统,这样我就可以标记posts,它们会显示在相关模块中。

【问题讨论】:

    标签: php wordpress templates custom-pages


    【解决方案1】:

    好的,实际上我自己找到了答案,但其他人也想这样做。下面是一个解决方案。

    解决方案在 Wordpress.org Codex http://codex.wordpress.org/Page_Templates#A_Page_of_Posts 上找到

    将文件命名为 pageofposts.php 并在 Wordpress 仪表板中编辑页面并将模板(在下拉菜单中)设置为“帖子页面”。宾果游戏!

    <?php
    /*
    Template Name: Page Of Posts
    */
    
    /* This example is for a child theme of Twenty Thirteen: 
    *  You'll need to adapt it the HTML structure of your own theme.
    */
    
    get_header(); ?>
    
        <div id="primary" class="content-area">
            <div id="content" class="site-content" role="main">
            <?php 
            /* The loop: the_post retrieves the content
             * of the new Page you created to list the posts,
             * e.g., an intro describing the posts shown listed on this Page..
             */
            global $post;
            $slug = get_post( $post )->post_name;
    
            if ( have_posts() ) :
                while ( have_posts() ) : the_post();
    
                  // Display content of page
                  get_template_part( 'content', get_post_format() ); 
                  wp_reset_postdata();
    
                endwhile;
            endif;
    
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
            $args = array(
                // Change these category SLUGS to suit your use. category_name is comma separated.
                'tag' => $slug, 
                'paged' => $paged
            );
    
            $list_of_posts = new WP_Query( $args );
            ?>
            <?php if ( $list_of_posts->have_posts() ) : ?>
                <?php /* The loop */ ?>
                <?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
                    <?php // Display content of posts ?>
                    <?php get_template_part( 'content', get_post_format() ); ?>
                <?php endwhile; ?>
    
                <?php twentythirteen_paging_nav(); ?>
    
            <?php else : ?>
                <?php get_template_part( 'content', 'none' ); ?>
            <?php endif; ?>
    
            </div><!-- #content -->
        </div><!-- #primary -->
    
    <?php get_footer(); ?>
    

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多