Get post_meta of one post type who is different of display post - custom-post-type

I'm looking for to get "get_post_meta" from a post type which is different of the display post ?
here is my code , but this display the post_meta of all posts in the post type !
I think it could be compare the posts who have the same slug (post_name) , but I don't know how to do !
<?php $sectionscontact = new WP_query(array('post_type' => 'sections-contact', 'post_count'=>1));if ($sectionscontact->have_posts()): while ( $sectionscontact->have_posts() ) : $sectionscontact->the_post(); $telephone_meta_empty = get_post_meta(451,'telephone', true);
if ( ! empty ( $telephone_meta_empty ) ) { ?>
<div class="tp_titre_bloc" style="border-right:1px solid #999999;"> Téléphone</div>
<div class="tp_content_bloc">
<?php echo get_post_meta(get_the_ID(), 'telephone', true); ?>
</div>
<?php } ?>

What I've done here is to tidy it up for you.
Also, it displays the post_meta for everything in this post type because you're using post_count as opposed to posts_per_page, which is the correct term to display some certain amount of posts in one query.
So, Instead of this:
<?php
$sectionscontact = new WP_query( array(
'post_type' => 'sections-contact',
'post_count' => 1
));
if ($sectionscontact->have_posts()):
while ( $sectionscontact->have_posts() ) :
$sectionscontact->the_post();
$telephone_meta_empty = get_post_meta(451,'telephone', true);
if ( ! empty ( $telephone_meta_empty ) ) { ?>
<div class="tp_titre_bloc" style="border-right:1px solid #999999;">Téléphone</div>
<div class="tp_content_bloc">
<?php echo get_post_meta(get_the_id(), 'telephone', true); ?>
</div>
<?php } ?>
You want this:
<?php
$sectionscontact = new WP_query( array(
'post_type' => 'sections-contact',
'posts_per_page' => 1 //changed post_count to posts_per_page
));
if ($sectionscontact->have_posts()):
while ( $sectionscontact->have_posts() ) :
$sectionscontact->the_post();
$telephone_meta_empty = get_post_meta(451,'telephone', true);
if ( ! empty ( $telephone_meta_empty ) ) { ?>
<div class="tp_titre_bloc" style="border-right:1px solid #999999;">Téléphone</div>
<div class="tp_content_bloc">
<?php echo get_post_meta($post->ID, 'telephone', true); // changed get_the_id() to $post->ID?>
</div>
<?php } ?>
Let me know if that helps.
More info on WP_Query: http://codex.wordpress.org/Class_Reference/WP_Query
Thanks.

Related

Get Post List Custom Post type Category Wise

I want to display all posts as list category wise of a custom post type.
for Ex.:
demo Cat 1
- Post of category demo 1
- Post of category demo 1
- Post of category demo 2
- Post of category name 2
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1, //show all posts
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<?php if(has_post_thumbnail()) { ?>
<?php the_post_thumbnail(); ?>
<?php }
/* no post image so show a default img */
else { ?>
<img src="<?php bloginfo('template_url'); ?>/assets/img/default-img.png" alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" width="110" height="110" />
<?php } ?>
<?php echo get_the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
https://wordimpress.com/loop-through-categories-and-display-posts-within/

Pagination not working( using wp-paginate) i m using wordpress

here is my code, I am using wp-paginate plugin in wordpress. I have tried many thing like replacing twentyfourteen_paging_nav(); with
if(function_exists('wp_paginate')) {
wp_paginate();
}
else {
twentyfourteen_paging_nav();
}
in archieve.php but still its not working.
<ul class="blog-list">
<?php
$blog = array(
'category' => '8', // post_id of category
'order' => 'Asc',
'posts_per_page' => 4,
'paged' => $paged
);
$blog_post = get_posts( $blog );
foreach ($blog_post as $post) : setup_postdata( $post );
$blog_img = $post->ID;
$blog_title = $post->post_title;
$blog_para = $post->post_content;
$blog_trimed=wp_trim_words($blog_para, 45);
$blog_name = get_post_meta($post->ID, 'author name', true);
?>
<li>
<div class="blog-content-left">
<?php echo get_the_post_thumbnail($blog_img, array(420,250)); ?>
</div>
<div class="blog-content-right">
<?php echo $blog_title; ?>
<p class="blog-date-area"><span class="blog-date"><?php echo get_the_date('M d, Y'); ?></span><span class="blog-date blog-crcl"><?php echo $blog_name; ?></span></p>
<div class="blog-para">
<p><?php echo $blog_trimed; ?></p>
</div>
<span class="read-more-btn-blue">
Read More
</span>
</div>
</li>
<?php endforeach;?>
</ul>
<?php echo wp_paginate(); ?>
Thanks for help in advance.
I didn't find any solutions, so I used pagednavi.

Displaying All Data from Custom Post Type Fields through Advanced Custom Fields

