Recently in the use of WP to customers to make a corporate website, but there have been no problems encountered.
The event is this: I'm infunction.phpThe file has an article that gets an article based on the category ID, because the HTML element in the method is shared across multiple pages, but I get exactly the same information when I invoke the index.php page, and the custom query condition fails, but if you use object direct access, there's no problem.
function.method names in PHP/** * Information corresponding to the first class ID * @param integer $cat _id [category ID number]* @param integer $per _page [show bars]* @return [type][description]*/functionCat_product ($cat _id=0,$per _page=3){$args=Array(' posts_per_page ' =$per _page, ' category ' =$cat _id );$rand _posts= Get_posts ($args );foreach($rand _posts as $post):Setup_postdata ($post );Echo $post->post_title. " <br> ";//There's no problem with this wayEchoGet_the_title ();Echo"<br><br>";//The next object data cannot be obtained in this way? ><?phpEndforeach; Wp_reset_postdata ();? ><?php}//End Cat_productIndex.PHP<?php cat_product (1); //cat_product (4);?>
Solution: 1. Write the method in Funciton directly on the index page disadvantage: You need to copy and paste the code every time you need to call, but basically the content structure is the same, it's not a different query condition.
<?PHP//There is no problem writing the methods in Funciton directly on the index page$args=Array(' Posts_per_page ' =>5, ' an ' to-do ' = ' rand ');$rand _posts= Get_posts ($args );foreach($rand _posts as $post):Setup_postdata ($posts );Echo $posts->post_title. " <br> ";EchoGet_the_title ();Echo"<br><br>";Endforeach; Wp_reset_postdata ();?>
2. Use the object access disadvantage directly in the function file: You cannot use methods such as the_id (), and you need to change the code
/** * Information corresponding to the first class ID * @param integer $cat _id [category ID number]* @param integer $per _page [show bars]* @return [type][description]*/functionCat_product ($cat _id=0,$per _page=3){$args=Array(' posts_per_page ' =$per _page, ' category ' =$cat _id );$rand _posts= Get_posts ($args );foreach($rand _posts as $post):Echo $post->post_title;//There's no problem with this wayEcho $post-ID;? ><?phpEndforeach; Wp_reset_postdata ();? ><?php}//End Cat_product
3. In function, you can use THE_ID () to resolve the object by declaring it as a global object.currently using the program
<? PHP foreach ($the _queryas$post):$GLOBALS$post; // saves the current object as a global object setup_postdata ($post); // Post that is declared as global, you can use the_id this way to get the data Var_dump (get_the_id ()); // This is the normal output. Endforeach; Wp_reset_postdata ();? >
Reference: Codex.wordpress.org/function_reference/get_postsget_posts () not working in FUNCTIONS.PHPCSDN WordPress setup_ The problem of Get_post () failure after postdata. Wordpress.org/function_reference/setup_postdata
From for notes (Wiz)
WordPress The_xxx method Invalidation in function.php file method