我是Wordpress的新手,只是想知道我是否可以检索8个最新的投资组合?
由于我的作品集太多,因此页面加载非常慢,因此我只想显示前8个。
答案 0 :(得分:0)
要列出投资组合,请在当前活动主题 functions.php 文件中添加以下代码,并使用简码 [portfolio-grid-custom posts_per_page =“ 8”] 。
function create_grid_layout_portfolio($attr){
$posts_per_page = $attr['posts_per_page'];
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'portfolio',
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => $posts_per_page,
'paged' => $paged
);
//[portfolio-grid-custom posts_per_page="4"]
$query = new WP_Query( $args );
?>
<!-- the loop -->
<?php
if ( $query->have_posts() ) {?>
<ul class="custom-col-<?php echo $num_of_col;?>" id="loop_portfolio_custom" >
<?php while ($query->have_posts()) { $query->the_post();
global $post;
$post_id = $post->ID;
$featured_img_url = get_the_post_thumbnail_url($post_id,'full');
$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'names');
$product_terms = wp_get_object_terms( $post_id, 'portfolio-types',$args );
$catnames = implode(", ",$product_terms);
?>
<li>
<div class="inner_portfolio" style="background-image: url(<?php echo $featured_img_url;?>);background-size: cover;background-position: center center;background-repeat: no-repeat;">
<div class="inner_hover" onclick='window.location.href="<?php the_permalink(); ?>"'>
<div class="hover_show" style="display:none;">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span><?php echo $catnames;?></span>
</div>
</div>
</div>
</li>
<?php } ?>
</ul>
<!-- pagination -->
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( '« Previous', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next »', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<style>
ul#loop_portfolio_custom {
margin-left: 0px;
margin-top: 80px;
}
ul#loop_portfolio_custom li {
display: inline-block;
width: 33%;
}
.inner_portfolio {
width: 95%;
margin: 0 auto;
height: 240px;
margin-top: 15px;
position: relative;
}
.inner_hover:hover {
opacity: .7;
background: #000;
}
.inner_hover {
width: 100%;
height: 100%;
text-align:center;
}
.hover_show a, .hover_show span {
display: inherit;
text-transform: capitalize;
}
.hover_show a{
font-weight: normal;
font-size: 39px;
line-height: 42px;
text-decoration: none;
color: white;
}
.hover_show span {
font-size: 18px;
line-height: 27px;
color: white;
text-transform: uppercase;
font-family: 'Open Sans';
font-style: normal;
font-weight: 300;
}
.hover_show {
position: relative;
top: 35%;
text-align: center;
z-index: 9999999;
}
.pagination a.next.page-numbers, .pagination a.prev.page-numbers {
text-decoration: none;
color: #6e0d16;
opacity: .7;
}
a.page-numbers{
color: #6e0d16;
opacity: .7;
}
span.page-numbers.current {
color: #0000008f;
}
.pagination {
text-align: center;
padding-bottom: 30px;
}
.pagination span, .pagination a {
margin: 0 3px;
}
</style>
<?php
}
}
add_shortcode('portfolio-grid-custom','create_grid_layout_portfolio');
或者,如果您只想限制帖子获取,则添加过滤器挂钩,如下所述:
function wpcodex_filter_main_search_post_limits( $limit, $query ) {
if ( ! is_admin() && $query->is_main_query() && is_page('YOUR_PAGE_ID')) {
return 'LIMIT 0, 8';
}
return $limit;
}
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );
答案 1 :(得分:0)
使用您的functions.php
// Custom Post Type
function custom_post_type_portfolio() {
$labels = array(
'name' => _x( 'Portfolio', 'Post Type General Name', 'idealthinker' ),
'singular_name' => _x( 'portfolio', 'Post Type Singular Name', 'idealthinker' ),
'menu_name' => __( 'Portfolio', 'idealthinker' ),
'parent_item_colon' => __( 'Parent Portfolio', 'idealthinker' ),
'all_items' => __( 'All Portfolio', 'idealthinker' ),
'view_item' => __( 'View Portfolio', 'idealthinker' ),
'add_new_item' => __( 'Portfolio Details', 'idealthinker' ),
'add_new' => __( 'Add New', 'idealthinker' ),
'edit_item' => __( 'Edit Portfolio', 'idealthinker' ),
'update_item' => __( 'Update Portfolio', 'idealthinker' ),
'search_items' => __( 'Search Portfolio', 'idealthinker' ),
'not_found' => __( 'Not Found', 'idealthinker' ),
'not_found_in_trash' => __( 'Not found in Trash', 'idealthinker' ),
);
$args = array(
'label' => __( 'portfolio', 'idealthinker' ),
'description' => __( 'Portfolio news and reviews', 'idealthinker' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'author', 'thumbnail'),
'taxonomies' => array( 'genres'),
'menu_icon' => 'dashicons-admin-generic',
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'portfolio', $args );
}
add_action( 'init', 'custom_post_type_Portfolio', 0 );
// custom_pagination
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav aria-label='Page navigation'>";
//echo "<div class='left'>Page " . $paged . " of " . $numpages . "</div> ";
echo "<div class='pagination'>" . $paginate_links . "</div> ";
echo "</nav>";
}
}
添加您的投资组合页面(例如page-our-portfolio.php)
<?php if ( have_posts() ) : the_post(); ?>
<div class="container">
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$my_query_args = array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 8,
'paged' => $paged
);
$my_query = new WP_Query( $my_query_args ); ?>
<div class="row portfolio">
<?php while ( $my_query->have_posts() ) : $my_query->the_post();
$images_alt_tag = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
?>
<div class="img-box">
<a href="<?php echo wp_get_attachment_url( get_post_thumbnail_id($page->ID)) ?>">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($page->ID)) ?>" alt="<?php echo $images_alt_tag; ?>">
</a>
<div class="title">
<p><?php the_title(); ?></p>
</div>
</div>
<?php endwhile; ?>
</div>
<!-- for pagination -->
<div class="row">
<div class="col-lg-12">
<?php if (function_exists('custom_pagination')) { ?>
<?php custom_pagination($my_query->max_num_pages, "", $paged); ?>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
</div>
<?php endif; ?>