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, i.e. 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.
Related
i would like to add a comment form and display the latest comment for each blog post on the main blog loop so i have used this code and added it in the main blog loop after the content.
global $withcomments;
$withcomments = 1;
comments_template( '', true );
but it displays only on the first blog post and not on the other ones.
I would like the form to be displayed on all the blog posts in the loop and only display the latest comment.
To be more exact as seen in the screenshot below, on the first post the comment textarea appears with the button and on the second post the textarea does not appear with the buttons despite the fact that the code was added in the loop.
Comment form not showing properly - screenshot
You want to use the get_comments() function.
In get_comments, you can set order parameter as 'desc'. so you get result in descending order.
<?php foreach (get_comments() as $comment): ?>
<!--Your Contents-->
<?php endforeach; ?>
I'm developing a wordpress theme where I have a set of custom pages and each page has it's own content! For an example, say there is a custom page where the content is already done using HTML but I need to be able to change the content of a single paragraph using the wordpress admin panel.
How do i do this using the wordpress framework? is there any special way of adding custom editable fields to specific content locations in a page?
Just add meta box and get the values from post meta to show in your html
you can generate metabox from this site without any programing skills
https://jeremyhixon.com/tool/wordpress-meta-box-generator/
and get meta field value with the below code
get_post_meta(get_the_ID(), 'meta_key', true);
Use Wordpress loop to do this:
<div class="content">
<p>Your static content goes here</p>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<p>Or here</p>
</div>
This part between if(have_posts()) and endif; will be generated from Wordpress admin panel so you can easily change it with page WYSIWYG editor without any additional plugins.
Don't forget to create your custom pages as page templates and assign to page in admin panel otherwise it won't work as expected.
Use the following code:
<?php
$id=2;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
Here, $id=2 is your page id which is you get from WordPress Admin panel.
I have a custom page template where I also want to display a list of posts.
Normally in a page you can just use the following code to display content
<?php while(have_posts()): the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
But what if before I display the page content, I have a custom query to display a list of posts? How can I after displaying those posts, go back to the original query to display page information?
After your custom query, call wp_reset_postdata() function to get back default wordpress loop.
<?php wp_reset_postdata(); ?>
For more info and example check at http://codex.wordpress.org/Function_Reference/wp_reset_postdata
I have been using the WordPress More tag on a custom page template. The content before the more tag will display fine but when i click the read more link the page re-loads but no content after the tag is displayed.
Here is the custom page code for i was told would fix the issue of the tag not displaying but now i am getting the new issue i just described. The previous issue i had was that the more tag would not display so i added the code below which now displays it but the content after the tag will not display no matter that i do
<?php global $more; $more = 0; ?>
<?php the_content('Continue Reading'); ?>
If there is a plugin or another filter altering the_content() this can happen.
Try something like this
apply_filters('the_content',get_the_content( 'Continue Reading' ));
More info here
http://codex.wordpress.org/Function_Reference/get_the_content
I have two different post categories ("Local" & "International") in WordPress and I have displayed the posts in my static page. Right now both categories post are coming on one page. Can I show only local posts in local.php and International posts in International.php page? Also another change please. Can I show newer posts at the top? Right now older post is at the top.
Following is my code.
<?php
require('wp-blog-header.php');
?>
<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : start_wp(); ?>
<?php echo "<h1>";the_date();echo "</h1>"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php
endforeach;
?>
Assuming custom page as a page in yours custom plugin. you will need to load wp-load.php to use the WordPress function/loops to fetch the information.
you will need
require_once("../../../wp-load.php"); // ../../../ according to you file location
// now you can loop using get_posts
$posts = get_posts('numberposts=10&category=CATEGORY_ID&order=DESC&');
// loop
If this is not in WordPress, you have to connect to MySQL and the fetch the information from tables with you SELECT query.
Use
$posts = get_posts('numberposts=10&order=DESC&category=[categoryID]&orderby=post_title');
in local.php respectively International.php. Notice that order=DESC instead of order=ASC, this reverses the post order. And be sure to use the appropriate categoryIDs. You find them in your Wordpress administration area.