gpt4 book ai didi

wordpress - 显示每个类别类型的第一篇文章(WordPress)

转载 作者:行者123 更新时间:2023-12-05 02:20:21 28 4
gpt4 key购买 nike

我是 WordPress 的新手,正在尝试在主页的一部分中使用一些自定义代码,该部分旨在显示三个类别(白皮书、文章和帖子)中每个类别的 #1 趋势帖子.代码只是显示(据我所知)最近的 3 个帖子,无论类别类型如何:

$args = array('posts_per_page' => 3);
$loop2 = new WP_Query($args);
while ($loop2->have_posts()) : $loop2->the_post();
$thumbnail = get_post_thumbnail_id(); $imageurl = wp_get_attachment_image_src($thumbnail,'theme-360x240');
$class=""; $categories = get_the_category();
if($categories[0]->slug == 'articles'){
$class="article-icon";
} else if($categories[0]->slug == 'videos' || $categories[0]->slug == 'webinars'){
$class="video-icon";
} else if($categories[0]->slug == 'white-papers' || $catgories[0]->slug == 'case-studies' || $categories[0]->slug == 'industry-information' || $categories[0]->slug == 'posts') {
$class="whitepapers-icon";
} else {
$class="event-icon";
}
$posttype = get_post_type(get_the_ID()); ?>
<div class="card radius shadowDepth1">
<div class="card__image border-tlr-radius">

<img src="<?php if(!empty($imageurl[0])) { echo $imageurl[0]; } else { echo 'http://placehold.it/360x205'; } ?>" alt="image" class="border-tlr-radius">
</div>

<div class="card__content card__padding">
<div class="card__share"> <?php $iconurl = z_taxonomy_image_url($categories[0]->term_id); ?>
<a <?php if($iconurl) { ?>style="background:url(<?php echo $iconurl; ?>);"<?php } ?> id="share" class="share-toggle share-icon <?php echo $class; ?>" href="<?php echo get_category_link($category[0]->term_id); ?>"></a>
</div>

<div class="card__meta">
<?php if($posttype == 'tribe_events') { ?>
<a href="<?php echo home_url(); ?>/events">Events</a>
<?php } else { ?>
<a href="<?php echo get_category_link($categories[0]->term_id); ?>"><?php echo $categories[0]->name; ?></a>
<?php } ?>
</div>

<article class="card__article">
<h2><a class="show_pop" href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a></h2>

<p><?php $content = get_the_content(); echo wp_trim_words($content,'15','...'); ?></p>
<a class="show_pop read-more" href="<?php echo get_permalink(); ?>">Read More</a>
</article>
</div>
</div>

这些代码都不是我创建的,所以我不能说它做得有多好或有多可靠……但就是这样。

如果这是一个愚蠢的问题,我深表歉意——我只是从来没有在 WordPress 上做过太多事情!任何帮助将不胜感激。

最佳答案

将 WP API 和各种教程中的点点滴滴放在一起后,此设置似乎按照我需要的方式工作:

<?php
$do_not_duplicate = array();

$categories = get_categories(
array(
'include'=> '7,8,1'
)
);
$do_not_duplicate = array();

foreach ( $categories as $category ) {
$args = array(
'cat' => $category->term_id,
'orderby' =>'post_date',
'order' => 'DESC',
'post_type' => 'post',
'posts_per_page' => '1',

);$query = new WP_Query( $args );

if ( $query->have_posts() ) {
$posttype = get_post_type(get_the_ID());?>

<?php while ( $query->have_posts() ) {

$query->the_post();
$do_not_duplicate[] = $post->ID;
?>
<div class="card radius shadowDepth 1">
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<!--This is supposed to get the thumbnail-->
<div class="card__image border-tlr-radius">
<?php

$thumbnail = get_post_thumbnail_id(); $imageurl = wp_get_attachment_image_src($thumbnail,'theme-360x240');
$class=""; $categories = get_the_category();
if($category->slug == 'articles'){
$class="article-icon";
} else if($category->slug == 'white-papers' || $category->slug == 'posts') {
$class="whitepapers-icon";
} else {
$class="event-icon";
}
?>

<?php if ( has_post_thumbnail() ) { ?>
<img src="<?php if(!empty($imageurl[0])) { echo $imageurl[0]; } else { echo 'http://placehold.it/360x205'; } ?>" alt="image" class="border-tlr-radius">

<?php } ?>
</div>
<div class="card__content card__padding">
<div class="card__share">
<?php $iconurl = z_taxonomy_image_url($category->term_id); ?>
<a <?php if($iconurl) { ?>style="background:url(<?php echo $iconurl; ?>);"<?php } ?> id="share" class="share-toggle share-icon <?php echo $class; ?>" href="<?php echo get_category_link($category->term_id); ?>"></a>
</div>
<div class="card__meta">

<a href="<?php echo get_category_link($category->term_id); ?>"><?php echo $category->name; ?></a>

</div>
<article class="card__article">
<h2>
<a href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a>
</h2>
<!--this is the exerpt of the post-->
<p><?php $content = get_the_content(); echo wp_trim_words($content,'15','...'); ?></p>
<a class="read-more" href="<?php echo get_permalink(); ?>">Read More</a>

</article>
</div>
</article>
</div>
<?php } // end while ?>


<?php } // end if

// Use reset to restore original query.
wp_reset_postdata();
}

关于wordpress - 显示每个类别类型的第一篇文章(WordPress),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39470314/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com