تكرار من خلال نوع النشر المخصص حسب نوع التصنيف المخصص؟ (طلب منشورات WordPress حسب الفئة ، أو عرض نوع النشر المخصص حسب مصطلح التصنيف)
-
27-09-2019 - |
سؤال
أريد أن أحصل على صفحة تعرض جميع المنشورات ، مفصولة حسب الفئة. والفكرة هي الحصول على الفئات ، ثم التكرار من خلال جميع المنشورات لكل فئة. المشكلة معقدة بسبب حقيقة أنني أريد التكرار من خلال جميع المنشورات من نوع مخصص معين ، باستخدام تصنيف مخصص كفئات. (تشغيل WordPress 3)
في وظيفتي
في قالب الصفحة المخصصة الخاصة بي التي من المفترض أن تعرض جميع مقاطع الفيديو التي يتم ترتيبها حسب الفئة ، هذا هو الرمز الذي لا يعيد أي منشورات (وهناك ، لقد راجعت):
<?php
$categories = get_categories(array(
'taxonomy' => 'video_types'
));
foreach ($categories as $cat):
?>
<section id="<?php $cat->slug ?>" class="video-category">
<?php
query_posts(array(
'cat' => $cat->cat_ID,
'posts_per_page' => -1
));
?>
<h2><?php single_cat_title(); ?></h2>
<p class="description"><?php echo category_description($cat->cat_ID); ?></p>
<?php while (have_posts()) : the_post(); ?>
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<article class="video">
<h3><?php the_title(); ?></h3>
<p>
<?php the_content() ?>
</p>
</article>
<?php endwhile; ?>
</section>
<?php endforeach; ?>
المحلول
JEEZ ، بمجرد معرفة أن كل عنصر من عناصر تصنيف مخصص يسمى مصطلح (ليس واضحًا على الفور في مستندات WordPress لـ NOOB) ، كل شيء أبسط بكثير للبحث عنه. هذا الحل أسهل في الفهم دون كل الأشياء المخصصة للاستعلام.
<?php
// A term is an item of a taxonomy (e.g. "Promotional" could be a term for the taxonomy "video_type")
// ...so $categories could be $terms and it would still make sense
$categories = get_terms('taxonomy_name');
foreach( $categories as $category ):
?>
<section class="category-<?php echo $category ?>">
<h2><?php echo $category->name; // Print the cat title ?></h2>
<p class="description"><?php echo $category->description ?></p>
<div class="<?php echo $category->post_type ?>-list">
<?php
//select posts in this category (term), and of a specified content type (post type)
$posts = get_posts(array(
'post_type' => 'custom_post_type_name',
'taxonomy' => $category->taxonomy,
'term' => $category->slug,
'nopaging' => true, // to show all posts in this category, could also use 'numberposts' => -1 instead
));
foreach($posts as $post): // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
// Now you can do things with the post and display it, like so
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<?php
// Getting custom field data example
echo get_post_meta($post->ID, 'field_key', true);
?>
<?php the_content() ?>
</article>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
ثم يمكن ملء أي فجوات في الفهم عن طريق البحث في الوظائف المذكورة أعلاه في مخطوطة WordPress. في الكود أعلاه ، بالنسبة لتطبيقي المحدد ، سيكون Custom_Post_Type_Name هو الفيديو ، وسيكون التصنيف infly_name video_type (أو video_types ، نسيت).
نصائح أخرى
قد تجرب نهجا آخر. حاول استخدام get_posts للحصول على مشاركاتك بتصنيفك حسب التصنيف المخصص الخاص بك ، وإعداد متغير هو في البداية سلسلة فارغة (تسمى $ current_cat أو شيء ما) ، ومع كل حلقة من النتائج ، تحقق من التصنيف ومقارنته بـ $ current_cat - إذا مختلف ، قم بطباعة رأسًا للفئة الجديدة ، ثم قم بطباعة الإدخال ، إذا كان الأمر نفسه ، إدخالًا كالمعتاد.
هناك مشكلة واضحة مع الكود الخاص بك (على ما أظن) هي أنك لا تستفسر بشكل صحيح من تصنيفك المخصص. يجب أن تستخدم ببساطة taxonomy_name => 'value'
ضمن استفسارك ، لن يتم لمس تصنيف مخصص cat
في استعلام.
اسمحوا لي أن أعرف إذا كنت بحاجة إلى مزيد من التفاصيل.
تحرير: مزيد من التفاصيل!
// get a list of categories, in this case your custom taxonomy (your_taxonomy_name)
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'your_taxonomy_name'";
$categories = $wpdb->get_results($querystr, OBJECT);
foreach( $categories as $category ): // begin a loop through those terms (categories in your custom taxonomy)
echo '<div class="category-header"><h3>'.$category->name.'</h3>'; // print the cat title
echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'your_taxonomy_name')).'</p></div>'; // cat description
$posts = get_posts( array( 'your_taxonomy_name' => $category->name, 'post_type' => 'your_post_type' ) ) //select posts in this category, and of a specified content type
foreach($posts as $post) : // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
[ ... ] // do things with your post (display it)
endforeach;
endforeach;
يجب أن تفعل ذلك - و هذه قد يكون مفيدًا لاستخدام Get_posts.