I have looked all over Google, trying to figure this out. I've made some progress but still stuck. I'm pretty new to ACF and custom post types. I have a custom post type of Attorneys that I setup through WCK. That post type has a field group with field names of attorney_photo, attorney_name and attorney_areas_of_practice. With the code below, I can get the attorney_name and attorney_areas_of_practice (repeater field) to display, but not the attorney_photo. I have the info displaying correctly on each attorneys specific page, but I need this page to be a listing of all the attorneys. Not sure what I am doing wrong with the image part.
<?php get_header(); ?>
<?php
$args = array(
'posts_per_page' => 30,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'attorneys'
);
query_posts($args);
if ( have_posts() ) :
?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="attorney-preview">
<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo; ?>" />
<p><strong><?php echo get_post_meta($post->ID,'attorney_name', true); ?></strong></p>
<ul>
<?php
while ( have_rows('attorney_areas_of_practice') ) : the_row();
$attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
echo "<li>" . $attorney_area_of_practice . "</li>";
endwhile;
?>
</ul>
</div><!-- .attorney-preview -->
<?php endwhile; ?>
<?php endif; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
?>
<?php get_footer(); ?>
When you add image field through ACF plugin there are some options of return value. For example return value is: image object or return value is: image url, in your case return value might be selected as image object, not image url. That is why here with your code is being returned an array rather than only url. To get only image url from this array, please write as following a bit change:
<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo['url']; ?>" />
Try this
<?php query_posts(array('posts_per_page' => 30,'order' => 'ASC','orderby' => 'title','post_type' => 'attorneys'));
if (have_posts()) : while (have_posts()) : the_post();
$attorney_photo = get_post_meta($post->ID, 'attorney_photo', true);
$attorney_name = get_post_meta($post->ID, 'attorney_name', true); ?>
<div class="attorney-preview">
<img src="<?php echo $attorney_photo; ?>" />
<p><strong><?php echo $attorney_name; ?></strong></p>
<ul>
<?php
while ( have_rows('attorney_areas_of_practice') ) : the_row();
$attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
echo "<li>" . $attorney_area_of_practice . "</li>";
endwhile;
?>
</ul>
</div><!-- .attorney-preview -->
<?php endwhile; endif; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
?>

Wordpress Pagination for a custom page template and custom post type

Edited: Answered below.
I'm using the following query to paginate my posts on a Custom Page template that pulls a Custom Post type. this exact code works on Author.php, but this custom template Page, "Filmmaking" does not show the pagination. I have a simple, tried and true pagination function after my loop. Why would it work for author.php, but not for page-filmmaking.php?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$film_query = array(
'post_type' => 'filmmaking', // Custom Post Type
'paged' => $paged, // Set it up to be paged so you can use pagination
'posts_per_page' => 2, // How many to show per page
);
// The Query
$the_query = new WP_Query( $film_query );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
Answer: Get rid of if has_posts.
Working Code:
<?php
/**
* Template Name: pagination test
*/
get_header(); ?>
<?php //get_sidebar(); ?>
<div id="page-left">
<?php
$wp_query = new WP_Query();
$wp_query->query(array(
'post_type'=>'filmmaking',
'paged' => $paged,
'posts_per_page' => 2,
));
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php previous_posts_link('« Nyare') ?>
<?php next_posts_link(' Äldre »') ?>
</div><!-- page left -->
<?php get_footer(); ?>

Yii Search in navigation menu

I'm currently writing an application in Yii in which i want to create a search box in the header (either in the navigation menu or just above it). This search box should be able to be accessed from every part of the site, and it should be able to search on different columns of different tables.
I have no idea on how to do this, and almost all posts on the web about it involves using the grid-view, or an extension (I'd like to create the code without an extension if that's possible).
Do you have an idea to how the search code should look (what i should put in which controller etc.)?
-- EDIT --
I still don't know how to do this but I will show you what I have at the moment anyway. It is not much and it is pretty obvious where i am missing some code.
/view/layout/main.php:
<?php echo CHtml::form(Yii::app()->createUrl('product/search'), 'get') ?>
<?php echo CHtml::textField('search_key','',array('placeholder' => 'Search')); ?>
<?php echo CHtml::submitButton('Go'); ?>
<?php echo CHtml::endForm() ?>
/view/product/search.php:
//Not sure by any means what to write here, but I'll like a list view populated with the search results
/controllers/productController.php
/**
* Search through model.
*/
public function actionSearch()
{
if(isset($_GET['search_key'])){
$search = $_GET['search_key'];
$model->name = $search;
}
$this -> render('search', array(
'model' => $model,
));
}
/models/Product.php
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
You can perform like this:
Find #mainmeu in /protected/views/layout/main.php
<div id="mainmenu">
<div style="width: 80%;float: right">
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'home', 'url'=>array('/site/index')),
array('label'=>'about', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'contact', 'url'=>array('/site/contact')),
),
)); ?>
</div>
<div style='float: left;direction: rtl; color: #ffffff; margin: 5px 0 0 5px; font-size: 13px'>
<?php echo CHtml::form(Yii::app()->createUrl('product/search'),'get') ?>
<?php echo CHtml::textField('search_key', 'search') ?>
<?php echo CHtml::submitButton(); ?>
<?php echo CHtml::endForm() ?>
</div>
</div><!-- mainmenu -->
Edit:
/models/Product.php:
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/controllers/productController.php -> actionSearch():
public function actionSearch()
{
$model = new Product('search');
$model->unsetAttributes();
if(isset($_GET['search_key']))
$model->name = $_GET['search_key'];
$this -> render('search', array(
'model' => $model,
));
}
/view/product/search.php:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'product-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
'name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>

Resources