简体   繁体   中英

WP custom post type UI

I use WP plugin "Custom Post Type UI" and a template of my own to add some content to my wp page.

<div id="AboutMain">
<?php 
    /* the original page contents */
    while (have_posts()) {
        the_post();
        get_template_part('content', 'page');

    /* New set of data fetched from my custom post types */
    $posts = get_posts(array(
        'numberposts' => -1,
        'post_type' => 'styrelse'
    ));

    if($posts)
    {
        foreach($posts as $post)
        {

            $namn=get_the_title($post->ID);
            $desc=get_the_content($post->ID);

            echo "
            <div class='styrelsen art-postcontent'>
                <h5 class='art-postcontent'>$namn</h5>
                <p>$desc</p>
            </div>
            ";  
        }
    }

Now I get a really strange behavior - since I have set the $posts with a custom query, in the for-each loop it should not contain the basic page contents anymore but my custom post type content right?

But the get_the_content($post->ID); fetches me the original contents of the page, and repeats it over and over while the get_the_title($post->ID); retrievs for me the correct title.

** * ** * ** solved ** * ** * ** * **** '

OK, here's how I did it.

first I realized that using that global variable name might not be the best idéa so I changed it to $board_posts.

Then I dumped that variable to find out how to access the title and body 'manually' and it turns out it was easier than I first thought.

foreach($styrelse_posts as $post)
{   
$namn = $post->post_title;
$desc = $post->post_content;

trial and error beats most issues =)

Using $post as a variable can cause some weird behavior because of how Wordpress uses that as a global variable. I'd suggest trying either calling setup_postdata($post) before get_the_title($post->ID) , or maybe changing the foreach to foreach($posts as $p) and using $p->ID as the argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM