I've used this in other functions, but it doesn't seem to work in this particular one...
<?php
$page = get_the_title();
$blogusers = get_users('orderby=display_name');
foreach ($blogusers as $user) {
$cpt_count = wpse31443_author_has_custom_post_type( $user->ID, $page );
if (!empty($cpt_count)) {
echo '<li>' . $user->display_name . '' . $cpt_count1 . '</li>';
}
}
?>
If I change $page = get_the_title(); to $page = 'title';then it works, so it's something with get_the_title(); but I'm not sure what because it has worked in other functions.
The most common reason why "get_the_title()" wont work is if it is not in "the loop". Make sure that you call the function from within the loop only. If called from elsewhere, you will need to pass the page/post id to the function.
You'll get more information here:
http://codex.wordpress.org/Function_Reference/get_the_title
Try This:
<?php
global $post;
$page = $post->post_title;
$blogusers = get_users('orderby=display_name');
foreach ($blogusers as $user) {
$cpt_count = wpse31443_author_has_custom_post_type( $user->ID, $page );
if (!empty($cpt_count)) {
echo '<li>' . $user->display_name . '' . $cpt_count1 . '</li>';
}
}
?>
Related
I'm learning PHP and WordPress development, so I assumed that maybe here I'll find the answer or a tip.
I've restriced the_content based on user role. After the end of the_content I'd like to display button whitch is unique to the specific post. So here is the code which display that:
function displaycontent($content) {
if(is_singular( 'custom_post_type' )) {
$aftercontent = 'I Want To Add Code Here';
$fullcontent = $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
And I'd like to insert code below into the underlined place above:
<?php
$post = $wp_query->post;
$group_id = get_field( 'link_number' , $post->ID );
if( $group_id ) {
echo do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
}
?>
How can I do that?
It's probably better to create a custom shortcode for this. If you change how the_content works, it will be global, everywhere.
Note:
This code is completely untested and put together after 5 min of Googling, so if somehting is wrong, feel free to comment and I'll amend it. It should be fairly close and is mostly for explaining the concept instead of a pure copy/paste solution
Register a new shortcode:
add_shortcode('my_awesome_content', 'my_awesome_content_func');
Create the callback function:
Here we've added $atts which will contain our attribute (the post id):
function my_awesome_content_func($atts = [])
{
$postId = $atts['postid'] ?? null;
if ($postId === null) {
// We got no id so let's bail
return null;
}
$post = get_post($postId);
if (!$post) {
// We didn't find any post with the id so again, let's bail
return null;
}
$group_id = get_field( 'link_number' , $post->ID );
$content = $post->content;
if( $group_id ) {
$content .= do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
}
return $content;
}
Usage:
Now you should be able to call it like this:
echo do_shortcode('[my_awesome_content postid="' . $post->ID . '"]');
You can just embed you code below in the above filter with some modifications:
function displaycontent($content) {
if (is_singular('custom_post_type')) {
$post_id = get_queried_object_id();
$group_id = get_field('link_number', $post_id);
$aftercontent = '';
if ($group_id)
$aftercontent = do_shortcode('[checkout_button class="button" level="'.$group_id. '" text="Order"]');
$fullcontent = $content.$aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
OK, thanks everyone, I Got it! Here is the code if anyone would have same problem:
function displaycontent($content) {
if(is_singular( 'custom_post_type' )) {
$group_id = get_field('link_number');
$aftercontent = do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
I am not really expert in php, what I want to do is wrap the $duration variable in the last line inside a span tag, so I can style it.
<?php
$duration = '';
if( function_exists( 'cbc_get_video_data' ) ){
global $post;
$video = cbc_get_video_data( $post );
if( $video ) {
$duration = $video->get_human_duration();
}
}
$title_content .= '<div class="post-title-wrapper"><h1 class="post-title">' . $duration . get_the_title() . '</h1>';
?>
when I do something like this the site breaks
<?php
$duration = '';
if( function_exists( 'cbc_get_video_data' ) ){
global $post;
$video = cbc_get_video_data( $post );
if( $video ) {
$duration = $video->get_human_duration();
}
}
$title_content .= '<div class="post-title-wrapper"><h1 class="post-title">' . '<span class="video-duration">'$duration'</span>' . get_the_title() . '</h1>';
?>
Any suggestions?
Thanks
In your 2nd example you are missing a . either side of $duration. You also aren't closing off a <div> and don't need the additional concatenation between the opening span.
To refine this code, try the following:
$title_content .= '<div class="post-title-wrapper"><h1 class="post-title"><span class="video-duration">' . $duration . '</span>' . get_the_title() . '</h1></div>';
OK, I'm trying to integrate an API that lists adoptable pets into a wordpress website. I've done lots of googling and read through tutorials, and so far have managed to put together a super basic plugin that seems to do what I'm trying to accomplish. Currently I'm trying to pull in an image, but just the first image. Each animal may have 5 images associated with it, but I only want to pull in the first (default). Currently my code brings them all. Now I realize the problem is that I'm using "foreach()". But, this is new to me and my googling is not going well, and any other way I've tried to do it is just not getting me ANY pictures. Any advice is appreciated....and if I'm doing anything else wrong, feel free to let me know :) I also need to figure out how to paginate it, but I'm thinking that's a separate question! Thanks!
<?php
add_shortcode('pets', 'petsshortcode');
function petsshortcode() {
$request = wp_remote_get( 'https://petstablished.com/api/v2/public/pets?public_key=UlEK4EWvDAoOjXeQXSCQZAyBywWfqfOg&search[status]=available,foster&pagination[limit]=20&pagination[page]=1' );
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
if( ! empty( $data ) ) {
foreach( $data->collection as $collection ) {
echo '<div id="pet-block"><ul class="pet-profile"><li class="pet-name">'. $collection->name; echo '</li>';
echo '<li class="pet-meta">'.'<span class="sex">' . $collection->sex; echo '</span>' . '<span class="breed">' . $collection->breed; echo '</span><span class="age">' . $collection->age; echo '</span></li>';
echo '<li><p>' . $collection->description; echo '</li></p>';
foreach( $collection->images as $images ) {
echo '<div class="pet-photo"><img src="' . $images->image->url; echo '" width="200"></div>';}
echo '</ul></div>';
}
}
}
You don't need nested foreach loops in this case, simply use just one foreach loop like this:
// your code
foreach( $data->collection as $collection ) {
echo '<div id="pet-block"><ul class="pet-profile"><li class="pet-name">'. $collection->name; echo '</li>';
echo '<li class="pet-meta">'.'<span class="sex">' . $collection->sex; echo '</span>' . '<span class="breed">' . $collection->breed; echo '</span><span class="age">' . $collection->age; echo '</span></li>';
echo '<li><p>' . $collection->description; echo '</li></p>';
echo '<div class="pet-photo"><img src="' . $collection->images[0]->image->url; echo '" width="200"></div>';
echo '</ul></div>';
}
// your code
I want two differents excerpts on my homepage, one for the posts inside the loop, the other one for the latest post published, outside the loop.
I managed to add the excerpt on the latest post outside of the loop like this
<?php $postslist = get_posts('numberposts=1&order=DESC&orderby=post_date');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<?php wp_latest_excerpt('wp_latest', 'excerpt_more_latest'); ?>
<?php endforeach; ?>
While the excerpt for the posts inside the loop is
<?php wp_excerpt('wp_index'); ?>
For some reasons I am able to set the excerpt length differently for both of my excerpts, but the "more" stays the same. (both "more" have the class .view-article)
I thought I could create another excerpt with a different "more" function, but it does not work, here are my 2 different excerpts and more functions.
For the length
function wp_index($length)
{
return 21;
}
function wp_latest($length)
{
return 18;
}
For the "more"
function view_article($more)
{
return '... </br><a class="view-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
function latest_view_article($more)
{
return '... </br><a class="view-latest-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
add_filter('excerpt_more_latest', 'latest_view_article');
add_filter('excerpt_more', 'view_article');
And finally the excerpts
function wp_latest_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more_latest', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
function wp_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
Is this a wrong way to achieve this or did I make some mistakes in my code ?
I have a foreach statement that loops through all of the pages of a WordPress installation and lists them inside a div (using the get_pages() function).
It currently looks like this:
<?php
$pages = get_pages();
foreach ( $pages as $page ) {
$linkPage = '<a class="order" href="' . get_page_link( $page->ID ) . '">';
$linkPage .= $page->post_title;
$linkPage .= '</a>';
echo $linkPage;
}
?>
What I need to do now is to add an if statement that inserts the string "current" after class="order... if the link is the one from the current page.
It may seem like a silly question, but I'm a front-end developer with little PHP experience, and every time I tried to add the if I got a syntax error, claiming there was an unexpected if statement.
I hope I made myself clear.
If any help can be provided, it will be very much appreciated.
<?php
//get id of your current page
$post_id = $post[0]->ID;
$pages = get_pages();
foreach ( $pages as $page ) {
$current = $post_id == $page->ID ? ' current' : '';
$linkPage = '<a class="order '.$current.'" href="' . get_page_link( $page->ID ) . '">';
$linkPage .= $page->post_title;
$linkPage .= '</a>';
echo $linkPage;
}
?>
Check this, I'm not sure if $post_id = $post[0]->ID works in your wp version, but the if-statement logic is correct and will work. Try to echo $post_id and $current if something wrong.
Try this Code :
<?php
//get id of your current page
$post_id = get_the_ID();
$pages = get_pages();
foreach ( $pages as $page ) {
//Condition statement to add the class current
$current = $post_id == $page->ID ? 'current' : '';
$linkPage = '<a class="order '.$current.'" href="' . get_page_link( $page->ID ) . '">';
$linkPage .= $page->post_title;
$linkPage .= '</a> <br> ';
echo $linkPage;
}
?>
Hope this will help you...