如何在Wordpress中的自定义帖子下显示自定义存档页面?

时间:2013-03-05 02:25:14

标签: php wordpress gallery custom-post-type portfolio

我正在使用我的Wordpress投资组合网站,并为我的投资组合部分创建了一个自定义帖子类型。当用户点击我的投资组合链接时,它会将他们带到archive-portfolio.php,它会显示我所有项目的图库,当他们点击项目时,它会将他们带到显示相关项目的single-portfolio.php。

一切都在运作它应该如何以及正确的样式,但现在我想在archive-portfolio.php上的画廊也显示在每个项目下,这是在单一portfolio.php上,制作导航项目之间很容易。如果在显示图库时,用户所在项目的链接被排除在图库之外也会很棒。

我试过插入

<?php wp_get_archives(); ?>

进入我的single-portfolio.php模板,但我似乎无法按照我想要的方式工作。我无法抓住我的自定义帖子,它只显示链接到我的常规帖子的月份链接。我需要它来显示我的自定义帖子和archive-portfolio.php上的库。 (基本上我只是想将archive-portfolio.php插入到single-portfolio.php的内容下的single-portfolio.php中)

这是我的第一个Wordpress网站,我自己一边学习这一切。我已经尝试过自己寻找解决方案,但我发现的一切似乎只是关于如何创建自定义帖子类型,我已经想到了。我仍然在本地开发我的网站,这就是为什么我现在无法链接到它,但为了让你知道我的目的,你可以查看这个用Cargo制作的网站:

http://www.timboelaars.nl/help-ink

基本上,我想要与示例站点相同的功能;我只想让画廊在项目下展示。

像往常一样,任何帮助都会非常感激!我一直在努力解决这个问题几天,一直在避免它,但网站已经完成,它已经无法避免了。我真的可以在这方面使用一些帮助。提前致谢!



(编辑:根据以下评论的要求,这是我的自定义php文件的样子)

归档portfolio.php:

<!-- get_header -->
<?php get_header(); ?>
<!-- END get_header -->         

            <!-- #content -->
            <div id="content">

                <!-- #inner-content -->
                <div id="inner-content" class="wrap clearfix">

                    <!-- #main -->
                    <div id="main" class="twelvecol first clearfix" role="main">

                        <!-- have_posts -->
                        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                        <!-- article -->
                        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> role="article">

                            <!-- .entry-content -->
                            <section class="entry-content clearfix portfolio-gallery">

                                <!-- PORTFOLIO PROJECT LINK -->
                                <a href="<?php the_permalink() ?>">

                                    <!-- .portfolio-project -->
                                    <figure class="portfolio-project">

                                        <!-- custom header -->
                                        <header class="portfolio-project-title">
                                            <h1 class="bigtext">
                                                <div>
                                                    <?php
                                                        global $post;
                                                            $text = get_post_meta( $post->ID, 'juroto_custom_header', true );
                                                        echo $text;
                                                    ?>
                                                </div>
                                            </h1>                                   
                                        </header> <!-- END custom header -->                                    

                                        <!-- the_excerpt -->
                                        <figcaption class="portfolio-project-excerpt">
                                            <?php the_excerpt(); ?>
                                        </figcaption> <!-- END the_excerpt -->

                                        <!-- .black-filter -->
                                        <div class="black-filter"></div>
                                        <!-- END .black-filter -->

                                        <!-- featured image -->
                                        <?php
                                            if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
                                                the_post_thumbnail('thumbnail-bw', array('class'=>'bw-image-fade'));
                                                the_post_thumbnail('thumbnail');
                                            } 
                                        ?> <!-- END featured image -->

                                    </figure> <!-- END .portfolio-project -->

                                </a> <!-- END PORTFOLIO PROJECT LINK -->

                            </section> <!-- END .entry-content -->

                        </article> <!-- END article -->

                        <?php endwhile; ?> <!-- END have_posts -->

                        <?php else : ?>

                            <!-- #post-not-found -->
                            <article id="post-not-found" class="hentry clearfix">
                                <header class="article-header">
                                    <h1><?php _e("Oops, Post Not Found!", "bonestheme"); ?></h1>
                                </header>
                                <section class="entry-content">
                                    <p><?php _e("Uh Oh. Something is missing. Try double checking things.", "bonestheme"); ?></p>
                                </section>
                                <footer class="article-footer">
                                    <p><?php _e("This is the error message in the custom posty type archive template.", "bonestheme"); ?></p>
                                </footer>
                            </article>

                        <?php endif; ?> <!-- END #post-not-found -->

                    </div> <!-- END #main -->

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

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

