简体   繁体   中英

Wordpress display page contents and a loop?

I have a custom loop in a page template to display posts by category by give category and tag. It works, but above the loop I need to show the content of the page itself, ie the content that's been entered into the Wordpress Dashboard normally.

What do I add to my template to display this content?

I have tried:

 $id = $post->ID;
 get_page($id);
 // then my custom loop

Which does get the current page ID, but no content.

In WordPress, calling

<?php the_content(); ?>

will pull in the content from the WYSIWYG editor on the page that is using that template as long as it is inside the loop.

The most basic example of this might look like this:

<?php while (have_posts()) : the_post();/* Start loop */ ?>
        <?php the_content(); ?>
<?php endwhile; /* End loop */ ?>

<!-- Enter your custom loop for displaying posts by category down here -->

More info here: http://codex.wordpress.org/Function_Reference/the_content

In my case, with a modern theme and using Gutenberg blocks, I had to apply filters to the content before the posts loop, as mentioned in this thread here: Proper way to get page content

Following one of the examples, a simple working solution would be:

<?php
    $id = 669; // page ID that has been assigned for posts
    $post = get_post($id);
    $content = apply_filters('the_content', $post->post_content);
    echo $content;
?>

<!-- Enter your custom loop for displaying posts by category down here -->

To display the content of the page, using the the_content() function.

Added your custom loop after this function call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM