自定义帖子类型不显示来自单个页面的内容-{自定义帖子类型}页面

时间:2012-08-22 作者:Ashy

我正在尝试创建一个带有分类法的自定义帖子类型,并在插件中为我的自定义搜索创建一个快捷码。当我激活插件时,我会在仪表板上看到我的CPT和分类法。但当我发布这篇文章时,我并没有看到以单个-{CPT}形式出现的数据。我创建的php页面。相反,它显示我的CPT中所有帖子的列表。相关文件如下。

指数php(我的插件页面)

function register_vfic_post_type() {        

    /* ----Begin Code to include Custom Post Types and its Taxonomies----- */

        register_post_type(\'vfic_publications\', array(  \'label\' => \'VFIC Publications\',\'description\' => \'\',\'public\' => true,\'show_ui\' => true,\'show_in_menu\' => true,\'capability_type\' => \'post\',\'hierarchical\' => false,\'rewrite\' => array(\'slug\' => \'\'),\'query_var\' => true,\'exclude_from_search\' => false,\'supports\' => array(\'title\',\'editor\',\'excerpt\',\'revisions\',\'author\',\'page-attributes\',),\'labels\' => array (
          \'name\' => \'VFIC Publications\',
          \'singular_name\' => \'\',
          \'menu_name\' => \'VFIC Publications\',
          \'add_new\' => \'Add VFIC Publications\',
          \'add_new_item\' => \'Add New VFIC Publications\',
          \'edit\' => \'Edit\',
          \'edit_item\' => \'Edit VFIC Publications\',
          \'new_item\' => \'New VFIC Publications\',
          \'view\' => \'View VFIC Publications\',
          \'view_item\' => \'View VFIC Publications\',
          \'search_items\' => \'Search VFIC Publications\',
          \'not_found\' => \'No VFIC Publications Found\',
          \'not_found_in_trash\' => \'No VFIC Publications Found in Trash\',
          \'parent\' => \'Parent VFIC Publications\',
        ),) );
        flush_rewrite_rules();
    }
    add_action( \'init\', \'register_vfic_post_type\' );

    register_taxonomy(\'Year\',array (
        0 => \'vfic_publications\',
        ),array( \'hierarchical\' => false, \'label\' => \'Year\',\'show_ui\' => true,\'query_var\' => true,\'rewrite\' => false,\'singular_label\' => \'Year\') );

    register_taxonomy(\'PubsAuthor\',array (
        0 => \'vfic_publications\',
        ),array( \'hierarchical\' => false, \'label\' => \'PubsAuthor\',\'show_ui\' => true,\'query_var\' => true,\'rewrite\' => false,\'singular_label\' => \'PubsAuthor\') ); 

    register_taxonomy(\'Journal\',array (
        0 => \'vfic_publications\',
        ),array( \'hierarchical\' => false, \'label\' => \'Journal\',\'show_ui\' => true,\'query_var\' => true,\'rewrite\' => false,\'singular_label\' => \'Journal\') );

    register_taxonomy(\'nts\',array (
        0 => \'vfic_publications\',
        ),array( \'hierarchical\' => false, \'label\' => \'Non-Technical Summary\',\'show_ui\' => true,\'query_var\' => true,\'rewrite\' => false,\'singular_label\' => \'nts\') );

    function vfic_ctg_rewrite_flush() {
        register_vfic_post_type();
        flush_rewrite_rules();
    }
register_activation_hook( __FILE__, \'vfic_ctg_rewrite_flush\' );

/* ----END Code to include Custom Post Types and its Taxonomies----- */

function vfic_pubs_forms_shortcode() { ?>
<div class="pub-search-form">

    <form role="search" method="get" id="searchform" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
        <p>Keyword(s): <input type="text" class="field" name="s" value="" id="s" class="s" />
        <input type="checkbox" name="abstract" value=""/>Just Search Abstract</p>

        <p>Author(s): &nbsp&nbsp <?php $myterms = get_terms(array(\'PubsAuthor\'), array(\'order\'=>\'ASC\',\'hide_empty\'=>true));
                $author = "pubsauthors";
                $emptyvalue = "";
                $text = "text";
                    $term_taxonomy=$term->PubsAuthor; 
                    $term_slug=$term->slug;
                    $term_name =$term->name;
                    $link = $term_slug;
                    $output .="<input type=\'".$text."\' name=\'".$link."\'/>";

                echo $output; ?>
                    <?php $myterms = get_terms(array(\'PubsAuthor\'), array(\'order\'=>\'ASC\',\'hide_empty\'=>true));
                $author = "pubsauthor";
                $emptyvalue = "";
                $output ="<select name=\'".$author."\'><option selected=\'".$selected."\' value=\'".$emptyvalue."\'>--Select Authors--</option>\'";

                foreach($myterms as $term){
                    $term_taxonomy=$term->PubsAuthor; 
                    $term_slug=$term->slug;
                    $term_name =$term->name;
                    $link = $term_slug;
                    $output .="<option name=\'".$link."\' value=\'".$link."\'>".$term_name."</option>";
                }
            $output .="</select>";
            echo $output;
                    ?></p>

       <p>Year:&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
        <?php $myterms = get_terms(array(\'Year\'), array(\'order\'=>\'ASC\',\'hide_empty\'=>true));
            $year = "pubsyear";
            $emptyvalue = "";
            $output ="<select name=\'".$year."\'><option selected=\'".$selected."\' value=\'".$emptyvalue."\'>--Select Year--</option>\'";

            foreach($myterms as $term){
                $term_taxonomy=$term->Year; 
                $term_slug=$term->slug;
                $term_name =$term->name;
                $link = $term_slug;
                $output .="<option name=\'".$link."\' value=\'".$link."\'>".$term_name."</option>";
            }
            $output .="</select>";
            echo $output;
        ?></p>
        <input type="hidden" name="post_type" value="vfic_publications" />
        <input type="submit" id="submit" value="Search" />
    </form>
</div>  
<?php } ?>
<?php add_shortcode(\'vfic_pubs_search\', \'vfic_pubs_forms_shortcode\');
add_filter( "template_redirect", "get_custom_post_type_template" ) ;?>
<?php function get_custom_post_type_template($template_redirect) {
         global $post;

         if ($post->post_type == \'vfic_publications\') {
              $template_redirect = get_template_part(\'loop\',\'vfic_publications\');
         }
         return $template_redirect;
    } ?>
循环-{自定义帖子类型}。php页面

get_header(); ?>
<?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();

foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$search = new WP_Query($search_query);
$author = $_GET[\'pubsauthor\'];
$abstract = $_GET[\'abstract\'];

$tax_query = array(\'relation\' => \'OR\');
if ($_GET[\'pubsauthor\'])
{
    $tax_query[] =  array(
            \'taxonomy\' => \'PubsAuthor\',
            \'field\' => \'slug\',
            \'terms\' => array($_GET[\'pubsauthor\'])
        );
}
if ($_GET[\'pubsyear\'])
{
    $tax_query[] =  array(
            \'taxonomy\' => \'Year\',
            \'field\' => \'slug\',
            \'terms\' => array($_GET[\'pubsyear\'])
        );
}

$query = new WP_Query(
array(
    //Retreive ALL publications posts
    \'post_type\' => \'vfic_publications\',
    \'posts_per_page\' => \'-1\',
    \'tax_query\' => $tax_query
)
);

?>
    <div id="wrap">
        <div id="content" role="main">
<?php if ( $query->have_posts() ) : ?>
    <p><h2>Your search returned<?php /* Search Count */ $allsearch = &new      WP_Query(array(
    //Retreive ALL publications posts
    \'post_type\' => \'vfic_publications\',
    \'posts_per_page\' => \'-1\',
    \'tax_query\' => $tax_query
    )); $key = wp_specialchars($s, 1); $count = $allsearch->post_count;   _e(\'\'); _e(\'<span class="search-terms">\'); echo $key; _e(\'</span>\'); _e(\' &mdash; \'); echo $count . \' \'; _e(\'result(s)\'); wp_reset_query(); ?></h2><br/>

<i>Please contact individual journals to obtain a full copy of the publication.</i><hr /><hr /></p>

<?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'agriflex\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2><br/><hr />

<?php endwhile; // End the loop. Whew. ?>

<?php elseif ( have_posts() ) : ?>
            <h1 class="page-title">....<?php printf( __( \'Search %s\', \'agriflex\' ), \'<span>\' . get_search_query() . \'</span>\' ); ?></h1>
            <?php
            /* Run the loop for the search to output the results.
             * If you want to overload this in a child theme then include a file
             * called loop-search.php and that will be used instead.
             */
             get_template_part( \'loop\', \'search\' );
            ?>

<?php else : ?>
            <div id="post-0" class="post no-results not-found">
                <h2 class="entry-title"><?php _e( \'Nothing Found\', \'agriflex\' ); ?></h2>
                <div class="entry-content">
                    <p><?php _e( \'Sorry, but nothing matched your search criteria. Please try again with some different keywords.\', \'agriflex\' ); ?></p>
                    <?php get_search_form(); ?>
                </div><!-- .entry-content -->
            </div><!-- #post-0 -->
<?php endif; ?>
        </div><!-- #content -->
    </div><!-- #wrap -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
非常感谢您的帮助。

3 个回复
SO网友:Chip Bennett

您需要重命名loop-{custom-post-type}.phpsingle-{custom-post-type}.php.

明确地说,基于上面的代码,实际的文件名应该是:

single-vfic_publications.php.

摆脱任何其他模板重定向或任何其他类似的,你正在使用的。根据Template Hierarchy, 如果您有一个名为vfic_publications, 和一个名为single-vfic_publications.php, WordPress将自动使用它来呈现该CPT的单个post视图。

另一个问题是,您似乎正在尝试使用single post view page 作为archive index page. 你不需要那样做。相反,创建一个archive-vfic_publications.php 文件,WordPress将自动使用它为您的CPT呈现存档索引页面视图。

您似乎还在模板文件中进行税务查询。您可能需要考虑使用taxonomy template files 为此目的。

SO网友:Tom J Nowell

您使用以下代码:

add_filter( "template_redirect", "get_custom_post_type_template" ) ;?>
<?php function get_custom_post_type_template($template_redirect) {
         global $post;

         if ($post->post_type == \'vfic_publications\') {
              $template_redirect = get_template_part(\'loop\',\'vfic_publications\');
         }
         return $template_redirect;
    } ?>
但不清楚你为什么要做这样的事。如果您试图加载loop-vfic_出版物。php get\\u template\\u部分将无法工作,除非该文件位于主题中。

如果是在主题中,那么这个函数就是浪费,你应该使用这个文件single-vfic_publications.php 而是使用get_template_part() 包含循环文件。

SO网友:Anh Tran

这个get_template_part 函数用于include 模板文件,不返回预期的文件路径。

我想更改您的线路:

$template_redirect = get_template_part(\'loop\',\'vfic_publications\');

$templates = array( \'loop-vfic_publications.php\', \'loop.php\' );
$template_redirect = locate_template( $templates, false, false );

结束

相关推荐

从Single.php重定向至404.php

我有一种情况,我想重定向用户从单一。php到我的404。php我该怎么做?我在单曲里就是这样做的。phpheader(\"HTTP/1.0 404 Not Found\"); header(\"Location: \".bloginfo(\'template_url\').\"/404.php\"); exit(); But when I check the http responses I get a 302 response instead of the 404Importan