<!-- get_footer -->
<?php get_footer(); ?>
<!-- END get_footer -->

基本上我想在所显示的项目下插入调用每篇文章(或者我称之为投资组合项目)的循环到single-portfolio.php。这就是single-portfolio.php的样子。

单portfolio.php:

<!-- get_header -->
<?php get_header(); ?>
<!-- END get_header -->

            <!-- #content -->
            <div id="content">

                <!-- #inner-content -->
                <div id="inner-content" class="wrap clearfix">

                    <!-- #main -->
                    <div id="main" class="twelvecol first clearfix" role="main">

                        <!-- have_posts -->
                        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                        <!-- article -->
                        <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">

                            <!-- .article-header -->
                            <header class="article-header">

                                <!-- .bigtext -->
                                <h1 class="bigtext">
                                    <div>
                                        <!-- the_title -->
                                        <?php the_title(); ?>
                                        <!-- END the_title -->
                                    </div>
                                </h1> <!-- END .bigtext -->

                            </header> <!-- END article header -->

                            <!-- .entry-content -->
                            <section class="entry-content clearfix">

                                <!-- GALLERY -->                                
                                <?php
                                    $jurotos_gallery = array( 
                                        // OPTIONS FOR THE GALLERY 
                                        'numberposts'       => -1, // Using -1 loads all posts or in this case images  
                                        'orderby'           => 'menu_order', // This ensures images are in the order set in the page media manager. To change the order of the images go to the post, click add media, click the drop down menu, select uploaded to this post, and just drag and drop the images in the order you want them to appear. When you're done just close the media manager, you don't need to update the post or insert image or anything.  
                                        'order'             => 'ASC', // ASC or DESC. ASC shows order as is in the media manager and DESC shows the reverse order (so the last image is first and so on)
                                        'post_mime_type'    => 'image', // Make sure it doesn't pull other resources, like videos  
                                        'post_parent'       => $post->ID, // *Important part - ensures the associated images are loaded 
                                        'post_status'       => null, 
                                        'post_type'         => 'attachment',
                                        'size'              => 'full',  // thumbnail, medium, large or full
                                        'include'           => '', // could choose to include something else
                                        'exclude'           => get_post_thumbnail_id( $post->ID ), // excludes thumbnail/featured image from being part of the gallery
                                    );  

                                    $images = get_children( $jurotos_gallery );
                                ?> 

                                <?php if($images){ ?>  
                                    <div class="flexslider">
                                        <ul class="slides">
                                            <?php foreach($images as $image){ ?>  
                                                <li>
                                                    <img src="<?php echo $image->guid; ?>" alt="<?php echo $image->post_title; ?>" title="<?php echo $image->post_title; ?>" />
                                                </li> 
                                            <?php } ?>
                                        </ul>  
                                    </div>  
                                <?php } ?>                              
                                <!-- END GALLERY -->  

                                <!-- the_content -->
                                <div id="accordion">
                                    <h3>Brief</h3>
                                        <div class="juroto-text-columns">
                                            <?php the_content(); ?>
                                        </div>
                                </div>
                                <!-- END the_content -->

                            </section> <!-- END .entry-content -->

                        </article> <!-- END article -->

                        <?php endwhile; ?> <!-- END have_posts -->          

                        <?php else : ?>

                            <!-- #post-not-found -->
                            <article id="post-not-found" class="hentry clearfix">
                                <header class="article-header">
                                    <h1><?php _e("Oops, Post Not Found!", "bonestheme"); ?></h1>
                                </header>
                                <section class="entry-content">
                                    <p><?php _e("Uh Oh. Something is missing. Try double checking things.", "bonestheme"); ?></p>
                                </section>
                                <footer class="article-footer">
                                    <p><?php _e("This is the error message in the single-portfolio.php template.", "bonestheme"); ?></p>
                                </footer>
                            </article>

                        <?php endif; ?> <!-- END #post-not-found -->

                    </div> <!-- END #main -->

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

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

<!-- get_footer -->
<?php get_footer(); ?>
<!-- END get_footer -->





(编辑:下面是archive-portfolio.php循环的摘录,显示了我所有的自定义组合帖子。我将这些帖子排列和样式看起来像一个帖子/项目的图库。这就是我想要的当您在单个portfolio.php显示的帖子/项目页面上时,在帖子/项目下显示。基本上我需要这个循环收集所有自定义帖子并将其排列成一个库中的单个-portfolio.php在single-portfolio.php的循环下收集特定帖子/项目的信息,但我不知道该怎么做。是否有一些代码如下:

<?php get_archive-portfolio(); ?> 

我可以在single-portfolio.php中使用它来显示自定义帖子的图库吗?)

来自archive-portfolio.php的

循环显示了投资组合库:

<!-- have_posts -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> role="article">

    <!-- .entry-content -->
    <section class="entry-content clearfix portfolio-gallery">

        <!-- PORTFOLIO PROJECT LINK -->
        <a href="<?php the_permalink() ?>">

            <!-- .portfolio-project -->
            <figure class="portfolio-project">

                <!-- custom header -->
                <header class="portfolio-project-title">
                    <h1 class="bigtext">
                        <div>
                            <?php
                                global $post;
                                    $text = get_post_meta( $post->ID, 'juroto_custom_header', true );
                                echo $text;
                            ?>
                        </div>
                    </h1>                                   
                </header> <!-- END custom header -->                                    

                <!-- the_excerpt -->
                <figcaption class="portfolio-project-excerpt">
                    <?php the_excerpt(); ?>
                </figcaption> <!-- END the_excerpt -->

                <!-- .black-filter -->
                <div class="black-filter"></div>
                <!-- END .black-filter -->

                <!-- featured image -->
                <?php
                    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
                        the_post_thumbnail('thumbnail-bw', array('class'=>'bw-image-fade'));
                        the_post_thumbnail('thumbnail');
                    } 
                ?> <!-- END featured image -->

            </figure> <!-- END .portfolio-project -->

        </a> <!-- END PORTFOLIO PROJECT LINK -->

    </section> <!-- END .entry-content -->

</article> <!-- END article -->

<?php endwhile; ?> <!-- END have_posts -->

<?php else : ?>

    <!-- #post-not-found -->
    <article id="post-not-found" class="hentry clearfix">
        <header class="article-header">
            <h1><?php _e("Oops, Post Not Found!", "bonestheme"); ?></h1>
        </header>
        <section class="entry-content">
            <p><?php _e("Uh Oh. Something is missing. Try double checking things.", "bonestheme"); ?></p>
        </section>
        <footer class="article-footer">
            <p><?php _e("This is the error message in the custom posty type archive template.", "bonestheme"); ?></p>
        </footer>
    </article>

<?php endif; ?> <!-- END #post-not-found -->





(编辑:下面是portfolio.php,它是我的Wordpress主题中的一个函数文件,用于创建我的自定义后期类型:'投资组合',为其存储数据,并在我的管理区域中创建投资组合部分。此文件还为自定义帖子类型创建自定义分类,并定义在其存档页面上每页显示多少个自定义帖子类型,我将其设置为所有自定义帖子。我将此文件与我的主题功能文件分开以保持它更有条理。)

portfolio.php功能:

<?php
/****************************************
Creates Portfolio section in the admin 
area and custom posts called Projects
*****************************************/

