Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hopfully I can explain this, I'm confusing myself now.

I have Template called 'Landing' that is the template for a number of different pages in Wordpress.

Each of the pages using this template will contain different blocks of information.

These blocks contain a title, an image, text and a link, I'm calling them Content_Widgets, they are not Widgets in the Wordpress sense.

My Content_Widgets are each posts and I have created a custom post type called Content_Widget.

Now I need to load the correct posts on the correct page.

I thought I could do this by having a category on the page in Wordpress and then the same category on the post for that page.

So it would be like load the post that have a category the same as this page.

Is this a good way to load the correct post on the correct page?

So this is the code I'm using but it's not outputting anything.(I'm using Advanced Custom Fields in the Custom post type.)

The correct category name outputs in the h1 but nothing else.

Anyone have any ideas why this isn't working, or is this the wrong way to do this??

    <div id="contentWidgets">

            <?php
                                    // get category name for page
                $cat = get_the_category($post->ID);
                $catName = $cat[0]->name;
                echo $catName;

                $content_args = array(
                    'post_type'         => 'Content_Widget',
                    'category_name' => $catName                 
                );

                $content_loop = new WP_Query($content_args);

                if($content_loop->have_posts()):
                    while($content_loop->have_posts()):
                        $content_loop->the_post();

                $linkImg = get_field('image');
                $txt = get_field('text');
                $link = get_field('link');

            ?>

            <h1><?php echo $catName;?></h1>

            <dl>
                <dt><?php the_title(); ?></dt>
                <dd><img src="<?php echo $linkImg; ?>" alt=""></dd>
                <dd><?php echo $txt; ?></dd>
                <dd><a href="<?php $link; ?>">Find out more</a></dd>
            </dl>


            <?php

                endwhile;
                endif;

                wp_reset_postdata();

            ?>

        </div><!--contentWidgets-->
share|improve this question
    
you use both $catName and $cat_Name, is that a typo here? –  Milo Aug 29 '13 at 16:39
    
also see register_post_type: $post_type (max. 20 characters, can not contain capital letters or spaces) –  Milo Aug 29 '13 at 16:41
    
$catName was a typo just in this example, I've updated. I've changed the Custom Post Type name to all lowercase but I'm still not getting any results. –  Simon Cooper Aug 29 '13 at 16:54
add comment

1 Answer

up vote 1 down vote accepted

category_name is a bit misleading, it expects the slug, not the name. Try:

$content_args = array(
    'post_type' => 'Content_Widget',
    'category_name' => $cat[0]->slug          
);
share|improve this answer
    
Thanks Milo, I've been messing with this all afternoon –  Simon Cooper Aug 29 '13 at 17:35
    
So thought I had this but I have another slight problem. Because the page that the post are loading into has the same category that is in the output. Is it possible to skip the first entry in the loop. –  Simon Cooper Aug 29 '13 at 18:30
    
not sure I understand what you're saying. you can skip querying the first item by adding 'offset' => 1 to the query, does that do what you need? –  Milo Aug 29 '13 at 20:23
    
'offset' => 1 fixed it. –  Simon Cooper Aug 30 '13 at 6:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.