मेरे पास एक कस्टम पोस्ट प्रकार है जिसे लघु पाठ्यक्रम कहा जाता है और मैंने इसमें एक कस्टम वर्गीकरण जोड़ा है जिसे कोर्स प्रकार कहा जाता है, जो मुझे पाठ्यक्रमों को वर्गीकृत करने में सक्षम बनाता है ...
मैं एक पेज टेम्पलेट बनाने की कोशिश कर रहा हूं जो सभी पाठ्यक्रमों को एक ही वर्गीकरण (श्रेणी) के साथ दिखाएगा। इस मामले में मैं उन सभी पाठ्यक्रमों को दिखाना चाहता हूं जिन्हें वर्गीकरण (श्रेणी) एडोब सौंपा गया है ... मैंने कुछ अलग चीजों की कोशिश की है, लेकिन मेरा नवीनतम कोड यहां है:
<?php
// get the custom post type's taxonomy terms
$custom_taxterms = wp_get_object_terms( $post->ID, 'adobe', array('fields' => 'ids') );
// arguments
$args = array(
'post_type' => 'short_courses',
'post_status' => 'publish',
'posts_per_page' => 20, // you may edit this number
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'adobe',
'field' => 'id',
'terms' => $custom_taxterms
)
),
'post__not_in' => array ($post->ID),
);
$related_items = new WP_Query( $args );
// loop over query
if ($related_items->have_posts()) :
echo '<ul>';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
echo '</ul>';
endif;
// Reset Post Data
wp_reset_postdata();
?>
यह कुछ भी नहीं खींचता है ...
मैंने इस कोड के साथ भी कोशिश की है:
<?php
$args = array(
'post_type' => 'short_courses',
'orderby' => 'title',
'order' => 'ASC',
'product_categories' => 'adobe'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
?>
<p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php $products_count = $the_query->current_post + 1; ?>
<?php if ( $products_count % 4 == 0): ?>
<?php endif; ?>
<?php endwhile; endif; ?>
लेकिन यह कोड केवल Adobe वाले ही नहीं, सभी लघु पाठ्यक्रमों के माध्यम से खींचता है ...
क्या कोई मुझे बता सकता है कि मैं कहाँ गलत हो रहा हूँ?
2 जवाब
ठीक है, मैं इसे ठीक करने में कामयाब रहा - यह बस एक छोटी सी गलती थी। ऊपर दिए गए कोड के दूसरे लॉट में, वह हिस्सा जो कहता है कि product_categories को course_type में बदलने की आवश्यकता है! तो यहाँ नया कार्य कोड है:
<?php
$args = array(
'post_type' => 'short_courses',
'orderby' => 'title',
'order' => 'ASC',
'course_type' => 'adobe'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
?>
<p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php $products_count = $the_query->current_post + 1; ?>
<?php if ( $products_count % 4 == 0): ?>
<?php endif; ?>
<?php endwhile; endif; ?>
क्या आपने इस लाइन पर ध्यान दिया है?
<?php if ( have_posts() ) : while
क्या आप इसे बदल सकते हैं
<?php if ( $the_query->have_posts() ) : while
और देखें कि क्या फ़िल्टर लागू किया गया है? वर्डप्रेस में वैश्विक पोस्ट फ़ंक्शन जैसे get_the_title() हैं और यह आपके कस्टम क्वेरी ऑब्जेक्ट में हस्तक्षेप कर सकता है जैसे कि आप $the_query का उपयोग कर रहे हैं।
मैं आपको यह भी सलाह दूंगा कि कोड की पठनीयता में सुधार के लिए समय से पहले if कथन को हटा दें। जबकि लूप पेज पर कोई आउटपुट नहीं देगा यदि क्वेरी में कोई पोस्ट नहीं है तो इफ स्टेटमेंट की आवश्यकता है।
पूरा कोड तब बन जाता है
<?php
$args = array(
'post_type' => 'short_courses',
'orderby' => 'title',
'order' => 'ASC',
'product_categories' => 'adobe'
);
$the_query = new WP_Query( $args );
?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
?>
<p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php $products_count = $the_query->current_post + 1; ?>
<?php if ( $products_count % 4 == 0): ?>
<?php endif; ?>
<?php endwhile; ?>
कृपया सुनिश्चित करें कि 'product_categories' => 'adobe' पर आप जिस टैक्सोनॉमी नाम का उपयोग कर रहे हैं वह वही है जिसका उपयोग आपने register_taxonomy फ़ंक्शन पर किया था अर्थात सुनिश्चित करें कि आपके पास >register_taxonomy('product_categories', array('short_courses'), $args ); अगर आपके पास product_categories पर कुछ और है, तो क्वेरी पर उसका इस्तेमाल करें। उम्मीद है ये मदद करेगा।
संबंधित सवाल
नए सवाल
wordpress
यह टैग वर्डप्रेस सामग्री प्रबंधन प्रणाली के बारे में प्रोग्रामिंग-विशिष्ट प्रश्नों के लिए है। ऑफ-टॉपिक प्रश्नों में थीम डेवलपमेंट, वर्डप्रेस एडमिनिस्ट्रेशन, मैनेजमेंट बेस्ट प्रैक्टिस, सर्वर कॉन्फिगरेशन आदि शामिल हैं। ये सबसे अच्छे वर्डप्रेस डेवलपमेंट स्टैक एक्सचेंज साइट (https://wordpress.stackexchange.com) पर पूछे जाते हैं।