function create_portfolio_section() { 
    // creating (registering) the custom type 
    register_post_type( 'portfolio', /* (http://codex.wordpress.org/Function_Reference/register_post_type) *IMPORTANT: If you change this, remember to resave/update permalinks from Wordpress admin menu under Setting/Permalinks for the page to display properly. Forgetting to do this will most likely lead you to the 404 error page */
        // All the options for this post type
        array('labels' => array(
            'name' => __('Portfolio', 'bonestheme'), /* This is the Title of the Group */
            'singular_name' => __('Project', 'bonestheme'), /* This is the individual type */
            'all_items' => __('All Projects', 'bonestheme'), /* the all items menu item */
            'add_new' => __('Add New Project', 'bonestheme'), /* The add new menu item */
            'add_new_item' => __('Add New Project', 'bonestheme'), /* Add New Display Title */
            'edit' => __( 'Edit Project', 'bonestheme' ), /* Edit Dialog */
            'edit_item' => __('Edit Project', 'bonestheme'), /* Edit Display Title */
            'new_item' => __('New Project', 'bonestheme'), /* New Display Title */
            'view_item' => __('View Project', 'bonestheme'), /* View Display Title */
            'search_items' => __('Search Project', 'bonestheme'), /* Search Project Title */ 
            'not_found' =>  __('No Projects found', 'bonestheme'), /* This displays if there are no entries yet */ 
            'not_found_in_trash' => __('No Projects found in the Trash', 'bonestheme'), /* This displays if there is nothing in the trash */
            'parent_item_colon' => ''
            ), /* end of arrays */
            'description' => __( 'This is a Portfolio Project', 'bonestheme' ), /* Project Description */
            'public' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'show_ui' => true,
            'query_var' => true,
            'menu_position' => 5, /* this is what order you want it to appear in on the left hand side menu */ 
            'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png', /* the icon for the custom post type menu */
            'rewrite'   => array( 'slug' => 'portfolio', 'with_front' => false ), /* you can specify its url slug *IMPORTANT: If you change this, remember to resave/update permalinks from Wordpress admin menu under Setting/Permalinks for the page to display properly. Forgetting to do this will most likely lead you to the 404 error page */
            'has_archive' => 'portfolio', /* you can rename the slug here *IMPORTANT: If you change this, remember to resave/update permalinks from Wordpress admin menu under Setting/Permalinks for the page to display properly. Forgetting to do this will most likely lead you to the 404 error page */
            'capability_type' => 'post',
            'hierarchical' => false,
            /* the next one is important, it tells what's enabled in the post editor */
            'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky')
        ) /* end of options */
    ); /* end of register post type */

    /* this adds your post categories to the Portfolio section */
    register_taxonomy_for_object_type('category', 'portfolio');
    /* this adds your post tags to the Portfolio section */
    register_taxonomy_for_object_type('post_tag', 'portfolio');

} 

    // adding the function to the Wordpress init
    add_action( 'init', 'create_portfolio_section');

    /*
    for more information on taxonomies, go here:
    http://codex.wordpress.org/Function_Reference/register_taxonomy
    */

    // this adds Portfolio Categories to the Portfolio section
    register_taxonomy( 'portfolio_cat', 
        array('portfolio'), /* if you change the name of register_post_type( 'portfolio', then you have to change this */
        array('hierarchical' => true,     /* if this is true, it acts like categories */             
            'labels' => array(
                'name' => __( 'Portfolio Categories', 'bonestheme' ), /* name of the custom taxonomy */
                'singular_name' => __( 'Portfolio Category', 'bonestheme' ), /* single taxonomy name */
                'search_items' =>  __( 'Search Portfolio Categories', 'bonestheme' ), /* search title for taxomony */
                'all_items' => __( 'All Portfolio Categories', 'bonestheme' ), /* all title for taxonomies */
                'parent_item' => __( 'Parent Portfolio Category', 'bonestheme' ), /* parent title for taxonomy */
                'parent_item_colon' => __( 'Parent Portfolio Category:', 'bonestheme' ), /* parent taxonomy title */
                'edit_item' => __( 'Edit Portfolio Category', 'bonestheme' ), /* edit custom taxonomy title */
                'update_item' => __( 'Update Portfolio Category', 'bonestheme' ), /* update title for taxonomy */
                'add_new_item' => __( 'Add New Portfolio Category', 'bonestheme' ), /* add new title for taxonomy */
                'new_item_name' => __( 'New Portfolio Category Name', 'bonestheme' ) /* name title for taxonomy */
            ),
            'show_admin_column' => true, 
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'custom-slug' ),
        )
    );   

    // this adds Portfolio Tags to the Portfolio section
    register_taxonomy( 'portfolio_tag', 
        array('portfolio'), /* if you change the name of register_post_type( 'portfolio', then you have to change this */
        array('hierarchical' => false,    /* if this is false, it acts like tags */                
            'labels' => array(
                'name' => __( 'Portfolio Tags', 'bonestheme' ), /* name of the custom taxonomy */
                'singular_name' => __( 'Portfolio Tag', 'bonestheme' ), /* single taxonomy name */
                'search_items' =>  __( 'Search Portfolio Tags', 'bonestheme' ), /* search title for taxomony */
                'all_items' => __( 'All Portfolio Tags', 'bonestheme' ), /* all title for taxonomies */
                'parent_item' => __( 'Parent Portfolio Tag', 'bonestheme' ), /* parent title for taxonomy */
                'parent_item_colon' => __( 'Parent Portfolio Tag:', 'bonestheme' ), /* parent taxonomy title */
                'edit_item' => __( 'Edit Portfolio Tag', 'bonestheme' ), /* edit custom taxonomy title */
                'update_item' => __( 'Update Portfolio Tag', 'bonestheme' ), /* update title for taxonomy */
                'add_new_item' => __( 'Add New Portfolio Tag', 'bonestheme' ), /* add new title for taxonomy */
                'new_item_name' => __( 'New Portfolio Tag Name', 'bonestheme' ) /* name title for taxonomy */
            ),
            'show_admin_column' => true,
            'show_ui' => true,
            'query_var' => true,
        )
    ); 

    /*
        looking for custom meta boxes?
        check out this fantastic tool:
        https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
    */



