简体   繁体   中英

Wordpress Page Gallery

I am attempting to have a Gallery of wordpress pages.

The code should get all of the child pages of a certain page, and return the results with the Thumbnail of the page and the name of it. The thumbnail needs to be a clickable link.

I have gotten to this point, and am stuck:

 <?php $pages = get_pages(array('child_of' => 8)); ?>  

        <?php foreach ($pages as $page): ?> 
            <h1><?php echo $page['post_title'] ?></h1> 
            <a href="<?php echo $page['guid'] ?>"><img src="" /></a> 
        <?php endforeach; ?>

Welcome to Stack Overflow. First thing's first: if you have any future issues that you want to ask on SO, saying "It doesn't work" doesn't tell us anything. If you have a problem with your car, do you walk to a repair shop and say "my car doesn't work, tell me what's wrong"?

Just keep that in mind, please.

To answer your question, the returned items from either get_pages or get_posts (or any standard WP Query Object) are not associative arrays. They are objects. Access them like so:

<?php
$pages = get_pages(array('child_of' => 8));
foreach($pages as $page):
?> 
    <h1><?php echo $page->post_title; ?></h1> 
    <a href="<?php echo $page->guid; ?>"><img src="" /></a> 
<?php
endforeach;
?>

UPDATE: What you're doing isn't BAD per say, but it is pretty sloppy. It's not your fault either, as it happens to everyone that's new to Wordpress.

What you've done is you've created essentially a "Blank" page with no content, and your intention was to use it as a sort of Shell for your Page Archive Template (each Page being a child of the Paintings page). In reality, you don't really even need to do this.

Judging by the classes in your main menu, you're using the Menu Object (located in Appearances->Menus) to create your menu bar at the top of your page. This is perfectly fine, and is the best way to generate your menus.

As for generating your Paintings Archive in a "Wordpress-friendly" manner, you should create a Category (Posts->Categories), name it "Painting", and then click the Add New Category button.

Then, take all of the Painting Pages that you've already created, move them over to new Posts, and select the "Painting" option for each Post that you create.

Once that's finished, you can switch out the empty Paintings Page in your Menu Object for the Painting Category (now available in the left side in your Category Selector). This will create a link that tells Wordpress and your Theme to Query only Posts of type "Painting" and aggregate the results onto one page, which is what you want.

The nice part about this is that if you want to further categorize each Painting into different sub-categories, you can do that as well. For instance, you can make a new category called "Watercolor" a child of "Painting", and tag all of your Watercolor Posts with that Category.

The theme you have doesn't seem to bad on its own. Before trying to rip apart or creating your own code from scratch, it would be best to get used to the Wordpress flow and organization first.

Let me know if this helps.

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