$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How can I use query_posts to loop through posts and construct my own content?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How can I use query_posts to loop through posts and construct my own content?

matteradmin10PV0评论

I am using query_posts to combine two posts categories on a page (by inserting this simple php script on the page using a plugin). This script gives me all the posts I want, however one of the categories has some extra custom fields using another plugin (CFS). The plugin has an api to get all fields and values by post id, I want to be able to loop through the wp posts array and add the custom fields where appropriate but I don't know much about wordpress and all the options and different ways of doing the same thing are confusing. I tried to use a wp_query but couldnt get it to work. Is it possible to use query_posts to not automatically add to the webpage but loop through the items so I can add some extra elements?

Edit: To clarify, I am looking for an alternate way to display the posts than add_post_content. I would like to basically make my own version of it, but in each loop also check for the extra fields and add those where appropriate. Is there an alternate to add_post_content like that/a way to get an array of posts rather than display the posts?

$paged = ( get_query_var('paged') ? get_query_var('paged') : 1);

 //adds all posts to page. Is there a way to get the posts but not add to page automatically?
$posts = query_posts( array( 'category__in' => array(152,1), 'posts_per_page' => 5, 'orderby' => 'DATE', 'order' => 'DESC', 'paged' => $paged,) );

foreach ($posts as $post){

    if($posts->cat_id = $target_category){ //pseudocode, if post category is cat with extra fields

        $fields = CFS()->get( False, $post->ID); //this line gives array of meta fields and values. 

         add_fields_to_div(); //pseudocode, if target cat add custom field data

       }

    add_post_content(); //add the content from $post
}

the_posts_pagination( ); //paginate
Post a comment

comment list (0)

  1. No comments so far