get post content into header - php

i have to get the post content into the tag <head>.
I was trying with this code into the header.php file of my theme:
if(is_single()){
$stringa = the_content();
}
but it doesn't work.
how can i do?
thanks

The functions the_content() and get_the_content() are meant to be used inside the WordPress loop, which means you can't just use them at will. You'll need to build a loop inside your header.php file that queries the WordPress database, fetches some content, and uses it as necessary.
Basically, wrap your the_content() call inside:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; endif; ?>
Then you'll be able to fetch post content anywhere on the page ... however, I don't quite understand why you're trying to get the post content inside the <head> section of the page. <head> is used for style declarations, <script> tags, and meta information about the page ... not for actual page content. If you're trying to get specific information about the current page, I'd recommend using a different function entirely.

I think what you are looking for is:
$stringa = get_the_content();

if (is_single())
{
the_post();
$content = get_the_content();
rewind_posts();
}
It's important to put rewind_posts(), otherwise post loop will not work in other templates.

Related

How to display the content of a blog post using a custom post template?

I am using my own custom WordPress theme and I am running into trouble displaying the content of blog posts. I can display the title and the date it was published using php but I can't get any of the paragraphs, images, headings, etc. to display on the page. I am using Gutenberg blocks (default) for the content of the blog posts.
I have tried using php functions to grab the content but they don't seem to be working.
<div class="col-md-6 col-md-offset-3">
<p class="date"><span class="glyphicon glyphicon-time">
</span> <?php echo get_the_date();?></p><br />
<p><?php $content = apply_filters('the_content', $post-
>post_content);?></p>
</div>
I am expecting the content of the post to display within the div container but the function is not grabbing the content. Any help would be appreciated!
It sounds like you may be trying to retrieve the post content from outside the loop.
If you look at the post template for a theme e.g. 2017, it is this bit that does the magic. It’s not even necessary to pass a post ID:
<?php
while ( have_posts() ) : the_post();
get_template_part( 'components/page/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
E.g. you should just be able to do:
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile; // End of the loop.
?>
Might be a good idea to start with the code on the link above, or copy the single.php file for the theme you’re using and use that as the basis for your custom post page?

Wordpress single.php another get_post function destory the first function

I am trying to deal with two get_post functions, in single.php the first get_post function is the post from the wordpress, But after that I called get_post function to other post also to use both of them in the same page but after I call the first get_post ( the main post ) I get the only second data and cant reach the first data.
My code called to the second function ( The first is from wordpress post):
$main_post = get_field('main_post');
$main_p = get_post($main_post->ID);
Then I am trying to use the variable $post OR the_title() OR any other functions to get the first post and it always returning the info of the $main_p post
for example
get_the_title( get_post()->ID )
returns the $main_p post id and not the main post of the single.php
any soulutions ?
I may be wrong, but it seems to me that you are trying to post a different post format with normal post format?
I, myself use get_post_format() so it can be styled differently or have different options.
in single.php I use
<!-- checking if there are any blogposts to be shown using have_posts check which is a wordpress function-->
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?> <!-- the correct syntax for while loop in wordpress to show all the blogposts -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : //else stament if there aren't any posts (put inside if before endif)?>
<p><?php __('No Posts Found'); ?></p>
<?php endif; ?> <!-- stop checking for blog posts-->
</div><!-- /.blog-main -->
Inside functions.php I activated the post-formats inside wp_theme_setup() function
add_theme_support('post-formats', array('aside', 'gallery'));
In this case i activated the gallery and aside posts
That way I have 2 different post types on one page
like this
image from my theme blog page
Here is also a video tutorial on post formats from Traversy media
https://www.youtube.com/watch?v=CRa7eiqyiCM&list=PLc5p9nvpdwBlrNU0hr1f0kXPRkh0aGo1Q&index=7
The key reason why your post values are being overwritten, the additional get_post() declarations are overriding the default query. Now, the code in your pastebin is a pretty massive dog's breakfast, so a direct solution is a rather large undertaking (e.g. the indentation is all over the place, the code snippets are less than ideal regarding their readability, etc...). However, I can point you in the right direction for the solution.
When I pull content from another page on my WordPress sites, I avoid using get_post() in favour of declaring a fresh new WP_Query() (that's just my preference), following it up with a wp_reset_postdata() declaration.
Here's an example of multiple queries on a single template in the WordPress codex:
https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
The key here is the wp_reset_postdata(). I'd recommend looking into it's purpose. It'll save you a lot of grief:
https://codex.wordpress.org/Function_Reference/wp_reset_postdata

Custom post_type template with get_template_part

So, my parent-theme uses a single.php calling a content-single.php with the function get_template_part().
The code for single.php:
get_header(); ?>
<div class="container">
<main id="main" class="postItem" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php codex_coder_content_nav( 'nav-below' ); ?>
<?php endwhile; // end of the loop. ?>
</main><!-- #main -->
<!--<?php get_sidebar(); ?>
I am using a custom post_type named "ourNews". Following wordpress documents, I created two files: single-ourNews.php and content-single-ourNews.php
With this, in my "single-ourNews.php" I changed the following line:
<?php get_template_part( 'content', 'single-ourNews' ); ?>
But it keeps loading the file "content-single.php". What am I doing wrong?
Other question is: How can I put a image with relative path on this custom template? I created a folder "img" and I was calling using:
But says that the image was not found. I read a little and some places said that I could not use relative path, but why not, if the theme uses? I'm confused.
Wordpress is still loading single.php that loads content-single.php
Your custom post is not ourNews. This is probably the label but not the slug. The slug is always lowercase and separated by a -. Try renaming your file to single-our-news.php.
Also, every time you register a custom post type, you should reset your permalinks options back to default and then back to what you want it to be.
For the relative path, you should not use it. You should use .'/img/name-of-image.jpg
When using get_template_part, the first parameter defines the base template name (e.g. content, which loads content.php) and the second parameter loads a suffixed version if available. Therefore your call
get_template_part('content', 'single-ourNews');
is looking for a file named content-single-ourNews.php first, then falls back to content.php in case the first one is not available. I'm not sure whether the get_template_part function converts the suffix parameter to something like single-ournews or single-our-news before appending it to the first parameter, be sure to test a few variants of that.
I'm not 100% sure if the function behaves differently or not in child themes and parent themes. One option is to override the parent's single.php in the child theme and modify it directly with
if ($post->post-type === 'ourNews') {
get_template_part('content', 'single-ourNews');
}
else {
get_template_part('content');
}
Lastly, WordPress will look for a file single-[cptslug].php before loading a template for a custom post type's single view.

Display posts on tag.php template?

Is it possible to create a tag.php template that when you navigate to the url www.domain.com/tag/architecture/ it will display all the custom posts that have been tagged with that specific tag? And so on for various other tags?
What code will I need to include on my template?
Yes You can create,below is the code i used to display custom post type "knowledge"
<?php
global $query_string;
$posts = query_posts($query_string.'&post_type=knowledge');
if ( have_posts() ) while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php endwhile; // end of the loop. ?>
This will help you understand hierarchy of templates:
http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
Use of $query_string (example) available here:
https://developer.wordpress.org/reference/functions/query_posts/
According to the template hierarchy, you can create a file called tag.php which will be used instead of index.php if a tag page is displayed. You can also prepare separate templates for specific tags.
The best way is to start by creating a copy of your theme's index.php and calling it tag.php, this way you'll have some basic code working. Then you can modify tag.php to fit your needs - probably by editing The Loop, or maybe changing some includes if your theme loads the loop from separate file. (but in this case you should consider editing index.php to load some other loop for tag pages - is_tag() may come in handy)

Run php functions on selected Wordpress pages

I am trying to customise a Wordpress theme. I have a function in themes/functions.php that I would like to run on some pages.
I need to be to:
Detect the page ID to determine whether the function should execute
Determine which hook to attach the function to (preferably something like page load.
Cheers
The file functions.php is for theme specific functions called inside your theme. If this is a theme specific function then the function call should be in the header (or wherever you want the output of the function to appear) via <?php my_function() ?>.
Hooks are for plugins, not template specific code.
If you are inside The Loop, then you can call <?php the_ID(); ?> as WarrenB said. If you are outside of the loop, then <?php echo $post->ID?> will print the page ID.
To the questions you posed, given you want to select on id #9, run your loop like this:
<?php
query_posts('page_id=9');
if (have_posts()) : while (have_posts()) : the_post();
// Do whatever on post id #9
?>
<?php endwhile; else: ?>
// Do whatever on all the other posts
<?php endif; ?>
If this isn't the answer you're looking for, please add more information to your question.

Categories