I faced very weird error, there is a post type "author", and I show the author list in post type "articles", the list shows all authors successfully and also save successfully, but when I want to show them in front end the author does not shown in loop but when I print_r("author") the array show me selected authors but in loop there is no author. Here is my code:
I also used foreach loop.
Functions.php
<?php
function ct_downlaod_meta($post){
$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
echo '<select name="opt_meta_author[]" id="opt_meta_author" multiple="multiple">';
$val = get_post_meta($post->ID, 'opt_meta_author', true);
$q = get_posts('post_type=author&post_parent=0&numberposts=-1&orderby=menu_order&order=ASC');
$val_array = explode('<br />', $val); echo $val_array;
foreach ($q as $obj)
{
echo '<option value="'.$obj->post_title.'"'.selected($obj->post_title, $val);
if(in_array($obj->post_title, $val_array)) { echo ' selected="selected"'; };
echo '>'.$obj->post_title.'</option>';
}
echo '</select>';
}
add_action('save_post','save_download_meta_data');
function save_download_meta_data(){
global $post;
$opt_meta_author = implode('<br />',$_POST['opt_meta_author']);
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);
}
?>
author-bio.php
<?php
$opt_meta_author_array = explode('<br />', get_post_meta($post->ID, 'opt_meta_author', true));
$args = array(
'post__in' => $opt_meta_author_array,
'post_type' => 'author',
'orderby' => 'title',
'order' => 'ASC',
);
print_r($args);
$author = new WP_Query($args);
if($author->have_posts()) :
?>
<h3 id="entry-author-title"><?php _e('About The Author(s)', 'framework') ?></h3>
<?php
while($author->have_posts()) :
$author->the_post();
?>
<section id="entry-author" class="clearfix">
<div style=" margin-bottom:12px; float:left; width:100%; padding-bottom:5px;">
<div class="gravatar">
<?php
if(has_post_thumbnail()){
the_post_thumbnail( array( 70, 70 ) );
}
else{
echo '<img src="http://0.gravatar.com/avatar/6179f2642031d79f16bf2f03f0f66df9?s=70&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D70&r=G" />';
}
?>
</div>
<?php /*?> <h4><?php echo the_title() ?></h4><?php */?>
<div style="width:100%; margin:0 0px;">
<?php //echo get_post_meta($post->ID, 'txt_meta_country', true); ?>
</div>
<div class="entry-author-desc">
<?php echo the_content() ?>
</div>
</div></section>
<?php
endwhile;
endif;
?>
When i print_r($args); the array give me true result
Array ( [post__in] => Array ( [0] => Andrew Turnell [1] => Adrian Gimpel ) [post_type] => author [orderby] => title [order] => ASC )
but in loop is empty result.
So one day delay i found a solution on my own in functions.php
<?php
function ct_downlaod_meta($post){
$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
echo '<select name="opt_meta_author[]" id="opt_meta_author" multiple="multiple">';
$val = get_post_meta($post->ID, 'opt_meta_author', true);
$q = get_posts('post_type=author&post_parent=0&numberposts=-1&orderby=menu_order&order=ASC');
$val_array = explode('<br />', $val); echo $val_array;
foreach ($q as $obj)
{
echo '<option value="'.$obj->ID.'"'.selected($obj->ID, $val);
if(in_array($obj->ID, $val_array)) { echo ' selected="selected"'; };
echo '>'.$obj->post_title.'</option>';
}
echo '</select>';
}
add_action('save_post','save_download_meta_data');
function save_download_meta_data(){
global $post;
$opt_meta_author = implode('<br />',$_POST['opt_meta_author']);
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);
}
?>
in author-bio.php i not change any thing and my code is running well
Related
i've created a colophon for my website in which i post all the logos of the different sponsors i have. I add all the sponsors via custom post type. i also added a specific custom taxonomy to distinguish between the different typologies of sponsorships.
I use this code in the footer.php to display them:
<?php $terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { ?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $term->slug,
);
$pesca_post = new WP_Query ($arg);
$quanti_post = $pesca_post->post_count;
if(have_posts()){
while ($pesca_post->have_posts()) : $pesca_post->the_post();
$featured = get_the_post_thumbnail_url(get_the_ID(),'large');
if ($quanti_post == 5){
$classe_bootstrap = 15;
}elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
echo '<div class="col-md-' . $classe_bootstrap . '">';
if (isset($featured)){
$img = $featured;
}else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($post->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
<?php echo '</div>';
endwhile;
}?>
</div>
<?php }
}?>
my problem is that this code is completely working just on some pages, on other it shows the contents avoiding the ones belonging to the first term, no matter which it will be.
I have noticed that it works in pagaes where i use other queries.
What am i doing wrong?
i changed it in this way and now it's working!
$terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { //per ogni termine presente
$nome = $term->slug;?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $nome,
);
$elementi = get_posts($arg);
$quanti_post = count( $elementi );
if ($quanti_post == 5){
$classe_bootstrap = 15;
}
elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}
elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
foreach($elementi as $elemento){
$featured = get_the_post_thumbnail_url($elemento->ID,'large');
if (isset($featured)){
$img = $featured;
}
else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<div class="col-md-<?php echo $classe_bootstrap; ?>">
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($elemento->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
</div>
<?php }?>
</div>
<?php }
}?>
I have two WordPress installs, one is to be used as an intranet and the other is public. I need to pull in the data for a custom post type from the public site to be shown on the intranet, essentially so the admin doesn't need to enter the same data on each site.
So, far I've got this:
$wpdb = new wpdb(*CONNECTION DETAILS*);
$careers = $wpdb->get_results(
'SELECT *
FROM wp_posts
WHERE post_type = "career-post"'
);
if (!empty($careers)) {
echo '<ul id="careers-list">';
foreach ($careers as $career) {
echo '<li class="career">';
echo '<a class="wrap" href="http://domainname.com/career/'.$career->post_name.'" target="_blank">';
echo '<h2>'.$career->post_title.'</h2>';
echo get_field('career_duration') ? '<p class="duration">('.get_field('career_duration').')</p>' : '<p class="duration">(permanent)</p>';
echo '<h3>'.get_field('career_area').'</h3>';
echo '<p class="description">'.get_field('career_short_description').'</p>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
else {
echo '<h2>Sorry, no vacancies are currently listed.</h2>';
}
Which is pulling in the slug and title as expected, but as the ACF data is stored in a different table, I am not sure how to a) access that data and b) link it to the correct post.
Fetch ACF fields for a custom post type
<?php
$wpdb = new wpdb(*CONNECTION DETAILS*);
$careers = $wpdb->get_results(
'SELECT *
FROM wp_posts
WHERE post_type = "career-post"'
);
if (!empty($careers)) {
?>
<ul id="careers-list">
<?php
foreach ($careers as $career) {
$career_duration = get_field('career_duration', $career->ID);
$career_area = get_field('career_area', $career->ID);
$career_short_description = get_field('career_short_description', $career->ID);
?>
<li class="career">
<a href="<?php echo get_permalink($career->ID); ?>" target="_blank">
<h2><?php echo get_the_title( $career->ID ); ?></h2>
<?php
if($career_duration!="")
{
?>
<p class="duration"><?php echo $career_duration;?></p>
<?php
}
else
{
?>
<p class="duration">permanent</p>
<?php
}
?>
<h3><?php echo $career_area;?></h3>
<p class="description"><?php echo $career_short_description;?></p>
</a>
</li>
<?php
}
?>
</ul>
<?php
}
else {
echo '<h2>Sorry, no vacancies are currently listed.</h2>';
}
?>
OR
<?php
$career_post_type = 'career-post';
$career_post_args=array(
'type' => $career_post_type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => -1,
'orderby' => 'name',
'order' => 'ASC',
);
$career_post_my_query = null;
$career_post_my_query = new WP_Query($career_post_args);
if( $career_post_my_query->have_posts() )
{
?>
<ul id="careers-list">
<?php
while ($career_post_my_query->have_posts()) : $career_post_my_query->the_post();
$career_duration = get_field('career_duration', $post->ID);
$career_area = get_field('career_area', $post->ID);
$career_short_description = get_field('career_short_description', $post->ID);
?>
<li class="career">
<a href="<?php echo get_permalink($post->ID); ?>" target="_blank">
<h2><?php echo get_the_title( $post->ID ); ?></h2>
<?php
if($career_duration!="")
{
?>
<p class="duration"><?php echo $career_duration;?></p>
<?php
}
else
{
?>
<p class="duration">permanent</p>
<?php
}
?>
<h3><?php echo $career_area;?></h3>
<p class="description"><?php echo $career_short_description;?></p>
</a>
</li>
<?php
endwhile;
?>
</ul>
<?php
}
else {
echo '<h2>Sorry, no vacancies are currently listed.</h2>';
}
wp_reset_query($career_post_my_query);
?>
Shital, I don't see how your second solution would work as it's not referencing the external DB?
I've tried the following (similar to your first solution) and it doesn't return anything for the ACF fields either:
foreach ($careers as $career) {
$ID = $career->ID;
$slug = $career->post_name;
$title = $career->post_title;
$duration = get_field('career_duration', $ID);
$area = get_field('career_area', $ID);
$description = get_field('career_short_description', $ID);
echo '<li class="career">';
echo '<a class="wrap" href="http://domainname.com/career/'.$slug.'" target="_blank">';
echo '<h2>'.$title.'</h2>';
echo $duration ? '<p class="duration">('.$duration.')</p>' : '<p class="duration">(permanent)</p>';
echo '<h3>'.$area.'</h3>';
echo '<p class="description">'.$description.'</p>';
echo '</a>';
echo '</li>';
}
hello I am new to programing. I am creating a simple RSS feed plugin for wordpress to upload some products to WooCommerce and need to upload the category to wordpress . In plugin the category is visible but they are not showing like the url link. I need the category to show like checkbox. Can anybody have any idea ?
File number 1
* Init feed with information from DB
private function load()
{
if ($this->id) {
$post = get_post( $this->id );
if (!$post) {
$this->id = null;
return;
}
$this->title = $post->post_title;
$this->id = $post->ID;
$meta = get_post_meta($post->ID);
foreach ($meta as $key=>$item) {
$newKey = substr($key, 1);
$this->properties[$newKey] = $item[0];
if ($newKey == 'post_category') {
$this->properties[$newKey] = unserialize($item[0]);
}
}
}
}
..................
$fields = array( 'post_category');
..................
// Create post
$post = array('post_category' => $this->post_category);
And the file number 2 have this
<div class="postbox">
<div class="handlediv" title="<?php esc_html_e('Click to toggle', 'rss-autopilot'); ?>"><br></div>
<h3 class="hndle ui-sortable-handle"><span><?php esc_html_e('Categories', 'rss-autopilot'); ?></span></h3>
<div class="inside">
<ul class="rssap-categories-list">
<?php wp_category_checklist( 0, 0, $feed->post_category, false, null, true ); ?>
</ul>
<div class="clear"></div>
</div>
</div>
Here is your code.
$terms = get_terms( 'product_cat', $args );
if ( $terms ) {
echo '<ul class="product-cats">';
foreach ( $terms as $term ) {
echo '<li class="category">';
echo '<h2>';
echo '<input name="product_category[]" type="checkbox" value="'. $term->term_id.'"';
echo $term->name;
echo '</a>';
echo '</h2>';
echo '</li>';
}
echo '</ul>';
}
I'm trying to tweak a Wordpress Plugin - Category Thumbnail List to display "Coming Soon" when the shortcode calls for posts in a category but there are none.
I've tried contacting the developer but had no luck.
I've modified the original code using the following but it doesn't display. Any ideas?
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail() ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;
Here's the full code:
<?php
/*
Plugin Name: Category Thumbnail List
Plugin URI: http://jonk.pirateboy.net/blog/category/bloggeriet/wordpress/plugins/
Description: Creates a list of thumbnail images, using the_post_thumbnail() in WordPress 2.9 and up.
Version: 1.11
Author: Jonk
Author URI: http://jonk.pirateboy.net
*/
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
if ($categoryThumbnailList_Order == '') {
$categoryThumbnailList_Order = 'date';
}
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
if ($categoryThumbnailList_OrderType == '') {
$categoryThumbnailList_OrderType = 'DESC';
}
$categoryThumbnailList_Path = get_option('siteurl')."/wp-content/plugins/categoy-thumbnail-list/";
define("categoryThumbnailList_REGEXP", "/\[categorythumbnaillist ([[:print:]]+)\]/");
define("categoryThumbnailList_TARGET", "###CATTHMBLST###");
function categoryThumbnailList_callback($listCatId) {
global $post;
global $categoryThumbnailList_Order;
global $categoryThumbnailList_OrderType;
$tmp_post = $post;
$myposts = get_posts('numberposts=-1&&category='.$listCatId[1].'&&orderby='.$categoryThumbnailList_OrderType.'&&order='.$categoryThumbnailList_Order);
$output = '<div class="categoryThumbnailList">';
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail() ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;
$output .= '</div>';
$output .= '<div class="categoryThumbnailList_clearer"></div>';
$post = $tmp_post;
wp_reset_postdata();
return ($output);
$output = '';
}
function categoryThumbnailList($content) {
return (preg_replace_callback(categoryThumbnailList_REGEXP, 'categoryThumbnailList_callback', $content));
}
function categoryThumbnailList_css() {
global $categoryThumbnailList_Path;
echo "
<style type=\"text/css\">
#import url(\"".$categoryThumbnailList_Path."categoy-thumbnail-list.css\");
</style>
";
}
add_action('wp_head', 'categoryThumbnailList_css');
add_filter('the_content', 'categoryThumbnailList',1);
?>
<?php
add_action('admin_menu', 'my_plugin_menu');
function my_plugin_menu() {
add_options_page('Category Thumbnail List Options', 'Category Thumbnail List', 'manage_options', 'category-thumbnail-list', 'my_plugin_options');
}
function my_plugin_options() {
global $categoryThumbnailList_Order;
global $categoryThumbnailList_OrderType;
if( $_POST['save_category-thumbnail-list_settings'] ) {
// update order type
if( !$_POST['category-thumbnail-list_ordertype'] )
{
$_POST['category-thumbnail-list_ordertype'] = 'date';
}
update_option('category-thumbnail-list_ordertype', $_POST['category-thumbnail-list_ordertype'] );
// update order
if( !$_POST['category-thumbnail-list_order'] )
{
$_POST['category-thumbnail-list_order'] = 'DESC';
}
update_option('category-thumbnail-list_order', $_POST['category-thumbnail-list_order'] );
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
echo "<div id=\"message\" class=\"updated fade\"><p>Your settings are now updated</p></div>\n";
}
?>
<div class="wrap">
<h2>Category Thumbnail List Settings</h2>
<form method="post">
<table class="form-table">
<tr valign="top">
<th scope="row">Order by</th>
<td>
<select name="category-thumbnail-list_ordertype" id="category-thumbnail-list_ordertype">
<option <?php if ($categoryThumbnailList_OrderType == 'date') { echo 'selected="selected"'; } ?> value="date">Date</option>
<option <?php if ($categoryThumbnailList_OrderType == 'title') { echo 'selected="selected"'; } ?> value="title">Title</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">Display order</th>
<td>
<select name="category-thumbnail-list_order" id="category-thumbnail-list_order">
<option <?php if ($categoryThumbnailList_Order == 'DESC') { echo 'selected="selected"'; } ?> value="DESC">Descending (z-a/9-1/2010-2001)</option>
<option <?php if ($categoryThumbnailList_Order == 'ASC') { echo 'selected="selected"'; } ?> value="ASC">Ascending (a-z/1-9/2001-2010)</option>
</select>
</td>
</tr>
</table>
<div class="submit">
<!--<input type="submit" name="reset_category-thumbnail-list_settings" value="<?php _e('Reset') ?>" />-->
<input type="submit" name="save_category-thumbnail-list_settings" value="<?php _e('Save Settings') ?>" class="button-primary" />
</div>
<div>
Update the thumbnail sizes here
</div>
<div>
You may need to update your css when changing the thumbnail size
</div>
</form>
</div>
<?php
}
?>
Many Thanks,
Mike
Haven't tested your code, but a quick glance shows at least one problem... you're using this outside of the loop. When not used in the loop, has_post_thumbnail() doesn't know what post you're checking. With just that modification, your code should be this:
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail( $post->ID ) ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;
I want to output the results of a foreach statement but I want them grouped into a div in 3's
So like:
<div>image image image</div>
<div>image image image</div>
<div>image image image</div>
Here is my code so far:
$args = array( 'numberposts' => 3, 'offset'=> 0, 'category' => 9 );
$myrows = get_posts($args);
foreach($myrows as $row) { ?>
<div>
<?php if ( has_post_thumbnail($row->ID)) {
echo '<a href="' . get_permalink( $row->ID ) . '" title="' . esc_attr($row->post_title ) . '">';
echo get_the_post_thumbnail($row->ID);
echo '</a>';
}?>
</div>
<?php } ?>
$myrows = get_posts($args);
$chunks = array_chunk($myrows,3);
?>
<?php foreach($chunks as $myrows): ?>
<div>
<?php foreach($myrows as $row): ?>
<div>
<?php if(has_post_thumbnail($row->ID)): ?>
<a href="<?=get_permalink($row->ID)?>" title="<?=esc_attr($row->post_title)?>">
<?=get_the_post_thumbnail($row->ID)?>
</a>
<?php endif ?>
</div>
<?php endforeach ?>
</div>
<?php endforeach ?>
You can create blocks using array_chunk():
foreach (array_chunk($myrows) as $mychunk) {
echo '<div>';
foreach ($mychunk as $row) {
// print your entries
if (has_post_thumbnail($row->ID)) {
echo sprintf('%s',
get_permalink( $row->ID ),
esc_attr($row->post_title ),
get_the_post_thumbnail($row->ID)
);
}
}
echo '</div>';
}
Granted, if the if condition isn't met, you would get blocks of zero, one or two items instead of the expected three.
<div>
<?php
$args = array('numberposts' => 3, 'offset' => 0, 'category' => 9);
$myrows = get_posts($args);
foreach($myrows as $idx => $row) {
if ($idx % 3 == 0) echo "</div><div>";
if (has_post_thumbnail($row->ID)) {
echo '<a href="' . get_permalink($row->ID) . '" title="' . esc_attr($row->post_title) . '">';
echo get_the_post_thumbnail($row->ID);
echo '</a>';
} ?>
</div>
Why not use the modulo operator?
$counter = 0;
echo '<div>';
foreach ($myrows as $row)
{
$counter++;
if ($counter % 3 == 0) echo '</div><div>';
echo $row;
}
echo '</div>';
Try this code.
<?php
$args = array( 'numberposts' => 3, 'offset'=> 0, 'category' => 9 );
$myrows = get_posts($args);
$tempCnt=0;
foreach($myrows as $row) {
//12
if($tempCnt==3)
{
$tempCnt=0;
//do your reset code here.
}
$tempCnt++;
?>
<div>
<?php if ( has_post_thumbnail($row->ID)) {
echo '<a href="' . get_permalink( $row->ID ) . '" title="' . esc_attr($row->post_title ) . '">';
echo get_the_post_thumbnail($row->ID);
echo '</a>';
}?>
</div>
<?php } ?>