// Custom Posts Per Page
function portfolio_posts_per_page($query)
{
    switch ( $query->query_vars['post_type'] )
    {
        case 'portfolio':  // Post Type named 'portfolio'
            $query->query_vars['posts_per_page'] = -1; // -1 makes all posts shown on one page, positive numbers show that number of posts per page.
            break;

        // If you have more custom post types and you want to have the posts per page different than the journal/blog settings copy and past the code above and assign proper post type name and desired posts per page
        // EXAMPLE
        /*
        case '<insert post type name here>':  // Post Type named 'portfolio'
            $query->query_vars['posts_per_page'] = <insert desiered posts per page number here>; // -1 makes all posts shown on one page, positive numbers show that number of posts per page.
            break;
        */

        default:
            break;
    }
    return $query;
}

if( !is_admin() )
{
    add_filter( 'pre_get_posts', 'portfolio_posts_per_page' );
}

?>

1 个答案:

答案 0 :(得分:1)

您正在尝试在单个帖子下方添加一个存档模板,列出多个帖子。为此,您需要运行多个循环 - 一个用于主要帖子,一个用于下面的帖子列表。该手抄本有multiple loops的信息。

编辑:不要使用 query_posts ,如下所述,因为它会改变可能导致问题的主要帖子循环,正如Brad Dalton在评论中所指出的那样。请改用新的 WP_Query对象。代码与我在下面描述的代码非常相似。Read more。)

首先,在主帖子下方,通过创建新的 WP_Query对象来查询其他帖子的列表,使用query_posts() 。您可以找到有关如何查询帖子in the codex的信息。 (您可能只需要后期类型投资组合,仅发布已发布的帖子等)然后,使用get_template_part()包含您的存档模板。最后,不要忘记致电wp_reset_query()

query_posts('post_type=portfolio'); # Use WP_Query arguments accordingly
get_template_part('archive-portfolio');
wp_reset_query();