Genesis 自定义 post 类型分页与自定义分类查询循环
Genesis custom post type pagination with custom taxonomy query loop
我正在尝试为包含来自特定分类法的所有帖子的页面创建 Genesis 分页。我会使用此 WordPress Stack Exchange 中提供的许多解决方案,但我不知道如何将他们的 wp_query
与我的相结合。我该怎么做呢? (请记住,我绝不是专业人士。)
这是我在尝试集成分页之前模板中的内容:
remove_action('genesis_loop','genesis_do_loop');
add_action('genesis_loop','get_article_content');
function get_article_content(){
$myterms = get_terms('article-category', 'orderby=none&hide_empty');
foreach ($myterms as $term) :
$args = array(
'post_type' => 'solar-articles',
'tax_query' => array(
array(
$term->slug
)
),
);
// assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);
endforeach;
// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();
?><div class="col-md-12"><?php
the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' );
?><p class="entry-time">POSTED <?php the_time('F j, Y'); ?></p>
<div class="entry-content"><?php
the_excerpt();
?></div></div><?php
endwhile;
}
genesis();
这是我尝试集成后的结果:
remove_action('genesis_loop','genesis_do_loop');
add_action('genesis_loop','get_article_content');
function get_article_content(){
$paged = 1;
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
if ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
$paged = intval( $paged );
$myterms = get_terms('article-category', 'orderby=none&hide_empty');
foreach ($myterms as $term) :
$args = array(
'posts_per_page' => 3,
'post_type' => 'solar-articles',
'tax_query' => array(
array(
$term->slug
)
),
'paged' => $paged
);
// assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);
endforeach;
// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();
?><div class="col-md-12"><?php
the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' );
?><p class="entry-time">POSTED <?php the_time('F j, Y'); ?></p>
<div class="entry-content"><?php
the_excerpt();
?></div></div><?php
endwhile;
genesis_posts_nav();
}
genesis();
您的 foreach 循环似乎在执行任何操作之前覆盖了 $wp_query 变量。如果你的分类法是一个类别,我创建了一个页面,只显示一个特定的类别并分页。我把它放在 page-whatever.php:
$myCategory = get_cat_ID('Knowledgebase');
remove_action('genesis_loop','genesis_do_loop');
add_action('genesis_loop', 'mry_events_do_loop');
function mry_events_do_loop() {
global $mry_knowledgebaseCatId;
$paged = (get_query_var('paged'))? get_query_var('paged'): 1;
$args = array('cat' => $mry_knowledgebaseCatId, 'posts_per_page' => 5, 'post_type' => 'post', 'paged' => $paged);
genesis_custom_loop($args);
}
这是我如何自定义条目的外观
add_action('genesis_entry_content', 'mry_entry_knowledgebase');
function mry_entry_knowledgebase(){
?>
<div class="articleImage">
<?php
if(has_post_thumbnail()) {
the_post_thumbnail();
}
else {
echo mry_add_default_image("Thumb");
}
?>
</div>
<div class="articleExcerpt">
<div class="titleArea"><a href="<?php the_permalink(); ?>"><span class="title"><h1><?php the_title(); ?></h1></span></a></div>
<div class="date"><time datetime="<?php echo get_the_date('c'); ?>"><?php echo get_the_date();?></time></div>
<span class="excerpt"><a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a></span>
</div>
<?php
}
我正在尝试为包含来自特定分类法的所有帖子的页面创建 Genesis 分页。我会使用此 WordPress Stack Exchange 中提供的许多解决方案,但我不知道如何将他们的 wp_query
与我的相结合。我该怎么做呢? (请记住,我绝不是专业人士。)
这是我在尝试集成分页之前模板中的内容:
remove_action('genesis_loop','genesis_do_loop');
add_action('genesis_loop','get_article_content');
function get_article_content(){
$myterms = get_terms('article-category', 'orderby=none&hide_empty');
foreach ($myterms as $term) :
$args = array(
'post_type' => 'solar-articles',
'tax_query' => array(
array(
$term->slug
)
),
);
// assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);
endforeach;
// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();
?><div class="col-md-12"><?php
the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' );
?><p class="entry-time">POSTED <?php the_time('F j, Y'); ?></p>
<div class="entry-content"><?php
the_excerpt();
?></div></div><?php
endwhile;
}
genesis();
这是我尝试集成后的结果:
remove_action('genesis_loop','genesis_do_loop');
add_action('genesis_loop','get_article_content');
function get_article_content(){
$paged = 1;
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
if ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
$paged = intval( $paged );
$myterms = get_terms('article-category', 'orderby=none&hide_empty');
foreach ($myterms as $term) :
$args = array(
'posts_per_page' => 3,
'post_type' => 'solar-articles',
'tax_query' => array(
array(
$term->slug
)
),
'paged' => $paged
);
// assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);
endforeach;
// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();
?><div class="col-md-12"><?php
the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' );
?><p class="entry-time">POSTED <?php the_time('F j, Y'); ?></p>
<div class="entry-content"><?php
the_excerpt();
?></div></div><?php
endwhile;
genesis_posts_nav();
}
genesis();
您的 foreach 循环似乎在执行任何操作之前覆盖了 $wp_query 变量。如果你的分类法是一个类别,我创建了一个页面,只显示一个特定的类别并分页。我把它放在 page-whatever.php:
$myCategory = get_cat_ID('Knowledgebase');
remove_action('genesis_loop','genesis_do_loop');
add_action('genesis_loop', 'mry_events_do_loop');
function mry_events_do_loop() {
global $mry_knowledgebaseCatId;
$paged = (get_query_var('paged'))? get_query_var('paged'): 1;
$args = array('cat' => $mry_knowledgebaseCatId, 'posts_per_page' => 5, 'post_type' => 'post', 'paged' => $paged);
genesis_custom_loop($args);
}
这是我如何自定义条目的外观
add_action('genesis_entry_content', 'mry_entry_knowledgebase');
function mry_entry_knowledgebase(){
?>
<div class="articleImage">
<?php
if(has_post_thumbnail()) {
the_post_thumbnail();
}
else {
echo mry_add_default_image("Thumb");
}
?>
</div>
<div class="articleExcerpt">
<div class="titleArea"><a href="<?php the_permalink(); ?>"><span class="title"><h1><?php the_title(); ?></h1></span></a></div>
<div class="date"><time datetime="<?php echo get_the_date('c'); ?>"><?php echo get_the_date();?></time></div>
<span class="excerpt"><a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a></span>
</div>
<?php
}