WordPress.org

Ready to get started?Download WordPress

Forums

Customizr
[resolved] Using Static Front Page: add blogroll to bottom? (42 posts)

  1. Ultimate Peter
    Member
    Posted 1 month ago #

    Apologies if this question has already been answered somehow.

    (WordPress: you need to make it easier to search these support posts, specifically for one theme / plugin. I can't sift through these 163 pages of answers)

    Anyway, I have created a static front page for my site here: http://qbservices.net

    Now, the only thing missing is:
    Under the placards I have setup, I would like the blogroll from the built-in front page to show up (you know, the format with listed blog posts and alternating left & right circles).

    Is there a shortcode, or a piece of PHP I can stick in (with php-on plugin) that will make the beautiful blog-roll from the standard front page end up after my custom content on the front page?

    Thanks much!
    Peter

  2. ElectricFeet
    Member
    Posted 1 month ago #

    It's probably easier to do it the other way around. That is, customize your site to show the blog posts on the front page and then insert your content before the posts.

    I was working out ways to do this and at first thought of using the ol' widget approach, but you don't want a whole load of possibly frequently-updated content sitting in a widget.

    You also don't want a whole load of content sitting in php, where it's harder to update, version, preview, etc.

    So I came up with the idea of inserting a page's content before the blog posts instead.

    I presume that for your current front page, you have set Customizr up to show a static page on the front page. So you already have a page of content that is your front page.

    Do the following :

    1. Change your current static page's title to home and set the slug to home;
    2. In Settings > Reading (not in Appearance > Customize *) , set the front page to show your latest posts;
    3. Put the following in your child-theme's functions.php**:
      add_action  ( '__before_loop', 'add_home_page_before_posts');
      function add_home_page_before_posts($path) {
      	if ( is_home() ) {
      		$post = get_page_by_path( '/home' );
      		$content = $post->post_content;
      		echo $content;
      	}
      }
    4. Use a redirect plugin (e.g. Eggplant 301 Redirects -- I've not used it myself, but it gets good ratings and seems well-supported) to permanently redirect your page called "home" to your real home/front page. (That is, redirect from what will then be http://qbservices.net/home to http://qbservices.net/ ) You need to do this because, although it will not be on your menus or anything, search engines and the odd curious user might end up there by mistake otherwise.
    5. Tell me if it works for you.

    * Don't use Appearance > Customize to change this because I think there's a WP bug in there and have opened a ticket. Using it now might confuse things.
    ** You mention using a php plugin, but you really don't need to do this. Read about customizing Customizr and you'll see that it's easy enough to do this yourself without a plugin.

    Polling d4z_c0nf and nikeo to see what they think of this solution, or whether there's an easier approach. (I'm sure there's a better, deluxe approach, which would take time to code, but this is relatively quick and easy.)

  3. d4z_c0nf
    Member
    Posted 1 month ago #

    What about something like this?
    Just add this snippet to your child-theme functions.php:

    add_action('__after_loop', 'my_blog');
    function my_blog(){
        if ( ! ( tc__f('__is_home') && 'page' == get_option('show_on_front') ) )
            return;
    
        query_posts('is_posts_page=true');
        if ( have_posts() && !is_404() ) :
            while ( have_posts() ) :
                the_post();
                do_action ('__before_article');
                ?>
                    <article <?php tc__f('__article_selectors') ?>>
                        <?php do_action( '__loop' ); ?>
                    </article>
                <?php
                do_action ('__after_article'); 
    
            endwhile;
            wp_reset_query();
         endif; ##end if have posts
    }
  4. ElectricFeet
    Member
    Posted 1 month ago #

    Thanks! Glad you couldn't see any holes in the solution I posted ... yet :-)

    Yup, that new code works too. It doesn't give post navigation when you get up to the max posts per page, so it's good if you have only a few posts.

  5. d4z_c0nf
    Member
    Posted 1 month ago #

    Just noticed :D . Yep, is good just if you have few posts or if you don't want the navigation in home :d . Whit this solution you can't rever to the query var "paged". You're right.
    Honestly I haven't digged in your solution, I just assumed it worked, in italian we say "Principio di Autorità" :D

  6. rdellconsulting
    Member
    Posted 1 month ago #

    Better in English, though my wife would disagree ;) (Non comprendi btw)

  7. d4z_c0nf
    Member
    Posted 1 month ago #

    This is the dual solution:
    1) Set the front page to show your latest posts;
    2) Add the following code in your child-theme functions.php:

    add_action('__before_loop', 'my_home_page');
    function my_home_page(){
       if ( ! ( tc__f('__is_home') && 'posts' == get_option('show_on_front') ) )
            return;
        $page_id='7761'; // page id you wanna show on front above the blog.
        query_posts("page_id=${page_id}");
        if ( have_posts() && !is_404() ) :
            while ( have_posts() ) :
                the_post();
                do_action ('__before_article');
                ?>
                    <article <?php tc__f('__article_selectors') ?>>
                        <?php do_action( '__loop' ); ?>
                    </article>
                <?php
                do_action ('__after_article'); 
    
            endwhile;
            wp_reset_query();
         endif; ##end if have posts
    }

    3) Add this to your custom css:

    .home .page .entry-header{
        display: none;
    }

    @rdellconsulting
    Should be Authority Principle in english ..
    <<"If an expert says it, then it must be true," is the basis of the Authority Principle.>>
    Listen to your wife .. she's wise :P

  8. Ultimate Peter
    Member
    Posted 1 month ago #

    Sorry, I could not test this until today, busy weekend.

    Yes! This worked brilliantly!

    You can check out the results: http://qbservices.net

    Now, I have my own custom navigation home, as well as a dynamic blogroll! And... It looks super seamless!

    I can't thank you enough, and I think this code should definitely be shared, because I can imagine it would be quite useful to quite a few people with this theme!

  9. ElectricFeet
    Member
    Posted 1 month ago #

    @Ultimate Peter: could you tell us which code you used?

    d4z_c0nf:

    Honestly I haven't digged in your solution, I just assumed it worked, in italian we say "Principio di Autorità" :D

    Senti il Principe dell'Autorità che parla a me dei Principii di Autorità! :-)))

    .
    .
    .
    (Before the mods complain about non-English, this means: "Look at the prince of authority telling me about authority principles!")

  10. Ultimate Peter
    Member
    Posted 1 month ago #

    Yes, after reading through the code, d4z_c0nf's dual-solution made the most sense to me:

    -If this is the homepage -> show page number 7761.
    -Then list the blog loop.
    -Then play cleanup with CSS and remove the page name on the homepage.

    Worked like a charm!

    Bonus: I still get to easily edit my front page content using the editor

  11. ElectricFeet
    Member
    Posted 1 month ago #

    @Ultimate Peter: Great! Good to know :-)

    @d4z_c0nf: OK, now I need to understand it better :-) I'm confused as to why you do all the __loop stuff in php when you can simply let Customizr do it with a blog on the home page.

  12. d4z_c0nf
    Member
    Posted 1 month ago #

    @ElectricFeet
    I don't get what you mean, I leave Customizr handle the __loop stuff with a blog in the home page, as Ultimate Peter said, I just add another (customizr) __loop to display a page above the blog in home (and just if you decide to display your latest posts on front page).
    There's a better way to achieve that? I'm opened to better solutions :D

    p.s.
    I've chosen this way just 'cause looking at his source page I've noticed that he was using a plug-in to build the page, otherwise I'd have suggested him the evergreen "add content after/before something"

  13. ElectricFeet
    Member
    Posted 1 month ago #

    There's a better way to achieve that? I'm opened to better solutions :D

    That was what I thought my first solution was: Tell Customizr to produce the blog posts and simply insert the required page above them.

  14. d4z_c0nf
    Member
    Posted 1 month ago #

    And isn't what I do? :D

  15. ElectricFeet
    Member
    Posted 1 month ago #

    You have the loop in your code; with a WP query; with action hooks. My solution uses the get_page_by_path() function, which goes straight to the db to get the contents of that page.

    As always, with WordPress, there are many ways to do what you want ... this is in the spirit of the pursuit of the "ideal", as a way of learning.

  16. d4z_c0nf
    Member
    Posted 1 month ago #

    Ahh, sorry didn't get your point :D.
    Well the fact is that I wanted to keep customizr hooks, article selectors filters, and use customizr __loop (with its hooks and functionality). You can also wrap the content in an <article>, and so on.
    Your way is cheaper, I think, so, as usual, the choice will depend on the actual user needs :D

  17. nikeo
    Member
    Theme Author

    Posted 1 month ago #

    Hi @peastvoldqbservicesnet,
    Congrats for your webdesign. I would like to know if you have used a plugin for your "mega menu" or if you did it yourself?
    thanks
    @electricfeet and @d4z_c0nf => very interesting php game there!
    for future refenrences, note that <aq href="http://codex.wordpress.org/Function_Reference/query_posts">uery_posts is not recommended by WordPress...in most cases, pre_get_posts is better

  18. ElectricFeet
    Member
    Posted 1 month ago #

    And in this case, as you're only getting one page, you don't need it anyway.

  19. d4z_c0nf
    Member
    Posted 1 month ago #

    What do you mean with "you don't need it anyway"? And why "as you're only getting one page"?
    I think that for something simple like that, and wanted to re-use customizr code to display a page content (as I've already said, keeping its hooks, article classes etc.) it's not that bad. It's not a big deal in that case. I think.
    About using pre_get_posts ok, I agree, but how could I use it for doing what I wanted? Dunno atm.
    :D

  20. ElectricFeet
    Member
    Posted 1 month ago #

    What do you mean with "you don't need it anyway"? And why "as you're only getting one page"?

    You don't need to load the loop, nor Customizr's hooks. Just go straight to WordPress's own code and pick up the code (see my original reply).

    On the second question, I dunno either :-) I'll let nikeo answer that one :-)

  21. d4z_c0nf
    Member
    Posted 1 month ago #

    Oh I see, I probably expressed myself badly. I didn't need that, in fact I never said I needed it, it's just I wanted it. I didn't need customizr hooks, I wanted them. I wanted to use the wordpress loop, and of course the global wp-query, 'cause I wanted to re-use customizr code to show the page. It's not just about displaying the content, it's about having also article class selectors etc., basically a structure of the displayed page coherent with the rest of the page (list of posts). To be honest I also wanted the title of the page written (and then hidden in css) because of that "google author/update fields issue".
    Of course I saw your original reply, and I tried your code, that's why I said some posts above at least to wrap the content in an <article>, maybe also adding some static class, to make it appear consistent with the rest of the page, or other page's contents (css).
    Also what's the problem with the loop? Computationally basically null. The problem with query_posts, what's this big deal in this case? I use it outside the loop, I reset it before a new loop, It's inefficient as re-runs SQL queries (as stated in the codex), what that $post = get_page_by_path( '/home' ) does if not a new sql query?
    https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/post.php#L3898
    (And why not use get_post then? Just to use the page slug instead of the id?)
    For example, I think your code is useful if I wanted to append the content of a page inside another one (filtering the_content of the "container page"), and it will stay inside the <entry-content> .

    There could be a better solution for what I wanted, without replicating the customizr code to show that content the way I wanted? I don't know, probably, and as I said I'm opened to that :D
    Can your solution satisfy those (mine) requirements? I don't think so. Is it cheaper? I already told you I think that.
    I'm sure interested in a way to achieve my same result without using query_posts to "replace for a while" the main query ('cause that's my aim). Could this be done with the pre_get_posts hook? I used it for different purposes, for example for displaying pages in the list of posts, but this is not the case...

    Sometimes there are different ways to achieve the same result, in this case we proposed different codes to achieve similar result. People can choose what they want, if they want my result, then I think they need that code :P (until someone comes with a better one, of course :D), otherwise they can use your one (or use get_post), and just wrap the content in a selector to style it the way they want :).
    Anyway.. pretty interesting topic :D eheh

  22. ElectricFeet
    Member
    Posted 1 month ago #

    Ah, naaaooooow I get where you're coming from :-))))

    My solution goes for raw minimalist content. Your solution goes for Customizr look and feel. Got it!

    It's often useful to find out why 2 people think they have the greatest solution to a problem, because once you've worked it all through, you end up finding that you actually have 2 great solutions for 2 separate problems.

    Thanks :-)

  23. rdellconsulting
    Member
    Posted 1 month ago #

    Wished I had a great solution..... ;)

  24. acub
    Member
    Posted 1 month ago #

    @d4z_c0nf: In principle, I don't agree with having to edit a page in order to modify another (the include a hidden page solution), but I'm afraid it's the only one that works right. I have this hunch your query switching solution will gracefully fail on pagination.

  25. Ultimate Peter
    Member
    Posted 1 month ago #

    I think D4z's solution is working great on my front page:
    qbservices.net

    ...and the pagination does work.

  26. acub
    Member
    Posted 1 month ago #

    I believe you used his second solution, not the first one. The first one should have problems with posts pagination. That was the "query switching solution".

    That's what query_posts() does. It replaces (switches) the main query.

    And now I just noticed he said it's good only if you don't need pagination. Initially I only looked at the code and I thought he didn't know this.

  27. Ultimate Peter
    Member
    Posted 1 month ago #

    I used the one above, under the heading:

    This is the dual solution:

    My apologies, were you referring to a different one?

    @Nikeo
    Oh, I didn't see you addressing me up above, sorry.

    Hi @peastvoldqbservicesnet,
    Congrats for your webdesign. I would like to know if you have used a plugin for your "mega menu" or if you did it yourself?

    I used the plugin, Mega Main Menu
    However, I will say that it has some issues when the screen gets narrow for mobile (like it pops down a mobile menu from your theme, then you have to click on a mobile menu button from mega main). I currently have it in a make shift state with buttons and I haven't had time to sort out the issues.

  28. d4z_c0nf
    Member
    Posted 1 month ago #

    @acub.
    Yes the first solution was wrong as you and ElectricFeet noticed if you wanted pagination.
    Probably I had to say "this is the dual and working solution" :D

  29. acub
    Member
    Posted 1 month ago #

    Here it is.
    d4z_c0nf's solution, added as a Customizr option.

    Put this in functions.php of your child theme. (And yes, you need to disable the code above, as I have added it in these functions)...

    add_action('after_setup_theme', 'customizr_theme_loaded');
    function customizr_theme_loaded() {
    	if (!function_exists('tc__f'))
    		return;
    	add_action( 'customize_register', 'tc_boh_controller');
    	if (true == tc__f( '__get_option' , 'tc_boh_check') )
    		add_action('__after_loop', 'tc_display_boh');
    
    }
    function tc_display_boh() {
    	if (! tc__f('__is_home') ) return;
    	ob_start();
    	query_posts('is_posts_page=true');
    	if ( have_posts() ) :
    		while ( have_posts() ) :
    			the_post();
    			do_action ('__before_article'); ?>
    			<article <?php tc__f('__article_selectors'); ?>>
    				<?php do_action( '__loop' ); ?>
    			</article>
    		<?php do_action ('__after_article');
    		endwhile;
    	endif;
    	remove_action('__after_loop', 'tc_display_boh');
    	do_action ('__after_loop');
    	wp_reset_query();
    	echo ob_get_clean();
    }
    
    function tc_boh_controller() {
    	global $wp_customize;
    	$wp_customize->add_setting('tc_theme_options[tc_boh_check]', array(
            'default'        => false,
            'capability'     => 'edit_theme_options',
            'type'           => 'option',
        ));
    	$wp_customize->add_control('tc_boh_check', array(
            'settings' => 'tc_theme_options[tc_boh_check]',
            'label'    => __('Display blog on static front page'),
            'section'  => 'tc_frontpage_settings',
            'type'     => 'checkbox'
        ));
    }

    You just check "Display blog on static front page" and... it works. I did not build the js for auto refresh on change/save in order to keep it simple, but whoever wants instant preview, here's how to do it.

    As a bonus, the text above the post list is in the home page, so you can easily change it with "Edit Page" in admin bar. And pagination works, ofcourse ;)

  30. d4z_c0nf
    Member
    Posted 1 month ago #

    Mmm but this takes the "wrong" solution, that one which fails with the pagination. And why use this?

    remove_action('__after_loop', 'tc_display_boh');
    do_action ('__after_loop');

    If you wanted to reset the query __after_loop (after the pagination) you could use
    add_action(__after_loop, 'wp_reset_query', 30), but still, since pagination will not work, displaying it is useless.
    So we cannot talk of "displaying blog on static front page", but something like "displaying latest N posts on static front page" with N = number of posts per page. :D

Reply »

You must log in to post.

About this Theme

About this Topic