display the custom post type post in page - wordpress

I have a custom post type named "audio". To display all the posts in a page I wrote the code below, but it does not work.
<?php
$post_type = "audioes";//post type names
echo "djnbfj";
$post_type->the_post();
// The Query
$the_query = new WP_Query( array(
'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC',
'showposts' => 1,
));
// The Loop
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="issue-content">
<?php get_template_part('includes/breadcrumbs', 'page'); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();}
get_template_part('loop-audio', 'page');
?>
</div><!--issue-content-->
<?php endwhile; ?>
</div> <!-- end #left_area -->
How can i got the posts type i want the custom one written above.

Is the post type called "audio" or "audioes"? Your $post_type variable is set to "audioes".

I'm not sure what the get_template_part lines or the echo line is doing, but the below code should display your post type. You also have the post type name as "audioes" in your code, but you state that the name is "audio". Try the code below, but if the post type is named "audioes" then you'll need to change that in my code below. You can wrap additional divs, spans, or other HTML tags around <?php the_post_thumbnail(); ?> and <?php the_content(); ?> to give them CSS styles is you wish to.
<?php query_posts(array('post_type' => 'audio', 'posts_per_page' => 1, 'orderby' => 'post_date', 'order' => 'DESC')); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="issue-content">
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_query(); ?>

Related

Wordpress, inserting URL in a shortcode

I'm trying to display/insert a url into the shortcode below:
<?php echo do_shortcode('[sc_embed_player fileurl="http://www.example.com/wp-content/uploads/my-music/mysong.mp3"]'); ?>
I'm using a ACF field called "add_audio" which is a mp3 URL.
I used the default get field ( <?php the_field('field_name'); ?> )
code to call the the url.
I made a variable too and called ($variable) too.
But nothing worked!!
Here's the error i get
"Prase error:syntax error, unexpected 'add_audio'(T_STRING),
expecting,''or')'in
/app/public/wp-content/oxygen/component-framework/components/layouts/code-block.php(33):eval()'d
code on line 43"
I used Oxygenbuilder plugin and ACF plugins.
<?php
/*
* Easy Query Shortcode
* [easy_query container="div" template="template_906184" posts_per_page="20" post_type="audio_files"]
*/
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array('audio_files'),
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 20,
'paged' => $paged,
);
// WP_Query
$eq_query = new WP_Query( $args );
if ($eq_query->have_posts()) : // The Loop
$eq_count = 0;
?>
<div class="wp-easy-query paging-style-default grey">
<div class="wp-easy-query-posts">
<div>
<?php
while ($eq_query->have_posts()): $eq_query->the_post();
$eq_count++;
?>
<div id="div_block-17-25" class="ct-div-block audio-div">
<h1 id="headline-18-25" class="ct-headline">
<?php the_title(); ?></h1>
<div id="div_block-20-25" class="ct-div-block">
<?php echo do_shortcode('[sc_embed_player_template1 fileurl="<?php the_field('add_audio'); ?>"]'); ?>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
<?php include(EQ_PAGING); ?>
</div>
<?php endif; ?>
Simply Try Removing from your Code write it as follows:
<?php echo do_shortcode('[sc_embed_player_template1 fileurl="'.the_field('add_audio').'"]'); ?>

displaying standard WP get_content with 'Advanced Custom Fields'

I have the following loop that utilises Advanced custom fields (ACF) - Everything displays except the content. I have tried various solutions with no success.
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'sectors',
'orderby' => 'menu_order',
'order' => 'ASC'
));
global $post;
if($posts) {
?>
<?php
foreach($posts as $post) {
?>
<div class="content full sector">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
<?php
}
?>
<?php
}
wp_reset_postdata();
?>
Any help is greatly appreciated.
Thanks
When you use get_posts() to return post data, some post-related data is not available by default. One of these items is the_content(). This is resolved by calling an internal function setup_postdata(), with the $post array as its argument.
Solution: Add setup_postdata( $post ); within your foreach().
Read more in the Codex.

Displaying Custom Category loop in homepage

