Ich habe einen benutzerdefinierten Beitragstyp mit Kategorien und Unterkategorien erstellt. Ich muss nur die Beitragstitel und -bilder für eine bestimmte Unterkategorie oder Kategorie in einer Seitenvorlage auflisten.
Ich habe bis jetzt alle aufgelisteten Artikel im benutzerdefinierten Beitragstyp erhalten, bin mir aber nicht sicher, wie ich weiter vorgehen soll ... jede Hilfe ist willkommen.
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
Die Funktion, mit der der benutzerdefinierte Beitragstyp und die Taxonomie erstellt werden, sieht folgendermaßen aus:
<?php
// CUSTOM POST TYPE 1
add_action('init', 'portfolio_register');
function portfolio_register() {
$args = array(
'label' => __('Portfolio'),
'singular_label' => __('Portfolio'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'thumbnail')
);
register_taxonomy("galleries", array("portfolio"), array(
"hierarchical" => true,
"label" => "Galleries",
"singular_label" => "Galleries",
"rewrite" => true)
);
register_post_type( 'portfolio' , $args );
}
add_action("admin_init", "admin_init");
add_action('save_post', 'save_portfolio_options');
add_action('save_post', 'save_portfolio_single_options');
function admin_init(){
add_meta_box("gallerymeta", "Gallery Options", "portfolio_meta_options", "portfolio", "normal", "low");
add_meta_box("portfoliometa", "Portfolio Item Options", "portfolio_single_meta_options", "portfolio", "side", "low");
}
function portfolio_meta_options(){
global $post;
$custom = get_post_custom($post->ID);
$excerpt = $custom["excerpt"][0];
$info = $custom["info"][0];
$linkto = $custom["linkto"][0];
?>
Dies ist eine Version einer Funktion, die ich in dem Framework verwende, an dem ich arbeite. Beispiel-HTML ersetzt eine andere Funktion, die so etwas enthält.
// Custom Loop
function arrr_custom_loop( $r_type = 'post', $r_post_num, $r_tax = 'category', $r_terms = 'featured' ) {
$args = array(
'showposts' => $r_post_num,
'tax_query' => array(
array(
'post_type' => $r_type,
'taxonomy' => $r_tax,
'field' => 'slug',
'terms' => array(
$r_terms
),
)
)
);
query_posts( $args );
if (have_posts())
while ( have_posts() ) : the_post();
$more = 0;
?>
<article>
<header class="pagetitle">
<?php if ( is_singular() ) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h2 class="entry"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php } ?>
</header>
<div class="content_wrapper">
<div class="content">
<?php the_content(); ?>
</div>
</div>
<?php if ( comments_open() && ! post_password_required() ) { ?>
<div class="comments_wrapper">
<div class="comments">
<?php comments_template(); ?>
</div>
</div>
<?php } ?>
</article>
<?php endwhile;
wp_reset_query();
}
Sie würden dann einfach die Funktion mit den erstellten Argumenten verwenden:
arrr_custom_loop( 'portfolio', 10, 'galleries', 'pirates' );
Sie können es bekommen, wenn Sie Kategorie/Unterkategorie Schnecke haben/kennen ...
Übergeben Sie es einfach im Array args für query_posts.
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10 ,'taxonomy_name'=>'slug of category/subcategory');
query_posts($args);
Ich habe dies zuvor verwendet, um mehr als das zu tun, was Sie benötigen, aber wenn Sie die zweite Taxonomie entfernen, werden alle Posts in einer bestimmten Kategorie für Sie aufgelistet:
<?php
// set up taxonomies
$tax_one = 'project_category';
$tax_two = 'brand';
$post_type = 'project';
$categories = get_categories( array(
'type' => $post_type,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'taxonomy' => $tax_one ));
foreach ( $categories as $category ) : // list all categories
echo '<li><a href="'.get_term_link( $category->slug, $tax_one ).'">'.$category->name.'</a><ul>'?>
<?php $terms = get_terms( $tax_two, array( 'hide_empty' => 0 ) );
foreach ( $terms as $term ) : // list all brands in each category
$myquery['tax_query'] = array(
array(
'taxonomy' => $tax_one,
'terms' => array($category->slug),
'field' => 'slug',
),
array(
'taxonomy' => $tax_two,
'terms' => array($term->slug),
'field' => 'slug',
)
);
$the_posts = new WP_Query($myquery);
if ( $the_posts->have_posts() ) : // if there are posts in the current brand and category then display it
echo '<li><a href="'.get_term_link( $term->slug, $tax_two ).'">'.$term->name.'</a></li>';
endif;
endforeach; ?>
<?php echo '</ul></li>';
endforeach; ?>
so passen Sie es Ihrer Situation an:
<?php
// set up taxonomies
$tax_one = 'category';
$post_type = 'portfolio';
$categories = get_categories( array(
'type' => $post_type,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'taxonomy' => $tax_one ));
foreach ( $categories as $category ) : // list all categories
echo '<li><a href="'.get_term_link( $category->slug, $tax_one ).'">'.$category->name.'</a><ul>'?>
<?php $the_posts = new WP_Query($myquery);
if ( $the_posts->have_posts() ) : // if there are posts in the current category then display it
foreach($the_posts as $post) :
echo '<li>Your Post stuff here</li>';
endforeach;
endif;
endforeach; ?>
<?php echo '</ul></li>';
endforeach; ?>
Ich habe das noch gar nicht getestet, hoffe aber, dass es hilft!
Prost,
Dave
Rajeevs Antwort funktionierte für mich mit Ihrem Code. In einer leeren Installation habe ich Folgendes ausgeführt:
portfolio_register
-Funktion und den add_action
-Hook enthält, der sie aufruftindex.php
erstellt, der nur den ersten eingefügten Codeblock enthält. Die einzige Änderung, die ich vorgenommen habe, war das Hinzufügen von 'galleries'=>'aerial'
zu Ihrer $args
-Variablen:$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10, 'galleries'=>'aerial' );
Es wurden nur "Antenne 1" und "Antenne 2" angezeigt.
Gibt es eine Möglichkeit, dass Ihr Code nicht aufgerufen wird (möglicherweise eine andere Vorlage)?