Last active
May 26, 2016 12:40
-
-
Save yratof/2a70cd4eb1e7e43ff14fcbcfac37eb78 to your computer and use it in GitHub Desktop.
Wordpress transients
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="nav__articles"> | |
<?php | |
// If the transient doesn't exist, do the query | |
$links = ''; | |
if ( FALSE === ( $links = get_transient( '_menu_article_display_list' ) ) ) : | |
$args = array( | |
'order' => 'DESC', | |
'post_type' => 'post', | |
'show_posts' => 10, | |
'fields' => 'ids', | |
); | |
$ids = get_posts( $args ); | |
if ( $ids ) { | |
foreach ( $ids as $id ) { | |
$links .= '<li><span class="nav__article--date">' . get_the_time( 'd F', $id ) . '</span><strong class="nav__article--name">' . get_the_title( $id ) . '</strong><a class="nav__article--more" href="' . get_permalink( $id ) .'">' . __( 'Read article', 'drivdigital' ) . '</a></li>'; | |
} | |
} | |
set_transient( '_menu_article_display_list' , $links, 12 * HOUR_IN_SECONDS ); | |
endif; | |
echo '<ul class="nav__articles--list">' . $links . '</ul>'; | |
?> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// I love setting queries in 'set_transient' and 'get_transient'. | |
// It basically saves the results from the query in the database | |
// so it doesn't have to do the query on every page load. It does it | |
// once, then just shows the cached results. | |
$links = ''; | |
$id = get_the_ID(); // Might need get_page_object_id(); | |
if ( FALSE === ( $links = get_transient( '_child_menu_transient_' . $id ) ) ) : | |
$args = array( | |
'child_of' => $id, // get all children of this post | |
'parent' => $id, // Make sure the parent is this post | |
'fields' => 'ids', // Only get IDs from this query. We don't need titles or meta or anything else | |
'post_type' => 'page', // only show pages, not posts | |
'posts_per_page' => -1, // show all results | |
'post_status' => 'publish', // only published results, not private | |
'orderby' => 'menu_order', // however they are in the admin is how they list here. | |
'order' => 'ASC' // Like jesus. | |
); | |
$children = get_pages( $args ); // get_pages based on this parent and children | |
if( $children ){ | |
$has_children = TRUE; | |
foreach( $children as $child ) { | |
$links .= '<li class="lined-grey"><a href="' . get_permalink( $child ) . '" class="p pud text-grey"><span class="arrow-right arrow-red"></span>' . get_the_title( $child ) . '</a></li>'; | |
} | |
} | |
set_transient( '_child_menu_transient_' . $id, $links, 12 * HOUR_IN_SECONDS ); | |
endif; | |
echo '<ul class="menu-vertical">' . $links . '</ul>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment