Skip to content

Instantly share code, notes, and snippets.

@MichaelVanDenBerg
Last active April 14, 2016 19:30
Show Gist options
  • Save MichaelVanDenBerg/2cdfb4bc3149448ba5db2bb96199545b to your computer and use it in GitHub Desktop.
Save MichaelVanDenBerg/2cdfb4bc3149448ba5db2bb96199545b to your computer and use it in GitHub Desktop.
Get the url of the portfolio page if the number of items to be shown is lower than the total number of items.
if ( ! function_exists( 'genius_portfolio_url' ) ) :
/**
* Echo a read more url for the portfolio page if the total posts is higher dan the displayed posts.
*/
function genius_portfolio_url() {
// Return if the post limit is more than total posts.
$post_limit = get_theme_mod( 'genius_portfolio_limit', 9 );
$total_posts = wp_count_posts( 'jetpack-portfolio' )->publish;
if ( $post_limit > $total_posts ) {
return;
}
// Get the portfolio page url.
$args = [
'post_type' => 'page',
'fields' => 'ids',
'nopaging' => true,
'meta_key' => '_wp_page_template',
'meta_value' => 'template-parts/template-portfolio.php'
];
$pages = get_posts( $args );
foreach ( $pages as $page ) {
$portfolio_id = $page;
}
$portfolio_url = get_page_link( $portfolio_id );
// Get the read more text.
$read_more = get_theme_mod( 'genius_portfolio_read_more', __( 'Discover more' ) );
echo '<a href="' . $portfolio_url . '">' . $read_more . '</a>';
}
endif; // genius_portfolio_url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment