• Resolved ourcore

    (@ourcore)


    Hey, guys,

    I recently finished building my first custom theme, so I’m still new to some aspects. My website starts with a front-page.php (static page), which then can lead to index.php (blog feed).

    I’d like to be able to pull and display the title and excerpt for the most recent post (that’s the easy part) on the static page, but the index.php template is pulling the content from a page within WordPress, so I’m not sure how to interject this. If the content was in the template file, I could see how.

    What would be the best way to add a loop to pull the most recent post in my scenario? Thanks in advance.

Viewing 11 replies - 1 through 11 (of 11 total)
  • You have to create a Custom Front Page Template which has a wp_query loop that calls the latest post.
    To do this, you should actually use a Child Theme.

    Thread Starter ourcore

    (@ourcore)

    Yes, as I mentioned, I’m already using a custom front page template via front-page.php, which is pulling content from a page.

    I could easily edit this template to pull the latest blog at the bottom, but I want to add it in the middle–that’s my problem.

    Richard

    (@richardcoffee)

    Sounds like you need to add a custom query to the page. Take a look at https://codex.www.ads-software.com/Class_Reference/WP_Query, and look at the multiple loops example.

    Thread Starter ourcore

    (@ourcore)

    If I use multiple loops, can I control where the content from the second loop is displayed? My main problem is that I want the latest blog post to be displayed in between the static page content. Thanks!

    Richard

    (@richardcoffee)

    You have complete control over where it shows up. What I usually do is put that second loop in a separate file, along with the HTML to display the content. Then in the static page I will use get_template_part() to load the file. This keeps it all modular, and allows me to use that same loop in other places if I need to.

    Thread Starter ourcore

    (@ourcore)

    The idea makes sense to me, but I’m still not sure how to put it all together.

    Currently, my front-page.php pulls the header, the static page’s content (by page ID), and the footer. All of the static page’s content is in a WordPress page, and since I don’t believe I can insert a loop in the page content, I’m unsure how I would inject a blog post in between the page content. I don’t mean before or after the page content, but literally in the middle of it. Is this where the multiple loop comes in?

    Richard

    (@richardcoffee)

    If multiple posts are being displayed on the the page then you should be able to insert what you want between the posts, something like this:

    if (have_posts()) {
      while (have_posts()) {
        the_post();
        the_content();
        get_template_part('insert_stuff');
      }
    }

    You may want to use a counter to only insert it between the 2nd and 3rd post, for example.

    If only one post is being displayed then you may be able to use the filter the_content to insert something. Only problem there would be to figure where to insert it at.

    add_filter('the_content','insert_my_stuff');
    function insert_my_stuff($content) {
      if (is_home()) { // maybe is_front()
        // locate where you want to insert
        $pos = strpos($content,'insert here maybe');
        if (!$pos===false) {
          // split content at point of insertation
          $before = substr($content,0,$pos);
          $after  = substr($content,$pos+1);
          // get new content
          $new = new_content_loop();
          // add in the new content
          $content = $before . $new . $after;
        }
      }
      return $content;
    }

    This is just a general outline of how you might do something like this. You would need to work with it to arrive at a usable solution.

    Hope this helps. Good luck!

    Richard

    (@richardcoffee)

    Followup: Using the the_content filter will not work if the new loop is using the_content() to display it’s content, due to recursion issues. While solvable, I don’t really recommend trying it that way.

    Thread Starter ourcore

    (@ourcore)

    Hi! I’m revisiting this as I keep putting it off when something else comes up.

    I’m still unsure a multiple loop is the way to go. If it helps, this is my current home page (static page): https://www.marioparra.me. Glancing over the multiple loop code, I’m not sure how it’d add a post excerpt in between the page content, unless I’m missing something.

    The front-page.php template pulls all of this content (“About” section down to “Contact”) from a page by its ID. Basically, I’d like to insert an excerpt of my most-recent post under the “Portfolio” section. If all the content was in the template, I could easily add a loop below said section, but that’s not the case. I also found a plugin that allows me to insert PHP into a page/post, but I don’t want to do this if there’s a better solution without adding any bulk.

    I also thought about dividing the content from the page into multiple pages, allowing me to insert loops in between more easily.

    Any help would be really appreciated!

    Richard

    (@richardcoffee)

    ahhh, then perhaps a shortcode may be the way to go.

    Insert a shortcode into the static page content where you want your post excerpt to appear:

    [mario_post_excerpt]

    Then, in the functions.php file, you can put the code to display what you need in a function like so:

    function mario_post() {
      $html = 'Content to be displayed';
      return $html;
    }
    add_shortcode('mario_post_excerpt','mario_post');

    The function code to retrieve the post excerpt will need to be a custom loop. The link I posted earlier has example code for that.

    Thread Starter ourcore

    (@ourcore)

    Ah, that makes perfect sense. Thanks, Richard! I forgot we could use shortcodes in pages/posts, and didn’t actually know that it was this easy to create custom codes, so this definitely seems like the way to go. Should be able to implement it pretty easily. Thanks again!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Displaying most recent post on static page’ is closed to new replies.