如何使用自己的自定义键显示所有 Wordpress 帖子 [英] How to display all Wordpress posts with own custom key
问题描述
我想通过我自己的自定义键expiration_date"获取并显示所有帖子.
I'd like to get and display all posts by my own custom key "expiration_date".
我使用 WCK 插件来添加我的自定义密钥.我添加了两个字段:expiration_date 和 price.
I use WCK plugin for adding my custom keys. I've added two fields: expiration_date and price.
我尝试自己编写它,并在模板 function.php 文件中创建了我的 owf 函数courses_by_date"并显示courses_by_date();在 content-page.php 文件中,当我想显示添加了上述两个字段的所有帖子时.
I tried to write it by myself and I created my owf function 'courses_by_date' in template function.php file and display courses_by_date(); in content-page.php file, when I want to display all posts with added these above two fields.
非常重要:有些帖子可能有几对字段:expiration_date 和 price.
Very important: some posts may have several pairs of fields: expiration_date and price.
现在,我只能使用这些键显示每个帖子的第一对这些字段.
Now, I can display only the first pair of these fields of each post with these keys.
function courses_by_date() {
$args = array("posts_per_page" => 20, "meta_key" => 'expiration_date');
$posts_array = get_posts($args);
foreach($posts_array as $i => $post)
{
echo $i . " <a class='title' href='https://post-url.com'>" . $post->post_title . "</a> | " . $post->expiration_date . ", price: " . $post->price . " EUR " . "<br>";
}
}
推荐答案
您可以在元键的末尾传递增量参数.检查下面的代码.
You can pass increment params at end of the meta key. check below code.
function kursy_wg_daty() {
$a = 0;
echo "<ul>";
while($a < 20) {
if($a == 0) {
$args = array("posts_per_page" => 20, "meta_key" => 'expiration_date');
$posts_array = get_posts($args);
foreach($posts_array as $post){
echo "<li> <a class='title' href=" . post_permalink($post) . ">" . $post->post_title . "</a> | " . $post->expiration_date . ", price: " . $post->price . " zł " . "</li>";
}
} else {
$args = array("posts_per_page" => 20, "meta_key" => "expiration_date" . "_" . $a);
$posts_array = get_posts($args);
foreach($posts_array as $post) {
$expiration_date = get_post_meta( $post->ID, 'expiration_date_'.$a, true );
$price = get_post_meta( $post->ID, 'price_'.$a, true );
echo "<li> <a class='title' href=" . post_permalink($post) . ">" . $post->post_title . "</a> | " . $expiration_date . ", price: " . $price . " zł " . "</li>";
}
}
$a++;
}
echo "</ul>";
}
这篇关于如何使用自己的自定义键显示所有 Wordpress 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!