我是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中从未做过多少事!任何帮助将不胜感激。
答案 0 :(得分:1)
将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();
}
答案 1 :(得分:0)
首先,你必须获得所有类别列表,然后你必须循环它 在 wp_query 中并传递term_id
<?php
//https://developer.wordpress.org/reference/functions/get_categories/
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'slug' => array('papers','article','post') //or you can use 'include'=>array('1,2,3')
) );
foreach( $categories as $category ) {
$args=array(
'posts_per_page' => 1,
'cat' => $category->term_id,
);
//https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
$loop2 = new WP_Query( $args );
while ( $loop2->have_posts() ) : $loop2->the_post() ?>
<div class="post">
<?php the_title() ?>
<!-- do whatever you else you want that you can do in a normal loop -->
</div>
<?php endwhile
}
答案 2 :(得分:0)
为每个类别创建三个查询/循环:
$articles = new WP_Query(array('category_name' => 'articles', 'posts_per_page' => 3));
while ($articles->have_posts()) {
...
}
$articles = new WP_Query(array('posts_per_page' => 3));
while ($articles->have_posts()) {
...
}
$white_papers = new WP_Query(array('category_name' => 'white-papers', 'posts_per_page' => 3));
while ($white_papers->have_posts()) {
...
}
答案 3 :(得分:0)
获取动态类别。这段代码对我有用。
<?php
$do_not_duplicate = array();
$categories = get_categories(
array(
'orderby' => 'name',
'parent' => 0
)
);
$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="swiper-slide boxNews">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'homepage-thumb', array( 'class' => '' ) );
echo '<img class="topLink" src="';
echo get_bloginfo('template_url').'/inc/assets/images/toplink.png';
echo '" />';
}
?>
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php } // end while ?>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
}
?>