开发者

Wordpress get unique Custom Field values only

开发者 https://www.devze.com 2023-03-03 19:26 出处:网络
I am working on a Wordpress site that displays a number of Projects stored in Custom Post Types. Each Custom Post includes two Custom Fields (amongst others) \"Project Name\" and \"Project Color\". Th

I am working on a Wordpress site that displays a number of Projects stored in Custom Post Types. Each Custom Post includes two Custom Fields (amongst others) "Project Name" and "Project Color". There may be several Custom Posts about a particular "Project Name".

I want to create a legend (a key) of all the projects which lists the "Project Names" and "Project Colors" taken from the custom field values above.

So for example, I have 3 projects called "Bananas" whose color is "Yellow", 6 called "Oranges" whose color is "orange" and 4 called "Apples" whose color is "green". My legend (key) should display one banana in y开发者_如何转开发ellow, one orange in orange and one apple in green.

Of course if I use the loop it displays 3 bananas, 6 oranges and 4 apples. How can I display just a list of the Project Names and Project Colors?

I've looked at three methods, a custom taxonomy, checking for duplicates in an array and using GROUP BY as a filter on posts_groupby.

GROUP BY seems like the right way to go but I can't get it to do anything. The furthest I got was putting this in my functions.php...

function sort_legend ( $groupby ) {
    global $wpdb;
    $groupby .= "{$wpdb->postmeta}.meta_key = 'Color' "; 
    return $groupby;
}

...and applying it as a filter around my Query object...

<?php  add_filter('posts_groupby', 'sort_legend'); ?>
<?php if ( $myQuery->have_posts() ) while ($myQuery->have_posts() ) : $myQuery->the_post(); ?>
<?php remove_filter('posts_groupby', 'sort_legend'); ?>

...but I still have multiple entries.


Here is my working solution...

Delete the 'Project Colors' and 'Project Names' custom fields from the custom template, (sigh), register a custom taxonomy called 'Project Classes' and assign the newly registered taxonomy to the custom post type, see here. In the Wordpress interface, give each custom post its appropriate 'Project Class' bananas, apples etc, again explained in the above link.

However there is still the problem of how to show a color key for each custom taxonomy entry. I reused the Project Class names as style sheet names so I could give each style name a background-color. However the real Project Class names have capital letters and spaces which need to be removed to make them style sheet friendly.

Add a php function rename_projects in functions.php that amends the spaces and capital letters in the 'Project Class' taxonomy name and makes it suitable for a css style name, I used...

// Rename Projects custom field for css styles
function rename_projects($sProjectName) {
$sFind = ' '; 
$sReplace = '-';
$sAmended = mb_strtolower(str_replace( $sFind, $sReplace, $sProjectName ));
return $sAmended;
}

Now you can use your Project Class names as style sheet names.

Next, in your page template display your custom taxonomy thus...

<?php
    $args=array(
    'taxonomy' => 'projectclass',
    'orderby' => 'name',
    'order' => 'ASC'
    );
    $categories=get_categories($args);
    foreach($categories as $category) {
        $sColorName = rename_projects($category->name); // To lower case and replace spaces
        echo '<div class="legend-color ' . $sColorName . '"></div> // Color style name
        <div class="legend-project">' . $category->name . '</div>'; // Taxonomy name
    } 
?>

So now for example a $category->name "My First Project" has a corresponding style sheet name my-first-project which you can add to your style sheet and style. Note that in the <div class="legend-color ' I am adding the style name to the existing class so the resulting class will be "legend-color my-first-project": i.e. two styles. The hard coded class "legend-color" holds all the style attributes except background-color which is in $sColorName.

Working fine here.

0

精彩评论

暂无评论...
验证码 换一张
取 消