Thing is, In the homepage of my theme, I want to show post from different category in Different Div. Each DIV will contain 3 post from a category. I need a loop that can pick last 3 post from a specific Category. Can't find any suitable ans for it.
To explain things more easily, here is a demo picture of the Content section,
http://i.imgur.com/5QSzAIS.png
It will be a great help, if someone help me with the code !
<?php query_posts('cat=10&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This should get you started. You need to use this code twice. Where it says cat=10, you should enter your category ID (you can check this when you click on a Category from the admin panel, the the browser it will show something like this http://yourwebsite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=4&post_type=post)
Where it says tag_ID is the ID of your category.
I'm currently using a different method on a page of the site I'm building which allows me to run multiple loops in one page and specify the category for each one. This method I personally like better as it is more straightforward to me, and I can define the category with the slug instead of the ID.
Instead of using have_posts() and such, you use WP_Query() after defining your array and then wp_reset_postdata() to end your loop. The benefit is that you can keep running loops this way.
I'm also loading the data from custom fields in my posts using get_post_meta, but this method will work without that stuff.
<div class="audioGrid">
<?php
$args = array( 'post_type' => 'post',
'category_name' => 'audio',
'posts_per_page' => 3,
'order' => 'DESC' );
$query1 = new WP_Query($args);
while ( $query1->have_posts() ) {
$query1->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'audio-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'audio_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Audio Grid ?>
<?php wp_reset_postdata(); ?>
<div class="videoGrid">
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'order' => 'DESC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'video-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'video_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Video Grid ?>
<?php wp_reset_postdata(); ?>
Another cool thing I'm doing is using a custom field to define the order of things and using meta_key and meta_value_num to get that number and force the order how I want, and since this site isn't complicated, defining the order this way is convenient. I just use leading zeroes to make it easy: 001, 002, 003, etc
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'meta_key' => 'video_order',
'orderby' => 'meta_value_num',
'order' => 'ASC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
Anyway, hope this helps if you need to use multiple loops to pull posts from different categories.

How to show Previous and Next link in WordPress

Below is my example code that is dynamically get the posts.
<?php
global $post;
$args = array( 'numberposts' => 4 );
$the_posts = get_posts( $args );
foreach( $the_posts as $post ){ ?>
//The Post Content Goes here...
?>
The code above will works correctly but my question is, since this is not a default blog page or a category, how can I use the posts_nav_link() so that I can still access the rest of the pages? I tried to used it but it doesn't work unless if the current page is a category. Hope you guys can help me this.
If you giving you paging in your custom post type. then i think you can do very simple you have to use wordpress plugin like wp-pagenavi after then add your custom post type in this plugin in admin panel after then add
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php
global $post;
$args = array( 'numberposts' => 4 );
$the_posts = get_posts( $args );
foreach( $the_posts as $post ){ ?>
//The Post Content Goes here...
?>
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
Without plugin you can use like this
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 3, 'cat' => '-10, -72&paged=' . $paged) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail(); ?> <?php the_title(); ?>
<span><?php the_time('d.m.y') ?></span>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php posts_nav_link(' — ', __('« Previous Page'), __('Next Page »')); ?>

Pagination on WordPress posts within page

I'm trying to do a custom archive for all posts, but I want it to look a little different than the category-specific archives. I've achieved this so far by placing the code below into a page on my site.
Is it possible to add pagination to something like this? I thought that 'paged' => $paged line might do it, but no such luck.
Here's my code: (I'm using a custom thumbnail size if you were wondering what that refers to.)
<?php
global $post;
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'paged' => $paged
);
$thumbnails = get_posts($args);
foreach ($thumbnails as $post)
{
setup_postdata($post);
?>
<div class="featuredarticle">
<h4 class="entry-title"><?php the_title(); ?></h4>
<div class="featuredimage">
<?php the_post_thumbnail('featured'); ?><br />
</div>
</div>
<p><?php the_excerpt(); ?></p>
<div class="entry-utility">
<span class="read-more">Read More</span>
</div>
<?php
}
?>
This code works very good within a page querying posts and use pagination.
<?php
/**
* Template Name: Page of Books
*
* Selectable from a dropdown menu on the edit page screen.
*/
?>
<?php get_header();
if ( have_posts() ) while ( have_posts() ) : the_post();
the_content();
endwhile; wp_reset_query();
?>
<div id="container">
<div id="content">
<?php
$type = 'book';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 2,
'caller_get_posts'=> 1
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
?>
<?php
get_template_part( 'loop', 'index' );?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Resources