Community Forums › Forums › Archived Forums › General Discussion › Understanding Loop Markup
- This topic has 9 replies, 4 voices, and was last updated 9 years, 6 months ago by Porter.
-
AuthorPosts
-
May 12, 2015 at 8:46 pm #151956PorterParticipant
I'm trying to create a custom loop for a Custom Post Type archive page (it's visually customized quite a bit), but I can't get it to look how I want. Basically, I have this:
remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'anib_articles_loop' ); function anib_articles_loop() { //Custom loop goes here } wp_reset_postdata(); } genesis();
The results, are as seen here:
Original (without custom loop) - anightinburlington.com/articles
Custom Loop - towerofgreed.com/articles/
As you can see, a bunch of markup is missing. What I don't get, is if I'm still using the genesis_loop hook, why isn't the markup there? I simply want the exact same page design (site-inner, wrap, etc), but I want my data to be shown, with my own styling. Simply removing the loop, then adding the loop with your own function doesn't achieve that!
I did some digging in the lib/structure/loops.php, and there's a lot of markup in there, such as:
function genesis_standard_loop() { //* Use old loop hook structure if not supporting HTML5 if ( ! genesis_html5() ) { genesis_legacy_loop(); return; } if ( have_posts() ) : do_action( 'genesis_before_while' ); while ( have_posts() ) : the_post(); do_action( 'genesis_before_entry' ); printf( '<article %s>', genesis_attr( 'entry' ) ); do_action( 'genesis_entry_header' ); do_action( 'genesis_before_entry_content' ); printf( '<div %s>', genesis_attr( 'entry-content' ) ); do_action( 'genesis_entry_content' ); echo '</div>'; do_action( 'genesis_after_entry_content' ); do_action( 'genesis_entry_footer' ); echo '</article>'; do_action( 'genesis_after_entry' ); endwhile; //* end of one post do_action( 'genesis_after_endwhile' ); else : //* if no posts exist do_action( 'genesis_loop_else' ); endif; //* end loop }
The genesis_custom_loop function seems to simply take custom arguments, then pass them into the original loop (I assume to retain the markup), so why would simply using the original genesis_loop hook not use it as well? Ideally I'd both get a solution, AND understand what's going on here, as it'll help me avoid issues in the future 🙂
May 13, 2015 at 12:28 am #151965upthinkParticipantYou have actually found it correct - the markup is indeed in the genesis_do_loop function. So unless you also include it in your custom loop function then you are not going to have it.
What I usually do is this - create a copy of the genesis_do_loop function (but obviously call it by another name). Then change the parameters etc as I want and hook that function with genesis_loop
May 13, 2015 at 7:55 am #151993PorterParticipantAh yes, I got confused on hook VS function again.
genesis_loop - Hook
genesis_do_loop - Function.So I'm simply telling my function where to be, not actually calling the function containing everything I'm missing. I'll play with getting this to work today, and if I have any questions I'll check back. Thanks for setting me on the right path!
May 13, 2015 at 6:10 pm #152103PorterParticipantSo it seems I'm still a bit lost on what to do here. I get that the markup is in the genesis_do_loop function, but I'm not sure what part. Is it the printf, the do_action, or both? I'm not sure which parts actually contain the main wrap, structures, etc. That aside, I'm not sure how to extract that in a meaningful way so that I have a reusable function for any general custom loop, or perhaps I'm not fully understanding something.
Here's what I have now:
remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'anib_articles_loop' ); function anib_articles_loop() { $type = 'articles'; $args = array( 'post_type' => $type, 'post_status' => 'publish', 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page'=> '100', ); $my_query = null; $my_query = new WP_Query($args); if($my_query->have_posts()) { // loop through Articles while( $my_query->have_posts() ): $my_query->the_post(); ?><div class="articles-columns-container"><?php echo(the_post_thumbnail('thumbnail')); ?><div class="articles-columns-text"> <h3> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </h3><?php echo(get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true)); ?></div><?php ?></div><?php endwhile; } wp_reset_postdata(); } genesis();
How do I go about isolating the markup in such a way that I can reuse a function for just the markup, but maintain the ability to run a custom loop as I've done above? If that's not possible, what are the key structural points I need to simply get a middle section (entry content-like) that has the wrap, site width (mine is 1200), etc? I don't need the EXACT markup, I'm simply want my loop to be contained within the 1200px area, with a background, etc, the rest is superfluous to my knowledge. Some example code would be great, as I do a lot better looking at something. Thanks for your help!
May 13, 2015 at 7:48 pm #152120brockMemberYou Would this be helpful to you?
http://genesissnippets.com/genesis-custom-loop/
Also, does the Genesis grid loop not work for what you are trying to acheive?
May 14, 2015 at 5:25 am #152146PorterParticipantI'm not sure what the Genesis grid loop does, but it doesn't sound like it's related.
I tried messing with this some more this morning (granted after being awake 22 hours), and I'm perplexed and feel like I understand this less than I thought. I tried to simply add the breadcrumbs in my custom function above by adding "genesis_do_braedcrumbs();", and it wouldn't work. I have another page template where I simply call that function, and they appear. I ensured I was removing them in the same way (from the original position), then adding them, but it just doesn't show. Why would it show the breadcrumbs on one template file, but not the other, when they're identical in structure?
All I really want is to wrap the content of my loop in a <div class="entry"> (which I can do manually), and add the breadcrumbs above that. I'm not sure what extra content is generally added, but I don't need it, or at least it doesn't seem I do (See the original design, it's fine without it).
So in closing, can I simply call genesis_do_breadcrumbs anywhere? If so, why isn't it showing, when an echo directly below it does?
May 14, 2015 at 5:54 am #152163PorterParticipantHmm, it seems to be something related to Yoast SEO / Genesis.
To be clear, I can't get the breadcrumbs to show on any archive pages, custom template or otherwise. On a custom post type, even with a custom template, I can call "genesis_do_breadcrumbs" and they show up, on an archive custom template, calling that function does nothing. If I add:
if ( function_exists('yoast_breadcrumb') ) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); }
They show up where I add that, but the function 'genesis_do_breadcrumbs' still does nothing. Why does that function work on a custom post type single, but not on a custom archive?
Something in here is failing to include archives:
unction genesis_do_breadcrumbs() { if ( ( ( 'posts' === get_option( 'show_on_front' ) && is_home() ) && ! genesis_get_option( 'breadcrumb_home' ) ) || ( ( 'page' === get_option( 'show_on_front' ) && is_front_page() ) && ! genesis_get_option( 'breadcrumb_front_page' ) ) || ( ( 'page' === get_option( 'show_on_front' ) && is_home() ) && ! genesis_get_option( 'breadcrumb_posts_page' ) ) || ( is_single() && ! genesis_get_option( 'breadcrumb_single' ) ) || ( is_page() && ! genesis_get_option( 'breadcrumb_page' ) ) || ( ( is_archive() || is_search() ) && ! genesis_get_option( 'breadcrumb_archive' ) ) || ( is_404() && ! genesis_get_option( 'breadcrumb_404' ) ) || ( is_attachment() && ! genesis_get_option( 'breadcrumb_attachment' ) ) ) return; if ( function_exists( 'bcn_display' ) ) { echo '<div class="breadcrumb" itemprop="breadcrumb">'; bcn_display(); echo '</div>'; } elseif ( function_exists( 'breadcrumbs' ) ) { breadcrumbs(); } elseif ( function_exists( 'crumbs' ) ) { crumbs(); } elseif ( class_exists( 'WPSEO_Breadcrumbs' ) && genesis_get_option( 'breadcrumbs-enable', 'wpseo_internallinks' ) ) { yoast_breadcrumb( '<div class="breadcrumb" itemprop="breadcrumb">', '</div>' ); } elseif( function_exists( 'yoast_breadcrumb' ) && ! class_exists( 'WPSEO_Breadcrumbs' ) ) { yoast_breadcrumb( '<div class="breadcrumb" itemprop="breadcrumb">', '</div>' ); } else { genesis_breadcrumb(); } }
I'm too tired to read that right now (going to sleep), but if someone could explain this to me I'd be grateful.
May 14, 2015 at 1:47 pm #152242May 14, 2015 at 1:49 pm #152243PorterParticipantHow did you solve it? As for the query, are you referring to how genesis_do_breadcrumbs isn't working, or are you referring to the loop structure?
May 14, 2015 at 2:11 pm #152249PorterParticipantI figured it out.
There were specific settings in the Genesis settings where you had to enable or disable the breadcrumbs for various pages, and archives wasn't checked. I hadn't visited the Genesis page in so long (almost a year) that I had completely forgotten those settings existed.
As for the loop structure bit, I still don't entirely understand where the markup is coming from, how to isolate it so you can repeat just the wrap for a page, etc, but perhaps my route is how you should. I simply did this:
add_action( 'genesis_loop', 'anib_articles_loop', 9); function anib_articles_loop() { ?><div class = "entry"><?php genesis_do_breadcrumbs(); $type = 'articles'; $args = array( 'post_type' => $type, 'post_status' => 'publish', 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page'=> '100', ); $my_query = null; $my_query = new WP_Query($args); if($my_query->have_posts()) { // loop through Articles while( $my_query->have_posts() ): $my_query->the_post(); ?><div class="articles-columns-container"><?php echo(the_post_thumbnail('thumbnail')); ?><div class="articles-columns-text"> <h3> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </h3><?php echo(get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true)); ?></div><?php ?></div><?php endwhile; } wp_reset_postdata(); ?></div><?php } genesis();
As you can see, I just manually wrapped my section in an "entry" div, which seems to be the only div I need. I then manually inserted the genesis_do_breadcrumbs call where I wanted it, as I had no need to use a hook since I was in the template already, and I wasn't using the loop, which meant no hooks would work as I'm not including that structure. Ideally, I would include that structure, so that if I wanted to position the breadcrumbs with a hook on that page, I could, as is, I have to call the function manually.
I've achieved the design I wanted, and everything works, I'm just trying to keep this best practice. I'm leaving this thread open for a day or two to see if anyone can answer my actual questions, as that's still important to me.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.