Is the syntax problematic with this PHP code for Wordpress? - php

Seems like the problem with this is the PHP syntax, but no luck in Wordpress forums. This first code block generates a link to the newest post in category "posts."
<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
This next code block should display the custom field data for the latest post in "posts," with the key of the custom field being "qanda." But it doesn't and it displays nothing.
<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php echo get_post_meta($post->ID, "qanda", $single = true); ?>
<?php endwhile; ?>
Thanks, Mark

try renaming your second query, otherwise Wordpress will think it is already done
<?php
$my_other_query = new WP_Query('category_name=posts&showposts=1');
while ($my_other_query->have_posts()) : $my_other_query->the_post();
echo get_post_meta($post->ID, "qanda", true);
endwhile;
?>

Apart fromthat $single = true should just be true it looks OK... try var_dump instead of echo and see what you get.

You might need to name it something different. Wordpress might think that you have already done that set of posts, so it is starting at the end, which means it doesn't have anymore posts to process.

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);

How to : Populate Content of Page to another page in WordPress

I am using OptionTree for WordPress option panel, I am getting the page ID, and using that page ID I want to populate the content of the page to another page. Here is my code:
<?php
$test_input = ot_get_option( 'for_myapp_features' );
?>
<?php $the_query = new WP_Query( 'page_id=$test_input' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile;?>
Any help would be helpful.
You can use get_post_field and get_the_title() to get
content and title.
$test_input = ot_get_option('for_myapp_features');
echo get_post_field('post_title', $test_input); // to get the title
//or
//echo get_the_title($test_input); // to get the title
echo get_post_field('post_content', $test_input); //to get the content
Hope this helps!
If you want to display the content for that page, then add the_content() within your while loop. the_content() will display the page’s content. I would also add a query reset, wp_reset_query();, after the endwhile to restores the global post data to the original query.

Integrating Wordpress page into existing HTML site

I've run into a bit of a wall with a site I'm designing for a client. This was originally a very low budget static site but the client now needs the ability to edit some content from time to time. I've heard that you can just embed a wordpress page into existing HTML site and have followed the steps on the Codex site but can't seem to get it working. Any help is greatly appreciated.
So the wordpress page I'm trying to embed is the following:
http://octagonclubmiami.com/cs/
And the code I'm using is as follows:
This is posted at the beginning of the php file
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('cs/wp-blog-header.php');
?>
And this is within the body of the php file
<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php
endforeach;
?>
The actual page I'm trying to embed into is the following:
http://octagonclubmiami.com/community_service_test.php
As it sits it's currently displaying I guess some sample post instead of the page mentioned earlier.
Thanks in advance!
The better solution (I think) is to use get_post() and specify the ID number of the post/page you wish to output:
<?php
$page = get_post( 'ID NUMBER HERE' );
setup_postdata( $page );
?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
You can identify the post's ID number in the WordPress back-end.

How To Show Number of Posts in WordPress? Not Working

I can’t figure out why this won’t work? I have more than 15 posts but they’re getting cut off the page.
The site: http://gruntsandco.com/
My loop:
<?php $latest = new WP_Query('showposts=15'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
Loopageness
<?php endwhile; ?>
You can try 'posts_per_page' attribute for WP Query.
<?php $latest = new WP_Query('posts_per_page=15'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
Loopageness
<?php endwhile; ?>
If you want to show all posts just set this parametr to -1.

Normal loop doesn't work after custom ones

I used two custom loops on my site:
<?php $wp_query = new WP_Query("post_type=page&post=15"); while($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php // content ?>
<?php endwhile; wp_reset_postdata(); ?>
<?php $wp_query = new WP_Query("post_type=page&post=15"); while($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php // content ?>
<?php $wp_query = new WP_Query('showposts=3&post_type=contests&orderby=meta_value&meta_key=date&order=desc'); if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php //content ?>
<?php endwhile; wp_reset_postdata(); else : ?>
<?php ... ?>
<?php endif; ?>
And then I wanted to return to my regular loop for retrieving the news:
<?php rewind_posts(); ?>
<?php while(have_posts()) : <?php // content ?>
<?php endwhile; ?>
But it returns nothing (and there are posts to be displayed). When I tried to run an if(have_posts...) statement, it returned false. What I did wrong?
You're doing nothing wrong per say; WordPress is. Those loop related functions set up and reset globals, making you lose state because you're nesting WP loops. Adding insult to injury, you're overwriting $wp_query, which is at the core of the WP loop-related functions.
You could potentially work around these problems in multiple ways. One would be to use output buffers outside of the loop to set a few variables that you can use further down. Another could be to not use the API at all apart from $q = new WP_Query(...) and a normal foreach loop -- without using the built-in template functions.
(Actually learning php should trump both of these ideas if you don't understand them. Seriously, don't create a WP theme with complicated logic without knowing at least enough php to understand the source code of the template functions you're relying upon.)

Categories