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')): ?>
Related
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);
I use the Wordpress is_page conditional tag frequently, and typically I am defining a single ID, or an array of ID's to include, or exclude from displaying some custom code.
What I'm trying to figure out, is whether I can define a range of ID's to feed in as an argument.
Here is a simple example of using the is_page condition:
<?php if(is_page( 5 ): ?>
<p>This is some text</p>
<?php endif; ?>
Here is a simple example of using the id_page condition with an array:
<?php if(is_page( array( 5,6,7 ) ): ?>
<p>This is some text</p>
<?php endif; ?>
What I'd like to do, is have this where I can pass through an argument, and define that as a range of ID's such as 5-7:
$arg = 5-7;
<?php if(is_page( $arg ): ?>
<p>This is some text</p>
<?php endif; ?>
Where I'm defining the arg, I think it's a matter of maybe setting a boundary, defining an array, and placing that in a foreach statement, but I'm still trying to wrap my head around it all.
According to http://codex.wordpress.org/Function_Reference/is_page, you can't use it as a range. However, I'd use this function instead
$isPage = false;
foreach(range(5, 7) as $page)
{
if(is_page($page))
$isPage = true;
}
I'm currently using a wordpress function in order to display posts from a specific category. A simplified example is shown below:
<?php query('cat_name=cat1&posts=1') ?>
Essentially this gets 1 post from the category cat1.
However I have a variable saved which gets the current category (this is on category pages):
<?php $thiscat = get_the_category(); ?>
Current Category: <?php echo $thiscat ?>
How can I now echo the variable $thiscat into the arguments of my query above so that the category name is filled in for me? This function is applied on different category pages so having it automatically passed to the arguments of my query saves a lot of hassle.
Thanks in advance for any help.
You only echo something when you want to output it to the browser, here we concatenate the query string with the variable:
<?php $thiscat = get_the_category(); ?>
<?php query('cat_name=' . $thiscat . '&posts=1') ?>
Not sure I understand the question, but it sounds like you want to use $thiscat in your query. This should do it:
<?php
$thiscat = get_the_category();
query("cat_name=$thiscat&posts=1")
?>
Note the double quotes, which are necessary. If you use single quotes, the variable will not get expanded.
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.
I have this function :
<?php
function getmypost($number)
{
query_posts('p=1828');
while (have_posts()) : the_post();
the_title('<h1>', '</h1>');
the_content();
endwhile;
}
?>
I need to make the 1828 as a variable
I have tried this:
query_posts('\'p='. $number .'\'');
But it does not work. What would be the right way to do this?
If I understand you correctly
query_posts('p='.$number);
should work.
If you need a single quote ' in the string you'd escape the '
query_posts('p=\''.$number.'\'');
or using double quotes (more elegant, and you can put the variable straight in. Dominik already suggested this in his answer)
query_posts("p='$number'");
You could use
query_posts("'p=$number'");