how to find Page id inwordpress - php

i need to get page ID in wordpress throught php?

You want to use the_ID() within the loop.

Assuming this is for a Theme, it's as simple as this.

There is a global variable "$post" which contains the related information of the current post / page, and is actually an object. You can access information just as you access variables from an object. Remember to keep it in the while loop.
For example, confider the following:-
<?php if (have_posts()) : ?>
<?php
while (have_posts()):
the_post();
global $post;
$idPagePost = $post->ID;
endwhile;
?>
<?php endif; ?>
Now the variable "$idPagePost" will contain the ID of the current page / post.
Hope it helps.

global $wp_query;
$id = $wp_query->post->ID;
// OR:
$id = $wp_query->queried_object_id;
This will work anywhere in your themes or plugins, as long as it happens after WordPress is loaded.

Related

HTML5Blank Save excerpt into array

I am using HTML5Blank as a starting theme and it comes with this function which returns 40 chars excerpt:
<?php html5wp_excerpt('html5wp_custom_post'); ?>
My main blog page is more complex, so I am using array to store values into it and echo them where I need them:
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $post_titles[$counter] = get_the_title($post->ID); ?>
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post_id); ?>
<?php $post_permalinks[$counter] = get_the_permalink($post->ID); ?>
<?php $post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid')); ?>
<?php $counter++; ?>
<?php endwhile; ?>
All other fields work and I can echo them, but I don't have any idea how to make excerpt work because it isn't echoing anywthing with:
<?php echo $post_excerpts[0]; ?>
First of all I notice, that you are using a variable called $post_id, which is not defined as far as I can see. You need to add a $post_id = $post->ID; before or alternatively you can use instead:
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post->ID); ?>
Maybe this already solves your problem. But to make sure, I will take it further.
I took a look at the functions.php of the HTML5-Blank-Theme: https://github.com/html5blank/html5blank/blob/master/src/functions.php
// Create 40 Word Callback for Custom Post Excerpts, call using html5wp_excerpt('html5wp_custom_post');
function html5wp_custom_post($length)
{
return 40;
}
So the function only returns the value of 40. I guess you can simply use the html5wp_excerpt() like this:
html5wp_excerpt(40);
Maybe there is something wrong with the html5wp_custom_post, so you can get rid of it to test this. And I also think, why use an addtional function, if it only returns a number... you can easily set it in the function call.
I don't know if this functions accepts an post ID as a parameter. So maybe it can only be used inside of a single.php page. I can't find a documentation about this, maybe you can do some research and find it out.
Here is another way to achieve it:
You can use the get_the_title() function that will accept the post id. Unfortunately, the get_the_excerpt() does not accept it.
So we first need to get the post object and then apply a filter to get the excerpt of the post. Do this by putting this code inside your while loop:
<?php $current_post = get_post($post->ID); ?>
You now have the current post as an object. In the next line, we apply the filter and save the result to the array at the right index position:
<?php $post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt); ?>
I wonder why there are so many php opening and closing tags, so you could make your code more readable:
<?php while ($the_query->have_posts()) : $the_query->the_post();
$current_post = get_post($post->ID);
$post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_titles[$counter] = get_the_title($post->ID);
$post_permalinks[$counter] = get_the_permalink($post->ID);
$post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid'));
$counter++;
endwhile; ?>
Just as an additional info, you could also use only the filters, should work with your post object:
$post_titles[$counter] = apply_filters('the_title',$current_post->post_title);
EDIT:
You can trim your excerpt to a certain length with mb_strimwidth (read more: https://www.php.net/manual/en/function.mb-strimwidth.php) :
$current_post = get_post($post->ID);
$trim_excerpt = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_excerpts[$counter] = mb_strimwidth($trim_excerpt, 0, 40, '...');
EDIT 2:
Maybe you should check if you are getting your post object. The fact that you are always seeing the same excerpt, maybe means you get the excerpt of the current page (not the post in your query).
$current_id = get_the_id();
$current_post = get_post($current_id);

Wordpress post Loop not working as expected

I'm using a theme that uses the blog page to display all of its content on the blog page rather than an excerpt and then when i click into the post i want to show the whole content.
I'm using the following code:
$postId = get_the_ID();
$ex = the_excerpt();
if($postId == 19){
echo $ex;
}
else{
echo $content;
}
The blog page is located at post =19
I would expect only the excerpt to show on the blog page and the content to show on the post page. However both show. Also it doesnt matter if i change the number 19 in my if statement as the same happens. Can anyone see where i am going wrong?
edit made changes, screen shots:
You need to use the_excerpt(); inside the condition means where you want to display the excerpt but in case you want to get value of excerpt but does not want to display it directly then you have to use get_the_excerpt(); so you need to change
$ex = the_excerpt();
to
$ex = get_the_excerpt();
And same is the case with the_content()
Hope it helps you.
functions such as the_excerpt() are only available inside the loop or after you call the function the_post()
You may want to display only the except in your index.php
while (has_posts()) {
the_post();
the_excerpt();
}
In your single.php you may want to display the whole content
if(has_posts()) {
the_post();
the_content();
}
Use $postId = get_queried_object_id(); instead of $postId = get_the_ID();

Wordpress postcounter

How do I get a value of how many posts I have? Is there a wordpress tag for that?
I want to use it in my theme like this: Number of posts: 37
Yes there is a function that does that :
$count_posts = wp_count_posts();
From the official Documentation :
The default usage returns a count of the posts that are published. This will be an object, you can var_dump() the contents to debug the output.
Or :
Again qoute :
If you want to show the number of published posts use this code.
$published_posts = wp_count_posts();
echo $published_posts->publish;
I'm not sure which one is the right method, never tried before but seems like both will do the trick.
This code may help you to show no of post in current page
<?php
$count = 1;
if (have_posts()) : while(have_posts()): the_post(); ?>
layout of regular template file here
<?php $count++;
endwhile; endif;
echo $count;
?>
Alternatively you can use as shown in Wordpress Documentation wp_count_posts
<?php
$count_posts = wp_count_posts();
?>

wordpress custom php function, simple question about calling variable

Simple question, but couldn't find the answer.
When the function 'enkelpost'is called anywhere in WP the passed value($one_p) is gonne be the post ID displayed. But how to put this in the function itself. This is in the functions.php:
<?php
function enkelpost($one_p)
{
query_posts('p= $one_p '); //how to make line this work?
while (have_posts()) : the_post();
global $more; $more = FALSE;
the_content('Read more...');
endwhile;
}
?>
Lets say we want the singe post with id 150:
<?php enkelpost('150') ?>
Variables are not parsed when they are enclosed with single quote, put them in double quotes.
query_posts("p= $one_p");
OR
Do not use quotes for variables at all
query_posts("p=".$one_p);
Well I think this will be easier way!
<?php if (function_exists('enkelpost')): ?>

wordpress - other posts of the same category on post page

i need to link the lasts 5 posts of the same category after the post content in wordpress. Anyone can help me? thanks
Before the loop declare some var that will hold post category. Than query posts with that cat.
Here is example:
<?php
$cat_id = 0; // declare var
if(have_posts()):
while(have_posts()):
// do what you usually do
$cat_id = $post->post_category;
endwhile;
endif;
// here you will get posts and make html output
$last_in_cat = get_posts('posts_per_page=5&cat='.$cat_id);
foreach($last_in_cat as $cat_post):
?>
<?php $cat_post->post_title ?>
<?php endforeach; ?>
You can obtain anithing you want using WP_Query. It is very flexible and you should definitely start using it.
It is just like the SQL queryes.

Categories