如何在公文包页面上仅显示1个类别中的公文包项目?

时间:2014-07-23 作者:Jamie Ludlow

我有一个网站,我正在为我的一个客户,其中有一个投资组合页面,你可以在这里看到。。。

http://lenhamsportscars.com/cars-for-sale

您会注意到,当时有一个按类别(待售汽车或已售出汽车)过滤的选项。我需要修改代码,使其自动只显示我选择的类别中的投资组合项目,而不是在同一页上同时显示这两个项目。

这是当前需要修改的代码。。。

    <?php
/*
Template Name: Portfolio Template

*/
?>
<?php get_header(); ?>
<div class="wrapper">
    <div class="portfolio-content"> 
    <h1 class="post-title-page"><?php the_title(); ?></h1>

    <?php 
        $categories=get_categories(\'taxonomy=section\');
       if ( !empty( $categories ) ){
          ?>

      <ul class="portfolio-categ filter">
      <li>view by:</li>
      <li class="all"><a href="#">All</a></li>
         <?php  wp_list_categories(\'taxonomy=section&title_li=\');    ?>
        </ul>

   <?php  } else {    $categories = array(); } ?>

            <?php 
                $count = 0;
            //   custom_query_posts(array(\'post_type\' => \'portfolioitems\',\'posts_per_page\'  => 50))
    global $wp_query;
    wp_reset_query();
    $paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1;
    $wp_query = new WP_Query(array(\'post_type\' => \'portfolioitems\',\'posts_per_page\' => 50,\'paged\'=> $paged));

            ?>
            <ul class="portfolio-area"> 

            <?php while (have_posts() ) : the_post(); ?>

            <?php   
                    $custom = get_post_custom($post->ID);
                    $bimg = $custom["bimg"][0];
                    $shortxt = $custom["shortxt"][0];

                    $terms = get_the_terms( get_the_ID(), \'section\' );
                        ?>      

                    <li class="portfolio-item2" data-id="id-<?php echo $count; ?>" data-type="cat-item-<?php foreach ($terms as $term) { echo $term->term_id; } ?>">    
                    <div>
                   <span class="image-block">
                    <a class="image-zoom" href="<?php the_permalink() ?>" rel="" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'portfolio-img\', array(\'alt\' => \'\'.get_the_title().\'\', \'title\' => \'\'.get_the_title().\'\')); ?>
                    <span class="mglass"></span>
                    </a>
                    </span>
                   <div class="home-portfolio-text">
                    <h2 class="post-title-portfolio" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                    <p class="post-subtitle-portfolio"><?php echo $shortxt; ?></p>
                    </div>

                    </div>  
                    </li>               

                <?php $count++;  ?> 
            <?php endwhile; ?> 

            <?php wp_reset_query(); ?>
            <div class="column-clear"></div>
                    </ul><!--end portfolio-area -->
        </div><!--end portfolio-content -->

  <div class="prev-next">
        <?php if(function_exists(\'pagenavi\')) :pagenavi();  ?>


       <?php else : ?>
  <ul class="other-entries">
            <li class="newer-entries"><span><?php previous_posts_link(\'&laquo; Previous\') ?></span></li>
            <li class="older-entries"><span><?php next_posts_link(\'Next &raquo;\') ?></span></li>
          </ul>

<?php endif; ?>

</div>

</div><!-- end wrapper -->    
<?php get_footer(); ?>
我已经对此进行了几天的研究和尝试,但我对代码不太在行,无法让它正常工作。任何帮助都将不胜感激,因为客户希望此网站在a.s.a.p.完成。

谢谢

下面是函数的代码。php文件:

<?php
/**
 * @package WordPress
 * @subpackage Granit
 */


// Add custom post type
include("include/portfolio-custom-post-type.php");

// Add latest tweets custom widget
include("include/widget-twitter.php");

// Disable WordPress’s auto-formating filters
include("include/disable-autoformat.php");

if ( !function_exists( \'optionsframework_init\' ) ) {

/*-----------------------------------------------------------------------------------*/
/* Options Framework Theme
/*-----------------------------------------------------------------------------------*/

/* Set the file path based on whether the Options Framework Theme is a parent theme or child theme */

if ( STYLESHEETPATH == TEMPLATEPATH ) {
    define(\'OPTIONS_FRAMEWORK_URL\', TEMPLATEPATH . \'/admin/\');
    define(\'OPTIONS_FRAMEWORK_DIRECTORY\', get_bloginfo(\'template_directory\') . \'/admin/\');
} else {
    define(\'OPTIONS_FRAMEWORK_URL\', STYLESHEETPATH . \'/admin/\');
    define(\'OPTIONS_FRAMEWORK_DIRECTORY\', get_bloginfo(\'stylesheet_directory\') . \'/admin/\');
}

require_once (OPTIONS_FRAMEWORK_URL . \'options-framework.php\');

}

if ( function_exists(\'register_sidebar\') ) {
    register_sidebar(array(
        \'name\' => \'Sidebar\',
        \'before_widget\' => \'<li id="%1$s" class="widget %2$s">\',
        \'after_widget\' => \'</li>\',
        \'before_title\' => \'<h3 class="widgettitle">\',
        \'after_title\' => \'</h3>\',
    ));
    register_sidebar(array(
        \'name\' => \'Footer1\',
        \'before_widget\' => \'<li id="%1$s" class="widget %2$s">\',
        \'after_widget\' => \'</li>\',
        \'before_title\' => \'<h5 class="widgettitle2">\',
        \'after_title\' => \'</h5>\',
    ));
}

// Register Post Thumbnail
add_theme_support( \'post-thumbnails\' ); 
set_post_thumbnail_size(605,200,true);
add_image_size( \'blog-img\', 605, 200, true );
add_image_size( \'portfolio-img\', 225, 140, true );

// Register Custom Menus
add_action(\'init\', \'register_my_menu\');
function register_my_menu() {
register_nav_menu(\'menu-1\', __(\'Menu 1\'));
}


function custom_blog_posts(array $query = array())
{
    global $wp_query;
    wp_reset_query();

    $paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1;

    $defaults = array(
        \'paged\'             => $paged,
        );
    $query += $defaults;

    $wp_query = new WP_Query($query);
}



function custom_query_posts(array $query = array())
{
    global $wp_query;
    wp_reset_query();

    $paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1;

    $defaults = array(
        \'paged\'             => $paged
    );
    $query += $defaults;

    $wp_query = new WP_Query($query);
}

/*-----------------------------------------------------------------------------------*/
/*  Get related posts by taxonomy
/*-----------------------------------------------------------------------------------*/

function related_projects_taxonomy($post_id, $taxonomy, $args=array()) {
  $query = new WP_Query();
  $terms = wp_get_object_terms($post_id, $taxonomy);
  if (count($terms)) {
    // Assumes only one term for per post in this taxonomy
    $post_ids = get_objects_in_term($terms[0]->term_id,$taxonomy);
    $post = get_post($post_id);
    $args = wp_parse_args($args,array(
      \'post_type\' => $post->post_type, // The assumes the post types match
      \'post__in\' => $post_ids,
      \'taxonomy\' => $taxonomy,
      \'term\' => $terms[0]->slug,
      \'posts_per_page\' => 4
    ));
    $query = new WP_Query($args);
  }
  return $query;
}

function wpe_excerptlength_blog($length) { return 60; }
function wpe_excerptlength_index($length) { return 25;  }
function wpe_excerptmore($more) { return \'...\'; }
function wpe_excerpt($length_callback=\'\', $more_callback=\'\') {
    global $post;
    if(function_exists($length_callback)){
    add_filter(\'excerpt_length\', $length_callback);
    }
    if(function_exists($more_callback)){
    add_filter(\'excerpt_more\', $more_callback);
    }
    $output = get_the_excerpt();
    $output = apply_filters(\'wptexturize\', $output);
    $output = apply_filters(\'convert_chars\', $output);
    $output = \'<p>\'.$output.\'</p>\';
    echo $output;
    }

function mytheme_comment($comment, $args, $depth) {
   $GLOBALS[\'comment\'] = $comment; ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
     <div id="comment-<?php comment_ID(); ?>">
      <div class="comment-author vcard comment-left">
         <?php echo get_avatar($comment,$size=\'30\',$default=\'\'); ?>
</div>
        <div class="comment-right">
         <?php printf(__(\'<cite class="fn">%s</cite>\'), get_comment_author_link()) ?>

      <?php if ($comment->comment_approved == \'0\') : ?>
         <em><?php _e(\'Your comment is awaiting moderation.\') ?></em>
         <br />
      <?php endif; ?>

      <div class="comment-meta commentmetadata"><?php printf(__(\'%1$s at %2$s\'), get_comment_date(),  get_comment_time()) ?> <?php edit_comment_link(__(\'(Edit)\'),\'  \',\'\') ?></div>

      <?php comment_text() ?>
    </div>
    <div class="column-clear"></div>

      <div class="reply">
         <?php comment_reply_link(array_merge( $args, array(\'depth\' => $depth, \'max_depth\' => $args[\'max_depth\']))) ?>
      </div>

     </div><!-- end comment -->
<?php } 

function pagenavi( $p = 2 ) { // pages will be show before and after current page
  if ( is_singular() ) return; // don\'t show in single page
  global $wp_query, $paged;
  $max_page = $wp_query->max_num_pages;
  if ( $max_page == 1 ) return; // don\'t show when only one page
  if ( empty( $paged ) ) $paged = 1;
  echo \'<span class="pages">Page: \' . $paged . \' of \' . $max_page . \' </span> \'; // pages
  if ( $paged > $p + 1 ) p_link( 1, \'First\' );
  if ( $paged > $p + 2 ) echo \'... \';
  for( $i = $paged - $p; $i <= $paged + $p; $i++ ) { // Middle pages
    if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<span class=\'page-numbers current\'>{$i}</span> " : p_link( $i );
  }
  if ( $paged < $max_page - $p - 1 ) echo \'... \';
  if ( $paged < $max_page - $p ) p_link( $max_page, \'Last\' );
}
function p_link( $i, $title = \'\' ) {
  if ( $title == \'\' ) $title = "Page {$i}";
  echo "<a class=\'page-numbers\' href=\'", esc_html( get_pagenum_link( $i ) ), "\' title=\'{$title}\'>{$i}</a> ";
}
?>
这是包含/组合自定义帖子类型的代码。php。。。

<?php
add_action(\'init\', \'buildPortfolio\');
function buildPortfolio() {

        $labels = array(
        \'name\' => _x(\'Our Portfolio\', \'post type general name\'),
        \'singular_name\' => _x(\'Portfolio Item\', \'post type singular name\'),
        \'add_new\' => _x(\'Add New Item\', \'portfolio item\'),
        \'add_new_item\' => __(\'Add New Portfolio Item\'),
        \'edit_item\' => __(\'Edit Portfolio Item\'),
        \'new_item\' => __(\'New Portfolio Item\'),
        \'view_item\' => __(\'View Portfolio Item\'),
        \'search_items\' => __(\'Search Portfolio\'),
        \'not_found\' =>  __(\'Nothing found\'),
        \'not_found_in_trash\' => __(\'Nothing found in Trash\'),
        \'parent_item_colon\' => \'\'
        );

        $portfolio_args = array(
            \'labels\' => $labels,
            \'label\' => __(\'Portfolio2\'),
            \'singular_label\' => __(\'Portfolio3\'),
            \'public\' => true,
            \'show_ui\' => true,
            \'capability_type\' => \'post\',
            \'hierarchical\' => false,
            \'rewrite\' => true,
            \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'excerpt\')
        );
        register_post_type(\'portfolioitems\',$portfolio_args);

         $labelsPortfolioSection = array(
    \'name\' => _x( \'Sections\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Section\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Sections\' ),
    \'all_items\' => __( \'All Sections\' ),
    \'parent_item\' => __( \'Parent Section\' ),
    \'parent_item_colon\' => __( \'Parent Section:\' ),
    \'edit_item\' => __( \'Edit Section\' ),
    \'update_item\' => __( \'Update Section\' ),
    \'add_new_item\' => __( \'Add New Section\' ),
    \'new_item_name\' => __( \'New Section Name\' ),
  );    

  register_taxonomy(\'section\',\'portfolioitems\',array(
    \'hierarchical\' => true,
    \'labels\' => $labelsPortfolioSection
  ));

}

add_action("admin_init", "addPortfolio");
add_action(\'save_post\', \'updateDetails\');

function addPortfolio(){
    add_meta_box("portfolio_details", "Portfolio Options", "portfolioOptions", "portfolioitems", "normal", "low");
}

function portfolioOptions(){
    global $post;
    $custom = get_post_custom($post->ID);
    $shortxt = $custom["shortxt"][0];
    $bimg = $custom["bimg"][0];
    $year = $custom["year"][0];
    $client = $custom["client"][0];
    $performance = $custom["performance"][0];
    $lnk = $custom["lnk"][0];


?>

    <style>
        #portfolio-options label { font-size: 12px; margin: 0 15px 0 0; }
        #portfolio-options input { border: 1px #ddd solid; padding: 8px; width: 300px; }
        .opt2{margin-top:10px;}
    </style>

    <div id="portfolio-options">
        <div><label>Short Text:</label><input name="shortxt" value="<?php echo $shortxt; ?>" /></div>       
        <div class="opt2"><label>Big Image URL:</label><input name="bimg" value="<?php echo $bimg; ?>" /></div> 
        <div class="opt2"><label>Project Year:</label><input name="year" value="<?php echo $year; ?>" /></div>      
        <div class="opt2"><label>Client Name:</label><input name="client" value="<?php echo $client; ?>" /></div>       
        <div class="opt2"><label>Performance:</label><input name="performance" value="<?php echo $performance; ?>" /></div>     
        <div class="opt2"><label>Site Link:</label><input name="lnk" value="<?php echo $lnk; ?>" /></div>   


    </div><!--end portfolio-options-->   

<?php

}
function updateDetails(){
    global $post;
    update_post_meta($post->ID, "shortxt", $_POST["shortxt"]);
    update_post_meta($post->ID, "bimg", $_POST["bimg"]);
    update_post_meta($post->ID, "year", $_POST["year"]);
    update_post_meta($post->ID, "client", $_POST["client"]);
    update_post_meta($post->ID, "performance", $_POST["performance"]);
    update_post_meta($post->ID, "lnk", $_POST["lnk"]);

} 

add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");

function portfolio_edit_columns($portfolio_columns){
    $portfolio_columns = array(
        "cb" => "<input type=\\"checkbox\\" />",
        "title" => "Project Title",
    );
    return $portfolio_columns;
}
?>

1 个回复
最合适的回答,由SO网友:jdm2112 整理而成

根据您是否打算使用此模板在其他地方显示其他类别,您可能需要一份以该类别命名的公文包模板文件的副本。

要从post type“portfolioitems”中选择要显示的单个类别,请修改以下行:

$wp_query = new WP_Query(array(\'post_type\' => \'portfolioitems\',\'posts_per_page\' => 50,\'paged\'=> $paged));
收件人:

$wp_query = new WP_Query(array(\'post_type\' => \'portfolioitems\', \'cat=4\', \'posts_per_page\' => 50,\'paged\'=> $paged));
这假设所需类别的ID为4。只需向WP\\u查询数组添加一个额外的键/值,就会包含该过滤器,并排除任何与额外条件不匹配的帖子。

您可以按名称、ID等筛选类别。这里详细介绍了可用的各种方法
WP Codex ref 对于类别参数。

**编辑到分类查询**基于此附加信息,您似乎需要特定于自定义帖子类型“portfolioitems”的分类过滤器。

$wp_query = new WP_Query(array(\'post_type\' => \'portfolioitems\', \'section\' => \'sectionname\', \'posts_per_page\' => 50,\'paged\'=> $paged));
将“sectionname”替换为所需节的名称。有关的完整详细信息taxonomy parameters

结束

相关推荐

Show post categories

我正在尝试显示一些面包屑的帖子类别。目前,我有:the_category(\' / \', \'multiple\'); 但出于某种原因,它两次声明了父类别(我只想parent > child):FASHION / DAILY FASHION CANDY / FASHION 它应该是:FASHION / DAILY FASHION CANDY 有人知道它为什么这样做,以及如何改变它吗?