ฉันค่อนข้างใหม่ในการพัฒนาชุดรูปแบบ WordPress และฉันไม่ได้เป็น PHP (ฉันมาจาก Java และ C #) และมีสถานการณ์ต่อไปนี้ในชุดรูปแบบที่กำหนดเองนี้
อย่างที่คุณเห็นในหน้าแรกฉันแสดงส่วน (ชื่อArticoli ใน evidenza ) ครั้งแรกที่มีโพสต์เด่น (ฉันได้ติดตั้งโดยใช้แท็กเฉพาะ) และภายใต้มันมีพื้นที่อื่น (ชื่อUltimi Articoli ) ที่มีโพสต์ล่าสุด นั่นไม่ใช่โพสต์เด่น
ฉันจะใช้รหัสนี้:
<section id="blog-posts">
<header class="header-sezione">
<h2>Articoli in evidenza</h2>
</header>
<!--<?php query_posts('tag=featured');?>-->
<?php
$featured = new WP_Query('tag=featured');
if ($featured->have_posts()) :
while ($featured->have_posts()) : $featured->the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part('content', get_post_format());
endwhile;
wp_reset_postdata();
else :
// If no content, include the "No posts found" template.
get_template_part('content', 'none');
endif;
?>
<header class="header-sezione">
<h2>Ultimi Articoli</h2>
</header>
<?php
// get the term using the slug and the tag taxonomy
$term = get_term_by( 'slug', 'featured', 'post_tag' );
// pass the term_id to tag__not_in
query_posts( array( 'tag__not_in' => array ( $term->term_id )));
?>
<?php
if (have_posts()) :
// Start the Loop.
while (have_posts()) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part('content', get_post_format());
endwhile;
else :
// If no content, include the "No posts found" template.
get_template_part('content', 'none');
endif;
?>
</section>
ใช้งานได้ดี แต่ฉันมีข้อสงสัยเกี่ยวกับคุณภาพของโซลูชันนี้และวิธีการทำงาน
ในการเลือกโพสต์เด่นทั้งหมดฉันใช้บรรทัดนี้ที่สร้างWP_Query
วัตถุใหม่ที่กำหนดคิวรีที่มีแท็กเฉพาะfeatured
:
$featured = new WP_Query('tag=featured');
จากนั้นฉันทำซ้ำผลการค้นหานี้โดยใช้have_posts()
วิธีการ
ดังนั้นจากสิ่งที่ฉันเข้าใจนี่ไม่ใช่แบบสอบถามหลักของ WordPress แต่เป็นคำถามใหม่ที่ฉันสร้างขึ้น จากสิ่งที่ฉันเข้าใจจะเป็นการสร้างแบบสอบถามใหม่ (ตามที่ทำ) ดีกว่าและไม่ใช้คิวรีหลักเมื่อฉันต้องการดำเนินการชนิดนี้
มันเป็นความจริงหรือว่าฉันขาดอะไรไป? ถ้าเป็นจริงคุณช่วยอธิบายฉันได้ไหมว่าทำไมมันถึงดีกว่าที่จะสร้างคิวรีแบบกำหนดเองใหม่และไม่แก้ไขคิวรีหลักของ Wordpress
ตกลงไปต่อ ฉันแสดงโพสต์ทั้งหมดที่ไม่มีแท็ก 'ที่โดดเด่น' หากต้องการทำสิ่งนี้ฉันใช้ข้อมูลโค้ดนี้ซึ่งในทางกลับกันจะแก้ไขคิวรีหลัก:
<?php
// get the term using the slug and the tag taxonomy
$term = get_term_by( 'slug', 'featured', 'post_tag' );
// pass the term_id to tag__not_in
query_posts( array( 'tag__not_in' => array ( $term->term_id )));
?>
<?php
if (have_posts()) :
// Start the Loop.
while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile;
else :
// If no content, include the "No posts found" template.
get_template_part('content', 'none');
endif;
?>
ดังนั้นฉันคิดว่านี่เป็นสิ่งที่น่ากลัวมาก จริงป้ะ?
UPDATE:
ที่จะทำดำเนินการเดียวกันผมพบว่าฟังก์ชั่นนี้ (ในคำตอบที่ดีด้านล่าง) ที่ผมได้เพิ่มfunctions.php
function exclude_featured_tag( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'tag__not_in', 'array(ID OF THE FEATURED TAG)' );
}
}
add_action( 'pre_get_posts', 'exclude_featured_tag' );
ฟังก์ชั่นนี้มีตะขอที่เรียกว่าหลังจากสร้างวัตถุตัวแปรแบบสอบถาม แต่ก่อนที่จะเรียกใช้แบบสอบถามที่เกิดขึ้นจริง
ดังนั้นจากสิ่งที่ฉันเข้าใจจะใช้วัตถุแบบสอบถามเป็นพารามิเตอร์ป้อนข้อมูลและปรับเปลี่ยน (จริงกรอง) โดยเลือกโพสต์ทั้งหมดยกเว้นแท็กเฉพาะ (ในกรณีของฉันfeatured
โพสต์แท็ก)
ดังนั้นฉันจะใช้การค้นหาก่อนหน้า (อันที่เคยแสดงการโพสต์เด่น) กับฟังก์ชั่นนี้เพื่อแสดงเฉพาะโพสต์ที่ไม่โดดเด่นในชุดรูปแบบของฉันได้อย่างไร หรือฉันต้องสร้างแบบสอบถามใหม่